NOTE: We’re currently working on documenting these sections. We believe the information here is accurate, however be aware we are also still working on this chapter. Additional information will be provided as we go which should make this chapter more solid.

我们目前需要使用这部分的文档来工作,我们相信这里的信息都是正确。附加信息将会使这部分的内容更加坚实。

Introducing WebDriver

The primary new feature in Selenium 2.0 is the integration of the WebDriver API. WebDriver is designed to provide a simpler, more concise programming interface in addition to addressing some 

一些初级的新的特性在2.0中是集成在WebDriver  API中的。 Webdriver会提供一个简便的、更简洁的编程接口,除了处理一些限制的API。

limitations in the Selenium-RC API. Selenium-WebDriver was developed to better support dynamic web pages where elements of a page may change without the page itself being reloaded.

Selenium-WebDriver是为了更好地支持开发动态web页面,页面元素没有被重新加载页面本身可能会改变。

 WebDriver’s goal is to supply a well-designed object-oriented API that provides improved support for modern advanced web-app testing problems.

WebDriver的目标是提供一个精心设计的面向对象的API,提供了现代先进的web测试问题改进的支持。

How Does WebDriver ‘Drive’ the Browser Compared to Selenium-RC?

Selenium-WebDriver makes direct calls to the browser using each browser’s native support for automation. How these direct calls are made, and the features they support depends on the 

Selenium-WebDriver使直接调用浏览器使用每个浏览器的本地支持自动化。如何将这些直接调用,和它们所支持的特性取决于您所使用的浏览器。

browser you are using. Information on each ‘browser driver’ is provided later in this chapter.

每个浏览器的司机的信息在本章后面提供。每个浏览器的驱动信息将在后边章节中提供。

For those familiar with Selenium-RC, this is quite different from what you are used to. Selenium-RC worked the same way for each supported browser. It ‘injected’ javascript functions into the

对于那些熟悉selenium rc,这是完全不同于你所使用过的。selenium rc为每个受支持的浏览器以同样的方式工作。

 browser when the browser was loaded and then used its javascript to drive the AUT within the browser. WebDriver does not use this technique. Again, it drives the browser directly using the 

它“注入”javascript函数到浏览器当浏览器加载,然后利用其javascript驱动AUT中浏览器。WebDriver 不使用这些技术。再有, 它直接驱动浏览器使用浏览器自动支持编译。

browser’s built in support for automation.

WebDriver and the Selenium-Server

You may, or may not, need the Selenium Server, depending on how you intend to use Selenium-WebDriver. If you will be only using the WebDriver API you do not need the Selenium-Server. If your browser and tests will all run on the same machine, and your tests only use the WebDriver API, then you do not need to run the Selenium-Server; WebDriver will run the browser directly.

There are some reasons though to use the Selenium-Server with Selenium-WebDriver.

  • You are using Selenium-Grid to distribute your tests over multiple machines or virtual machines (VMs).

  • 您正在使用selenium grid将测试分配到多台机器上或虚拟机(vm)。

  • You want to connect to a remote machine that has a particular browser version that is not on your current machine.

