Selenium 2.0 Webdriver User Guide

Source: Internet
Author: User
Tags tagname xpath seleniumhq

Seleniumwebdriver
Note: We are working on perfecting each chapter of the Help guide, although there are still areas to be perfected, but we firmly believe that the help information you see today is accurate, and we will provide more guidance to refine the help documentation.
1.WebDriver Introduction
The main new feature of Selenium2.0 is the integration of WEBDRIVERAPI. We designed Webdriver to provide a more straightforward interface to compensate for SELENIUM-RCAPI deficiencies. In Dynamic Web pages, only local HTML elements are usually updated, and Webdriver helps the user quickly locate the elements. Our ultimate goal is to address the modern advanced Web page by providing a well-designed object-oriented APITestProblem.
2.WebDriver How to driveBrowser? What's the difference with SELENIUM-RC?
Different types of browsers have native interfaces that support automated operations, and selenium send instructions directly to the browser via these interfaces. How you send these instructions depends on the type of browser you are currently using, which we'll cover later in this section.
It seems that the webdriver is similar to the previous SELENIUM-RC, in fact there is an essential difference between the two. The same approach is used for all types of browser SELENIUM-RC: When the browser starts, it injects JavaScript into it to use these JS to drive the AUT (applicationundertest) in the browser. Webdriver does not use this technique, it drives the browser directly by invoking the native Automation API of the browser.
3.WebDriver and Seleniumserver
Whether you need to use seleniumserver depends on the way you use Webdriver. The following two cases do not need to use seleniumserver,webdriver directly to run the browser: 1, testcases only use Webdriver api;2, browser and testcase on the same PC, And testcases only used the Webdriver API.
There are three situations in which you need to combine seleniumserver to use the Webdriver:
1) Use Selenium-grid to manage testcase on clustered environments (or virtual machines);
2) need to invoke a different version of the browser on a non-native computer;
3) do not use any languagebinding (Java/c#/python/ruby), and have the intention to use Htmlunitdriver.
4. Configure Selenium-webdriver Engineering
Installation of selenium refers to theDevelopment EnvironmentA project can then be programmed in this project using selenium. How you configure it depends on the development language and programming environment you use.
Using MAVEN is the simplest way to configure a Selenium2.0java project. Maven downloads all javabingdings and all associated libraries (theselenium2.0javaclientlibrary). By using Pom.xml (Maven profile) to create a new project, you can import the MAVEN project into Intellijidea or eclipse to your liking.
First, create a folder to hold the MAVEN project file. Then, create the Pom.xml, which you can edit using TextEditor. Given that there are a lot of good references to "how to use Pom.xml in Maven engineering," There is no longer much discussion of the details. Here is an example of creating a similar file for your project.
<?xmlversion= "1.0" encoding= "UTF-8"?>
<projectxmlns= "http://maven.apache.org/POM/4.0.0"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "Http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd" >
<modelVersion>4.0.0</modelVersion>
<groupId>MySel20Proj</groupId>
<artifactId>MySel20Proj</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.38.0</version>
</dependency>
<dependency>
<groupId>com.opera</groupId>
<artifactId>operadriver</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.opera</groupId>
<artifactId>operadriver</artifactId>
<version>1.5</version>
<exclusions>
<exclusion>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</dependencyManagement>
</project>

Please make sure that the webdriver you are using is the latest current version. The above example gives the latest version when this document is written. Shortly after the release of Selenium2.0, Webdriver had frequent updates. Please confirm the current version in this link mavendownloadpage and modify the Pon.xml in your project accordingly.
Now you can use the DOS interface to access the project folder using the CD command, and run maven with the following command.
Mvncleaninstall
The selenium and related kits are automatically downloaded and loaded into your project after running.
Finally, import your project into your preferred IDE. If you are not very clear about the process of importing, we have prepared the Operation guide.
Importingamavenprojectintointellijidea.importingamavenprojectintoeclipse
5. How to migrate automated engineering from Selenium1.0 to Selenium2.0
has been built on the Selenium1.0TestEngineering users, we provide you with a guide on how to migrate existing code to Selenium2.0. Selenium2.0 's chief development engineer, Simonstewart, wrote an article on this: Magratingfromseleniumrctoseleniumwebdriver.
6.SELENIUM-WEBDRIVERAPI Introduction
Webdriver can be used to implement the Web application'sAutomated Testing, especially for scenarios that verify that the actual results match the expected results. Webdriver is designed to provide an easy-to-use, user-friendly API that is easier to explore and understand than Selenium1.0, enablingTestUse cases become easy to read and maintain. Webdriver does not use any third partyTestFramework, so it can be used in conjunction with the Unit Test tool or the oldest main function. This section will show you how to use the Webdriver API to help you get started with Webdriver. If you have not yet created a new selenium project, please do this first, with a detailed description in this section.
Once you have created the Selenium project, you will find that Webdriver is completely independent of the normal third-party libraries, and you do not need to start any additional processes or installers before you use them, but if you use SELENIUM-RC you need to start the proxy server first.
Note: Additional steps are required when you use the following webdriver: Chromedriver,operadriver,androiddriver,iphonedriver.
Now you must be tempted to write some code. Let's start the first journey with a simple example: Search for "Cheese" on Google and print out the title of the search results page.
Packageorg.openqa.selenium.example;
Importorg.openqa.selenium.By;
Importorg.openqa.selenium.WebDriver;
Importorg.openqa.selenium.WebElement;
Importorg.openqa.selenium.firefox.FirefoxDriver;
Importorg.openqa.selenium.support.ui.ExpectedCondition;
importorg.openqa.selenium.support.ui.WebDriverWait;
publicclassselenium2example{
Publicstaticvoidmain (String[]args) {
Create a Firefoxdriver instance
This class relies on interfaces rather than implementations of interfaces
Webdriverdriver=newfirefoxdriver ();
Accessing Google using the Get method
Driver.get ("http://www.google.com");
Using this method can also achieve the purpose of accessing Google
Driver.navigate (). to ("http://www.google.com");
Find the name of the HTML input box
Webelementelement=driver.findelement (By.name ("Q"));
Enter what you are looking for
Element.sendkeys ("cheese!");
Submit the form, Webdriver will automatically find the form where we need to submit the element
Element.submit ();
Print the title of a Web page
System.out.println ("Pagetitleis:" +driver.gettitle ());
Google's search page will be rendered dynamically via JS
Wait for the page to load, time-out is 10 seconds
(Newwebdriverwait (driver,10)). Until (Newexpectedcondition<boolean> () {
Publicbooleanapply (webdriverd) {
Returnd.gettitle (). toLowerCase (). StartsWith ("cheese!");
}
});
The following information will be printed on the console: "Cheese!-googlesearch"
System.out.println ("Pagetitleis:" +driver.gettitle ());
Close browser
Driver.quit ();
}
}
In the next section of this chapter, we will learn how to use Webdriver to manipulate your browser, how to use frames and windows toTestWeb site. Of course, we will provide more detailed discussion and examples.
7.selenium-webdriverapi detailed
7.1 Getting a Web page
The first thing we need to do is to get control of the Web page through Webdriver, using the Get method in general
Driver.get ("http://www.google.com");
In some cases, such as the combination of operating system and browser, Webdriver may not wait for Web page loading to complete, in which case Webdriver will return an error or run the next operation directly. In order to ensure the robustness of the program, you need to wait for an element in the page to load and then proceed to the next step, please refer to explicitandimplicitwaits.
7.2 Positioning UI Elements
We can locate UI elements through Webdriver instances or webelement classes. We offer two methods for each programming language: "Findelement" and "findelements". The first method returns a Webelement and throws an exception if it is not found. The second method returns a Webelement linked list (list), which returns an empty list if no DOM element is found.
The Find method uses a class like a probe, which is called by. Some common methods of by are listed below:
Byid
When we locate a UI element, this is the most effective and the best way. However, this method is not a panacea, and some front-end development in the design of UI elements will omit the ID or use the dynamic ID, in both cases to avoid the use of this method. It is more appropriate to use the Get class name method at this time than Byid.
Example: How to use this method to position an element
<divid= "Coolestwidgetevah" >...</div>
Webelementelement=driver.findelement (By.id ("Coolestwidgetevah"));

Byclassname
In this scenario, we reference the properties of the DOM element. The reality is that many elements have the same classname, so finding multiple elements with the same classname is more important than finding the first element that has the classname.
Example: How to use this method to position an element
<divclass= "cheese" ><span>cheddar</span></div><divclass= "cheese" ><span> Gouda</span></div>
List<webelement>cheeses=driver.findelements (By.classname ("cheese"));
Bytagname
The name of the DOM element tag.
Example: How to use this method to position an element
<iframesrc= "..." ></iframe>
Webelementframe=driver.findelement (By.tagname ("iframe"));
ByName
Find the same INPUT element as the Name property.
Example: How to use this method to position an element
<inputname= "Cheese" type= "text"/>
Webelementcheese=driver.findelement (By.name ("cheese"));
Bylinktext
Finds a hyperlink that exactly matches the Text property.
Example: How to use this method to position an element
<ahref= "Http://www.google.com/search?q=cheese" >cheese</a>
Webelementcheese=driver.findelement (By.linktext ("cheese"));
Bypartiallinktext
Finds a hyperlink that matches the Text property blur.
Example: How to use this method to position an element
<ahref= "Http://www.google.com/search?q=cheese" >searchforcheese</a>
Webelementcheese=driver.findelement (By.partiallinktext ("cheese"));
Bycss
This method name means that it is a CSS probe. If the browser supports this method by default, it is recommended that you build a CSS selector based on the standard documentation. If the browser does not support CSS selectors, you can use sizzle. ie6,7 and FireFox3.0 are using sizzle as the CSS query engine.
Note that not all browsers use the same CSS selector expression, and some CSS may only take effect in one version.
Example: How to use this method to position an element
<divid= "Food" ><spanclass= "dairy" >milk</span><spanclass= "dairyaged" >cheese</span> </div>
Webelementcheese=driver.findelement (By.cssselector ("#foodspan. dairy.aged"));
Byxpath
Webdriver can also use the browser's own XPath when needed. For browsers that do not support XPath, we provide a unique implementation of Webdriver. Make sure you are familiar with the differences between XPath in different engines, or you will cause some unexpected problems.
Driver case Sensitive property value is visible whether it supports xapth
Htmlunitdriver only identify lowercase visible yes
Iedriver only identify lowercase visible no
Firefoxdiver case insensitive visible is the above table has some abstractions, let's take a look at an example
<inputtype= "text" name= "Example"/>
<inputtype= "text" name= "other"/>
List<webelement>inputs=driver.findelements (By.xpath ("//input"));
The matching results are as follows
XPath expression htmlunitdriverfirefoxdriveriedriver
input122
INPUT020 Some label properties have default values, in which case the default value is matched if the attribute value is not specified. For example, the "Input" label "type" property defaults to "text". The first principle of using XPath is not to ignore these hidden implementations.
Using JavaScript
As long as the return is a webelement, you can also use arbitrary JS code to find the Web element, according to the query results will be automatically modified to a Webelement object.
A simple example of using jquery:
Webelementelement= (Webelement) ((javascriptexecutor) driver). Executescript ("return$ ('. Cheese ') [0]");
Find all input elements for each label in the page:
List<webelement>labels=driver.findelements (By.tagname ("label"));
List<webelement>inputs= (list<webelement>) ((javascriptexecutor) driver). Executescript (
"Varlabels=arguments[0],inputs=[];for (vari=0;i<labels.length;i++) {" +
"Inputs.push (document.getElementById (Labels[i].getattribute (' for"));} returninputs; ", labels);
7.3 Simulating user input behavior
We've shown how to enter text in a text box, what other web elements should do? You can trigger an option in the checkbox, or you can select an option for select. Webdriver processing the Select element is also straightforward.
Webelementselect=driver.findelement (By.tagname ("select"));
List<webelement>alloptions=select.findelements (by.tagname ("option"));
for (webelementoption:alloptions) {
System.out.println (String.Format ("valueis:%s", Option.getattribute ("value"));
Option.click ();
}
In the example above, the first SELECT element in the Web page is selected, and the value of the option is printed in a loop and the option is clicked. You may have noticed that using this method is not the most effective. Webdriver provides a "select" class that is better suited to handle this scenario.
Selectselect=newselect (Driver.findelement (By.tagname ("select"));
Select.deselectall ();
Select.selectbyvisibletext ("Edam");
In the example above, first remove the focus of the selected first option, and then select the option with the value "Edam".
Once you've finished typing all the form fields, the next step is to submit the form. One way is to find the Submit button in the Web page and click:
Driver.findelement (By.id ("submit")). Click ();
As an alternative, the element class of Webdriver has a more convenient method of "Sublmit". If you use this method on an element in the form, Webdriver will be on the DOM object where it resides until it finds the form it belongs to and submits it. If the element is not in a form, an exception nosuchelementexception will be thrown.
Element.submit ();
7.4 Switching between windows and frames
Some Web programs contain many frames and windows, and Webdriver provides a "SwitchTo" method to switch between:
Driver.switchto (). Window ("Windowname");
All the specified transfers to Webdriver will be transmitted to the switched window. How do I get to the name of the window? To view JS and open the window, you can:
<ahref= "somewhere.html" target= "Windowname" >Clickheretoopenanewwindow</a>
As an alternative, you can use a window handle to pass to the SwitchTo (). Window () method. Based on this method, all open windows will be traversed using iterators:
For (Stringhandle:driver.getWindowHandles ()) {
Driver.switchto (). window (handle);
}
You can also switch between frames (or into a frame):
Driver.switchto (). FRAME ("framename");
You can also use the frame's child frame according to the path, and you can position the frame by index.
Driver.switchto (). FRAME ("Framename.0.child");
The above method switches to the first child frame of the frame with the name "FrameName", and all frames are the topmost start count of the Web page.
7.5 Popup Box
SELENIUM2.0BETA1 version, we provide methods to get the popup box. After you trigger the popup box, you can use the following method to enter the popup box:
Alertalert=driver.switchto (). alert ();
The above method will return the currently open Alert object and you can do anything to manipulate it: Click Cancel, click OK, close the window, get the text content of alert, etc. This interface has a good application on alerts, confirms, prompts objects, see the API documentation for details.
7.6Navigation: Browser Local History
In the previous article, we used the Get method to get the Web page (driver.get ("http://www.example.com")). As you can see, Webdriver has a lot of lightweight, feature-focused interfaces, and navigation is one such. Just because loading a webpage is a common requirement, this method exists under the driver class, but the usage is simple:
Driver.navigate (). to ("http://www.example.com");
Again, "navigate (). to ()" and "Get ()" Do the same thing, but one of them is more suitable for printing.
The Navigate interface also provides methods to page back and forth in browser history.
Driver.navigate (). Forward ();
Driver.navigate (). back ();
Please note that the above features are entirely dependent on the underlying browser. If you are accustomed to cross-browser operations, unexpected exceptions can occur when you use these interfaces.

7.7Cookies
Before we start the next step, you may be interested in how Webdriver operates local cookies. First, you must be in the scope of the current cookie. If you try to pre-provision a cookie before you open a webpage, and your page is large to take a long time to load, you need to find a smaller page instead, such as the HTTP404 Web page (http://example.com/some404page).
Open a cookie-acting website
Driver.get ("http://www.example.com");
Set Global Cookie
Cookiecookie=newcookie ("Key", "value");
Driver.manage (). Addcookie (cookie);
Output all available cookies for the current web page
Set<cookie>allcookies=driver.manage (). GetCookies ();
for (cookieloadedcookie:allcookies) {
System.out.println (String.Format ("%s->%s", Loadedcookie.getname (), Loadedcookie.getvalue ()));
}
You have three ways to delete cookies
ByName
Driver.manage (). deletecookienamed ("CookieName");
Bycookie
Driver.manage (). Deletecookie (Loadedcookie);
Orallofthem
Driver.manage (). Deleteallcookies ();
?? 7.8 Modifying the user proxy server
?? It's easy for Firefox:
Firefoxprofileprofile=newfirefoxprofile ();
Profile.addadditionalpreference ("General.useragent.override", "someuastring");
Webdriverdriver=newfirefoxdriver (profile);
7.9 Dragging web elements
The following is an example of a drag-and-drop Web page element, provided that the local event must be available.
Webelementelement=driver.findelement (By.name ("source"));
Webelementtarget=driver.findelement (By.name ("target"));
(Newactions (Driver)). Draganddrop (Element,target). Perform ();
8 Characteristics of various driver and how to choose the right driver
In translation
9 forward Compatibility: Fused Webdriver and SELENIUM-RC
In translation
10 separate boot for remote Webdriver seleniumserver
In translation
Translator Note:
1, the original link: http://www.seleniumhq.org/docs/03_webdriver.jsp.
2, the text contains only Java-related operations, Webdriver also support C#/python/ruby/perl/php/perl, if necessary, please read the original text.
3, languagebinding, also called Gluecode, meaning is the glue code, for example has a C + + Lib library, Java calls this library API is called javabinding. Reference: Http://en.wikipedia.org/wiki/Language_binding.
4, the wording is poor, some words and sentences did not dig into the literal translation, deeply from reading to translation is not only a dictionary, there are cultural differences. The author strongly recommends reading the original text of the official website directly, if my translation causes misunderstanding to you, deeply uneasy. This is also the last three chapters do not dare to continue to swim, and so I selenium familiar with the later back to fill the whole.

This article was selected from: http://www.spasvo.com/ceshi/open/kygncsgj/Selenium/2014214135011.html

Selenium 2.0 Webdriver User Guide

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.