Selenium2 (2) WebDriver, selenium2webdriver

Source: Internet
Author: User

Selenium2 (2) WebDriver, selenium2webdriver

As Selenium2 (1) mentioned above, Selenium is a Web application testing framework. If you need a simple web application: open a browser, log on to the Baidu homepage, and enter the keyword "European Cup, can Selenium be used to perform a series of operations by clicking the search button? Yes. The WebDriver described below is one of the methods to complete this task:

Webdriver (Selenium2) is an automatic testing tool for Web applications. It provides a friendly set of APIS, compared with Selenium 1 (Selenium-RC, the API of Selenium 2 is easier to understand and use, and its readability and maintainability are also greatly improved. Webdriver is a set of class libraries and does not rely on any testing framework. Apart from the necessary browser drivers, you do not need to start other processes or install other programs, or start services as Selenium 1 does.

In addition, the two adopt different technical solutions. Selenium 1 runs JavaScript in the browser for testing, while Selenium 2 directly controls the browser through Native browser support or browser extension.

Developed for various browsers, Selenium 2 replaces JavaScript embedded in the tested Web application. Close integration with browsers allows you to create more advanced tests to avoid restrictions on JavaScript Security models. In addition to support from browser vendors, Selenium 2 also simulates user input using operating system-level calls. WebDriver support

  • Firefox (FirefoxDriver)

  • IE (InternetExplorerDriver)

  • Opera (OperaDriver)

  • Chrome (ChromeDriver)

  • And safari)

It also supports mobile app testing for Android (Selendroid) and iPhone (Appium. In addition, Selenium 2 includes HtmlUnit-based non-interface implementation, known as HtmlUnitDriver (which will be introduced later), and webkit-based non-interface browser phantomjs. Selenium 2 API

  • Java

  • C #

  • PHP

  • Python

  • Perl

  • Ruby

Allows developers to use common programming languages to create tests.

To put it bluntly, to test the above functions using WebDrive, we should first build the development environment:

I. Development Environment:

1. JDK

2. MyEclipse

3, Selenium: selenium-java-2.53.1.zip,: http://code.google.com/p/selenium/downloads/list

Decompress the selenium-java package, which contains four parts, for example:

2. Create a Java Project:

1. Copy the jar files (including libs and selenium-java-2.53.1.jar) extracted above to the new project directory, the directory structure is as follows:

2. Add build path and right-click the project directory --> Build Path --> config build path --> Java Build Path --> Libraries --> Add JARs

Add all the jar packages under the libs folder, and then add selenium-java-2.39.0 and selenium-java-2.39.0-srcs

3. After adding the directory structure, for example, adding Referenced Libraries, here is the jar package added in the previous step:

4. Associate selenium source code:

Now, the environment is ready.

3. Compile the test class

Import org. openqa. selenium. by; import org. openqa. selenium. webDriver; import org. openqa. selenium. webElement; import org. openqa. selenium. firefox. firefoxDriver; public class SeleniumTest {/*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stub // If Firefox is not installed on drive C by default, You need to specify its path // System. setProperty ("webdriver. firefox. bin "," D:/Program Files/Mozilla firefox/firefox.exe "); WebDriver driver = new FirefoxDriver (); driver. get ("http://www.baidu.com/"); driver. manage (). window (). maximize (); WebElement txtbox = driver. findElement (. name ("wd"); txtbox. sendKeys ("European Cup"); WebElement btn = driver. findElement (. id ("su"); btn. click (); // For the convenience of viewing the test results, you can close the browser operation comments // driver. close ();}}

In this way, you can see that the program will automatically open FF, automatically enter the European Cup, and then the query results will appear.

When you observe the program execution process, you will find that it takes some time to load the page, whether it is to open the browser or when we simulate entering keywords and click to search, webdriver can only find the existing elements of the page, so sometimes you need to add a waiting time so that the page elements can be fully loaded before you can find the desired elements through the findelement method. Therefore, it is best to add a waiting statement after the click statement. The program programming is as follows:

1 import org. openqa. selenium. by; 2 import org. openqa. selenium. webDriver; 3 import org. openqa. selenium. webElement; 4 import org. openqa. selenium. firefox. firefoxDriver; 5 6 7 public class SeleniumWebDriverSimpleDemo {8 9/** 10 * @ param args11 */12 public static void main (String [] args) {13 // TODO Auto-generated method stub14 // If Firefox is not installed on drive C by default, set the path to 15 // System. setProperty ("webdriver. firefox. bin "," D:/Program Files/Mozilla firefox/firefox.exe "); 16 WebDriver driver = new FirefoxDriver (); 17 driver. get ("http://www.baidu.com/"); 18 19 driver. manage (). window (). maximize (); 20 21 WebElement txtbox = driver. findElement (. name ("wd"); 22 txtbox. sendKeys ("European Cup"); 23 24 WebElement btn = driver. findElement (. id ("su"); 25 btn. click (); 26 try {27 28 Thread. sleep (3000); 29 30} catch (InterruptedException e) {31 32 // TODO Auto-generated catch block33 34 e. printStackTrace (); 35 36} 37 38 39 // For ease of viewing test results, comment out the close browser operation 40 // driver. close (); 41 42} 43 44}

This sleep () is not a self-contained method of WebDriver, but a method of sleeping threads in java. It is not recommended here. WebDriver comes with a smart wait method.

 

driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

 

 

Arg0: length of wait time, int type;

Arg1: the unit of the waiting time, TimeUnit. SECONDS. Generally, SECONDS are used as the unit.

 

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.