你想要连接到远程计算机,一个特定的浏览器版本,不是你现在的机器上。

  • You are not using the Java bindings (i.e. Python, C#, or Ruby) and would like to use HtmlUnit Driver

  • 你不使用Java绑定(例如Python,c#或Ruby),愿用HtmlUnit驱动。

Setting Up a Selenium-WebDriver Project


To install Selenium means to set up a project in a development so you can write a program using Selenium. How you do this depends on your programming language and your development environment.

安装Selenium意味着建立一个项目在 开发工具,这样你可以使用Selenium写一个程序。你如何做到这一点取决于你的编程语言和开发环境。

Java

The easiest way to set up a Selenium 2.0 Java project is to use Maven. Maven will download the java bindings (the Selenium 2.0 java client library) and all its dependencies, and will create the 

最容易的方法是建立一个Selenium2.0java项目用Maven.Maven将下载java绑定(Selenium2.0 java客户端库)及其所有依赖项。并将为你创建一个项目,用Maven pom.xml (project configuration) file.

project for you, using a maven pom.xml (project configuration) file. Once you’ve done this, you can import the maven project into your preferred IDE, IntelliJ IDEA or Eclipse.

一旦你这样做,你可以将maven项目导入到你喜欢的IDE,IntelliJ IDEA或Eclipse。

First, create a folder to contain your Selenium project files. Then, to use Maven, you need a pom.xml file. This can be created with a text editor. We won’t teach the details of pom.xml files or for 

首先, 创建一个文件夹包含你的Selenium项目文件 。 然后,你用Maven ,你需要一个pom.xml file。 这将创建一个文本编辑器,我们会提供一些 细节针对pom.xml files

using Maven since there are already excellent references on this. Your pom.xml file will look something like this. Create this file in the folder you created for your project.

使用Maven,因为已经有很好的参考。xml文件将会看起来像这样。创建这个文件在您为您的项目创建的文件夹。



        4.0.0
        MySel20Proj
        MySel20Proj
        1.0
        
            
                org.seleniumhq.selenium
                selenium-java
                2.53.0
            
            
                org.seleniumhq.selenium
                selenium-htmlunit-driver
                2.20
            
        

Be sure you specify the most current version. At the time of writing, the version listed above was the most current, however there were frequent releases immediately after the release of Selenium 2.0. Check the Maven download page for the current release and edit the above dependency accordingly.

确保您指定最新版本。在写这篇文章的时候,上面列出的版本是最新的,然而之后有频繁的发布Selenium .检查Maven下载页面的当前版本和编辑上述相应的依赖.

Now, from a command-line, CD into the project directory and run maven as follows.

现在,从一个命令行,cd命令进入项目目录 并运行Maven按照如下。

mvn clean install

This will download Selenium and all its dependencies and will add them to the project.

这将下载Selenium及其所有依赖项并将它们添加到项目中。

Finally, import the project into your preferred development environment. For those not familiar with this, we’ve provided an appendix which shows this.

最后,将项目导入到您的开发环境。对于那些不熟悉这个,我们提供一个显示了这个附录。

Importing a maven project into IntelliJ IDEA. Importing a maven project into Eclipse.

Introducing the Selenium-WebDriver API by Example

WebDriver is a tool for automating web application testing, and in particular to verify that they work as expected. It aims to provide a friendly API that’s easy to explore and understand, easier to 

WebDriver是自动化的工具web应用程序测试,特别是确认他们是否按预期的方式工作。它旨在提供一个友好的API很容易探索和理解,

use than the Selenium-RC (1.0) API, which will help to make your tests easier to read and maintain. It’s not tied to any particular test framework, so it can be used equally well in a unit testing or 

使之比1.0的API更容易 ,这将有助于使您的测试更容易阅读和维护。不与任何特定的测试框架,它同样可以使用在一个单元测试从一个普通的“主要”方法。

from a plain old “main” method. This section introduces WebDriver’s API and helps get you started becoming familiar with it. Start by setting up a WebDriver project if you haven’t already. 

本节介绍WebDriver API和帮助你开始熟悉它。首先建立一个WebDriver项目如果你还没有准备好。

This was described in the previous section, Setting Up a Selenium-WebDriver Project.

这是在前一节中描述

Once your project is set up, you can see that WebDriver acts just as any normal library: it is entirely self-contained, and you usually don’t need to remember to start any additional processes or 

一旦设置您的项目,您可以看到WebDriver行为就像任何正常库:它是完全独立的,你通常不需要记住任何额外的流程或开始或

run any installers before using it, as opposed to the proxy server with Selenium-RC.

运行安装程序之前使用它,而不是与selenium rc代理服务器。

Note: additional steps are required to use ChromeDriver, Opera Driver, Android Driver and iOS Driver

You’re now ready to write some code. An easy way to get started is this example, which searches for the term “Cheese” on Google and then outputs the result page’s title to the console.

你现在准备编写一些代码。一个简单的方法开始是这个例子中,这在谷歌上搜索“奶酪”这个词,然后输出结果页面的标题到控制台。

package org.openqa.selenium.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Selenium2Example  {
    public static void main(String[] args) {
        // Create a new instance of the Firefox driver
        // Notice that the remainder of the code relies on the interface, 
        // not the implementation.
        WebDriver driver = new FirefoxDriver();

        // And now use this to visit Google
        driver.get("http://www.google.com");
        // Alternatively the same thing can be done like this
        // driver.navigate().to("http://www.google.com");

        // Find the text input element by its name
        WebElement element = driver.findElement(By.name("q"));

        // Enter something to search for
        element.sendKeys("Cheese!");

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

        // Check the title of the page
        System.out.println("Page title is: " + driver.getTitle());
        
        // Google's search is rendered dynamically with JavaScript.
        // Wait for the page to load, timeout after 10 seconds
        (new WebDriverWait(driver, 10)).until(new ExpectedCondition() {
            public Boolean apply(WebDriver d) {
                return d.getTitle().toLowerCase().startsWith("cheese!");
            }
        });

        // Should see: "cheese! - Google Search"
        System.out.println("Page title is: " + driver.getTitle());
        
        //Close the browser
        driver.quit();
    }
}

In upcoming sections, you will learn more about how to use WebDriver for things such as navigating forward and backward in your browser’s history, and how to test web sites that use frames and windows. We also provide a more thorough discussions and examples.

在接下来的部分中,您将学习更多关于如何使用WebDriver向前和向后导航,例如在你的浏览器的历史,以及如何测试web站点使用帧和窗户。我们还提供一个更详细的讨论和示例。

Selenium-WebDriver API Commands and Operations

Fetching a Page

The first thing you’re likely to want to do with WebDriver is navigate to a page. The normal way to do this is by calling “get”:

你可能想要做的第一件事和WebDriver导航到一个页面。正常的方法是通过调用“get”:

driver.get("http://www.google.com");

Dependent on several factors, including the OS/Browser combination, WebDriver may or may not wait for the page to load. In some circumstances, WebDriver may return control before the page has finished, or even started, loading. To ensure robustness, you need to wait for the element(s) to exist in the page using Explicit and Implicit Waits.

取决于几个因素,包括操作系统/浏览器组合,WebDriver可能会或可能不会等待页面加载。在某些情况下,WebDriver可能返回控制页面完成之前,甚至开始装载。为了确保加载速度,您需要等待页面中的元素(s)存在使用显式和隐式等待。

Locating UI Elements (WebElements)

定位用户界面元素(WebElements)

Locating elements in WebDriver can be done on the WebDriver instance itself or on a WebElement. Each of the language bindings expose a “Find Element” and “Find Elements” method. The first returns a WebElement object otherwise it throws an exception. The latter returns a list of WebElements, it can return an empty list if no DOM elements match the query.

WebDriver定位元素可以在完成本身或WebElement WebDriver实例。每种语言绑定公开“Find Element”和“Find Elements”方法。第一个返回一个WebElement对象否则它将抛出一个异常。后者WebElements返回一个列表,它能返回一个空列表如果没有DOM元素匹配查询。

The “Find” methods take a locator or query object called “By”. “By” strategies are listed below.

“Find”的方法来定位器或查询对象称为“By”。下面列出了“By”策略。

By ID

This is the most efficient and preferred way to locate an element. Common pitfalls that UI developers make is having non-unique id’s on a page or auto-generating the id, both should be avoided. A class on an html element is more appropriate than an auto-generated id.

这是最有效的和首选方法来定位一个元素。常见的陷阱,UI开发人员在一个页面上有非唯一id或自动生成的id,都应该避免.一个类一个html元素上比一个自动生成的id更合适。

Example of how to find an element that looks like this:

如何找到一个元素的例子是这样的:

...
WebElement element = driver.findElement(By.id("coolestWidgetEvah"));

By Class Name

“Class” in this case refers to the attribute on the DOM element. Often in practical use there are many DOM elements with the same class name, thus finding multiple elements becomes the more practical option over finding the first element.

“类”在这种情况下是指DOM元素上的属性.通常在实际使用DOM元素有很多相同的类名,从而发现多个元素变得更实用的选择在找到第一个元素。

Example of how to find an element that looks like this:

Cheddar