WebDriver entry and Improvement

Source: Internet
Author: User
Tags seleniumhq

The first time I came into contact with selenium's Webdriver, it was in a web project. This project uses it for functional testing. When I see that the page content in Firefox is automatically filled and automatically redirected, it is amazing. Through this period of study, I think I can summarize my knowledge about WebDriver.

What are selenium and WebDriver?

Selenium is a browser automation framework. Selenium mainly consists of three tools. The first tool, seleniumide, is an extension of Firefox. It supports user recording and return test. The recording/Return Visit mode has limitations and is not suitable for many users, therefore, the second tool, selenium Webdriver, provides APIs in various language Environments to support more control and write applications that comply with standard software development practices. The last tool, seleniumgrid, helps engineers use the selenium API to control browser instances distributed on a series of machines and supports concurrent running of more tests. Within the project, they are called "ide", "WebDriver", and "Grid" respectively ".

The second tool is WebDriver.

Webdriver is a clean, fast framework for Automatic Testing of webapps. But I think it is not limited to automated testing and can be used for other purposes.

Webdriver is developed for various browsers, replacing JavaScript embedded in the tested web application. Close integration with browsers allows you to create more advanced tests to avoid restrictions caused by JavaScript Security models. In addition to support from browser vendors, WebDriver also simulates user input using operating system-level calls. WebDriver supports Firefox (firefoxdriver) and IE
(Internetexplorerdriver), Opera (operadriver), and chrome
(Chromedriver ). It also supports mobile app testing for Android (androiddriver) and iPhone (iphonedriver. It also includes a non-interface implementation based on htmlunit, known as htmlunitdriver. WebDriver APIs can be accessed through python, Ruby, Java, and C #. developers can use their preferred programming languages to create tests.

 

How to use it?

First, you need to add the jar package of WebDriver to your project classpath. You can download it through http://code.google.com/p/selenium/downloads/list.

If you are using Maven to build your project, you only need to add the following dependencies to the Pom. xml file.

<Dependency>

<Groupid> org. seleniumhq. Selenium </groupid>

<Artifactid> selenium-Java </artifactid>

<Version> 2.25.0 </version>

</Dependency>

<Dependency>

<Groupid> org. seleniumhq. Selenium </groupid>

<Artifactid> selenium-server </artifactid>

<Version> 2.25.0 </version>

</Dependency>

Then you can use it. The WebDriver API follows the "best fit" principle and finds an optimal balance between maintaining good user experience and flexibility.

The following example uses htmlunitdriver. Htmlunitdriver only executes this code in the memory and does not display a real page.

Packageorg. openqa. Selenium. example;

Import org. openqa. Selenium.;
Import org. openqa. Selenium. WebDriver;
Import org. openqa. Selenium. webelement;
Import org.openqa.selenium.html unit. htmlunitdriver;

Public class example {
Public static void main (string [] ARGs ){
// Create a new instance of the HTML unit driver
// Notice that the remainder of the Code relies onthe interface,
// Not the implementation.
WebDriver driver = new htmlunitdriver ();

// And now use this to visit Google
Driver. Get ("http://www.google.com ");

// Find the text input element by its name
Webelement element = driver. findelement (by. Name ("Q "));

// Enter something to search
Element. sendkeys ("Cheese! ");

// Now submit the form. WebDriver will find theform for us from the element
Element. Submit ();

// Check the title of the page
System. Out. println ("page title is:" + driver. gettitle ());
}
}

If you want to use the Firefox browser. You only need to set WebDriver driver = new firefoxdriver (). The premise is that your Firefox is installed in the default location.

Operating System

Default Firefox installation location

Linux

Firefox (found using "which ")

Mac

/Applications/Firefox. APP/contents/MACOs/Firefox

Windows

% ProgramFiles % \ Mozilla Firefox \ firefox.exe

If your Firefox is not installed in the specified location, you can set "WebDriver. Firefox. bin"

Environment variable value to specify its location. The following code can be used in Java:

System. setproperty ("WebDriver. Firefox. bin", "thelocation of Firefox ");

If you want to use the Chrome browser, it will be more troublesome. You need to download a chromedriver (: http://code.google.com/p/chromedriver/downloads/list) first ). This program is provided by the Chrome team. You can think of it as a bridge between WebDriver and chrome. Start chromedriver and you will get a URL and listening port. Then use WebDriver
= Newremotewebdriver (URL, desiredcapabilities. Chrome () to create a chromewebdriver. Of course, you can start chromedriver in a child thread and set it to WebDriver.

File file = new file (your chromedriverfile path );

Chromedriverservice service = newchromedriverservice. Builder (). usingchromedriverexecutable (file). usinganyfreeport (). Build ();

Service. Start ();

WebDriver = new chromedriver (service );

.....

.....

....

Service. Stop ();

 

How does WebDriver work?

Webdriver is a W3C standard hosted by selenium.

Specific protocol standards can be viewed from the http://code.google.com/p/selenium/wiki/JsonWireProtocol#Command_Reference.

From this protocol, we can see that the reason why WebDriver can interact with the browser is that the browser implements these protocols. This protocol uses josn for transmission over HTTP.

It uses the classic client-server mode. The client sends a requset and the server returns a response.

We define several concepts.

Client

The machine that calls webdriverapi.

Server

The machine that runs the browser. Firefox directly implements the communication protocol of Webdriver, while chrome and IE are implemented through chromedriver and internetexplorerdriver.

Session

The server needs to maintain the Session of the browser. The request header sent from the client contains the session information, and the server executes the corresponding browser page.

Webelement

This is an object in webdriverapi, representing a DOM element on the page.

For example, the following code uses the "command" Firefox to jump to the Google homepage:

 

WebDriver driver = new firefoxdriver ();
// Instantiate a driver
 
Driver. Get ("http://www.google.com ");

 

When executing the driver. Get ("http://www.google.com") Code, the client, our test code, sends the following request to the remote server:

Postsession/285b12e4-2b8a-4fe6-90e1-c35cba245956/URL post_data {"url": "http://google.com "}

Request the localhost: Port/hub/session/session_id/URL address through post, and request the browser to complete the jump URL operation.

If the above request is acceptable, or the remote server implements this interface, the remote server will jump to the URL contained in the post data and return the following response

{"Name": "Get", "sessionid": "285b12e4-2b8a-4fe6-90e1-c35cba245956", "status": 0, "value ":""}

The response contains the following information:

Name: the name of the implementation method on the remote server. Get indicates that the method is redirected to the specified URL;

Sessionid: ID of the current session;

Status: Status Code of the request execution. If it is not 0, it indicates that the request is not executed correctly. If it is 0, it indicates that everything is OK;

Value: Return Value of the request. The returned value is null. If the client calls the title interface, the value should be the title of the current page;

If the client sends a request to locate a specific page element, the returned value of response may be as follows:

{"Name": "findelement", "sessionid": "effect", "status": 0, "value": {"element": "{2192893e-f260-44c4-bdf6-7aad3c919739 }"}}

Name, sessionid, and status are similar to the preceding example. The difference is that the returned value of this request is element: {2192893e-f260-44c4-bdf6-7aad3c919739}, indicating that the element ID is located, the client can send a request such as click to interact with the server.

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.