How to Use selenium for automated page Testing

Source: Internet
Author: User

This is a test topic and a development topic. Selenium is a good choice for Web applications that require automated page tests. Selenium is an automated testing framework with IDE and API interfaces. It can be used in Java, C #. Python, Ruby, and other languages. Use selenium to build an automated testProgramVery simple. First, you need to be familiar with the request and response concepts in web applications and the usage of xpath. Here I will introduce how to use JUnit and selenium for automated page testing.

1. download the necessary dependent file selenium-server-standalone-2.25.0.jar, junit-4.7.jar, and place them under the Lib folder of the project (I am using the Firefox browser as the client here, so there is no need to download additional browser actuators, if you want to use IE or chrome as the client, download the corresponding executor.

Http://code.google.com/p/selenium/downloads/list)

2. Create a test project, create a test file in the project, and add the followingCode:

Import COM. thoughtworks. selenium. selenium; import JUnit. framework. testcase; import Org. JUnit. after; import Org. JUnit. before; import Org. JUnit. test; import Org. JUnit. runner. runwith; import Org. JUnit. runners. blockjunit4classrunner; import Org. openqa. selenium. by; import Org. openqa. selenium. webDriver; import Org. openqa. selenium. webdriverbackedselenium; import Org. openqa. selenium. webelement; import Org. openqa. selenium. firefox. firefoxdriver; import Org. openqa. selenium. internal. wrapsdriver; import Org. openqa. selenium. support. UI. wait; import Org. openqa. selenium. support. UI. webdriverwait; import Java. io. ioexception; import static Org. openqa. selenium. support. UI. expectedconditions. visibilityofelementlocated; @ runwith (blockjunit4classrunner. class) public class picktest extends testcase {protected static selenium; Private Static WebDriver driver; @ before public void createandstartservice () throws ioexception {selenium = new webdriverbackedselenium (New firefoxdriver (), ""); driver = (wrapsdriver) selenium ). getwrappeddriver () ;}@ after public void createandstopservice () {driver. quit () ;}@ test public void should_open_google_page () throws interruptedexception {driver. get ("http://www.google.com.hk ");Webelement searchbox = driver. findelement (by. XPath ("// * [@ ID = \" lst-Ib \ "]"); Searchbox. sendkeys ("Selenium"); webelement searchbutton = driver. findelement (. XPath ("// * [@ ID = \" TSF \ "]/Div [2]/Div [3]/center/input [1]"); searchbutton. click (); Wait <WebDriver> wait = new webdriverwait (driver, 30); wait. until (visibilityofelementlocated (. XPath ("// * [@ ID = \" AB _name \ "]/span "))); }}

3. Run this test. You will see that the firebox browser is automatically started, and then you will automatically enter selenum and search.

In this way, a simple automated page test is completed. Some may not understand the meaning of this Code. In the above code, I marked the red and blue parts. I will explain them briefly. Selenium performs page processing by wrapping the browser. Therefore, we will first create a WebDriver object related to the browser. Then we need to find the page element to obtain the Page Object (the red part of the Code) through the findeelement method and the XPath method ). Generally, it takes some time for a click operation to generate a server. The code in the blue part is to create a waiting object. You can use XPath to determine which element on the page is loaded after the returned result, at the same time, the waiting object also has a timeout setting, which means that the server never returns or fails. We can still end the test. How to quickly determine the XPath of page elements is as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.