温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Selenium官文翻译--(二)Selenium WebDriver(未完待续)

发布时间:2020-05-26 16:32:26 来源:网络 阅读:818 作者:honzhang 栏目:web开发

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文件将会看起来像这样。创建这个文件在您为您的项目创建的文件夹。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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.0 http://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.53.0</version>
            </dependency>
            <dependency>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-htmlunit-driver</artifactId>
                <version>2.20</version>
            </dependency>
        </dependencies>
</project>

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<Boolean>() {
            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:

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

<div id="coolestWidgetEvah">...</div>
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:

<div class="cheese"><span>Cheddar</span></div><div class="cheese"><span>Gouda</span></div>
List<WebElement> cheeses = driver.findElements(By.className("cheese"));
By Tag Name

The DOM Tag Name of the element.

DOM元素的标记名称。

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

<iframe src="..."></iframe>
WebElement frame = driver.findElement(By.tagName("iframe"));
By Name

Find the input element with matching name attribute.找到匹配的名称属性的输入元素。

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

<input name="cheese" type="text"/>
WebElement cheese = driver.findElement(By.name("cheese"));
By Link Text

Find the link element with matching visible text.找到匹配的可见文本链接元素。

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

<a href="http://www.google.com/search?q=cheese">cheese</a>>
WebElement cheese = driver.findElement(By.linkText("cheese"));
By Partial Link Text

Find the link element with partial matching visible text.发现部分匹配可见文本链接元素。

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

<a href="http://www.google.com/search?q=cheese">search for cheese</a>>
WebElement cheese = driver.findElement(By.partialLinkText("cheese"));
By CSS

Like the name implies it is a locator strategy by css. Native browser support is used by default, so please refer to w3c css selectors for a list of generally available css selectors. If a browser does not have native support for css queries, then Sizzle is used. IE 6,7 and FF3.0 currently use Sizzle as the css query engine.

喜欢这个名字意味着它是一个由css定位策略。在默认情况下使用本机浏览器支持,所以请参阅w3c css选择器投入使用css选择器的列表。如果浏览器不支持css的查询,然后使用Sizzle。IE 6、7和FF3.0目前使用Sizzle作为css查询引擎。

Beware that not all browsers were created equal, some css that might work in one version may not work in another.

注意,并不是所有的浏览器都是平等的,一些css,可能工作在一个版本可能不会在另一个工作。

Example of to find the cheese below:

<div id="food"><span class="dairy">milk</span><span class="dairy aged">cheese</span></div>
WebElement cheese = driver.findElement(By.cssSelector("#food span.dairy.aged"));

At a high level, WebDriver uses a browser’s native XPath capabilities wherever possible. On those browsers that don’t have native XPath support, we have provided our own implementation. This can lead to some unexpected behaviour unless you are aware of the differences in the various xpath engines.

在高级别上,WebDriver尽可能使用浏览器的原生XPath功能。在那些没有原生XPath支持的浏览器,我们提供自己的实现。这可能导致一些意想不到的行为,除非你知道各种xpath引擎的差异。

This is a little abstract, so for the following piece of HTML:

<input type="text" name="example" />
<INPUT type="text" name="other" />
List<WebElement> inputs = driver.findElements(By.xpath("//input"));

The following number of matches will be found

Sometimes HTML elements do not need attributes to be explicitly declared because they will default to known values. For example, the “input” tag does not require the “type” attribute because it defaults to “text”. The rule of thumb when using xpath in WebDriver is that you should not expect to be able to match against these implicit attributes.

有时HTML元素不需要显式声明属性,因为他们将默认为已知值。例如,“输入”标签不需要“type”属性,因为它默认为“文本”.WebDriver使用xpath时的经验法则是,你不应该期望能够匹配这些隐式属性。

Using JavaScript

You can execute arbitrary javascript to find an element and as long as you return a DOM Element, it will be automatically converted to a WebElement object.

你可以执行任意javascript来找到一个元素,只要你返回一个DOM元素,它将自动转换为WebElement对象。

Simple example on a page that has jQuery loaded:

jQuery加载的页面上的简单的例子:

WebElement element = (WebElement) ((JavascriptExecutor)driver).executeScript("return $('.cheese')[0]");

Finding all the input elements to the every label on a page:

找到所有的输入元素上的每个标签页:

List<WebElement> labels = driver.findElements(By.tagName("label"));
List<WebElement> inputs = (List<WebElement>) ((JavascriptExecutor)driver).executeScript(
    "var labels = arguments[0], inputs = []; for (var i=0; i < labels.length; i++){" +
    "inputs.push(document.getElementById(labels[i].getAttribute('for'))); } return inputs;", labels);

User Input - Filling In Forms

We’ve already seen how to enter text into a textarea or text field, but what about the other elements? You can “toggle” the state of checkboxes, and you can use “click” to set something like an OPTION tag selected. Dealing with SELECT tags isn’t too bad:

我们已经看到如何在文本区域输入文本或文本字段,但是其他元素呢?你可以“切换”复选框的状态,你可以使用“点击”设置这样一个选项标签选择。处理选择标签并不是太糟了:

WebElement select = driver.findElement(By.tagName("select"));
List<WebElement> allOptions = select.findElements(By.tagName("option"));
for (WebElement option : allOptions) {
    System.out.println(String.format("Value is: %s", option.getAttribute("value")));
    option.click();
}

This will find the first “SELECT” element on the page, and cycle through each of its OPTIONs in turn, printing out their values, and selecting each in turn. As you will notice, this isn’t the most efficient way of dealing with SELECT elements. WebDriver’s support classes include one called “Select”, which provides useful methods for interacting with these.

这将会发现第一个页面上的“选择”元素,并通过它的每个周期选项,打印出它们的值,并选择每个。你会注意到,这并不是最有效的方式处理选择元素。WebDriver类包括一个称为“选择”的支持,这对与这些交互提供了有用的方法。

Select select = new Select(driver.findElement(By.tagName("select")));
select.deselectAll();
select.selectByVisibleText("Edam");

This will deselect all OPTIONs from the first SELECT on the page, and then select the OPTION with the displayed text of “Edam”.

Once you’ve finished filling out the form, you probably want to submit it. One way to do this would be to find the “submit” button and click it:

driver.findElement(By.id("submit")).click();

Alternatively, WebDriver has the convenience method “submit” on every element. If you call this on an element within a form, WebDriver will walk up the DOM until it finds the enclosing form and then calls submit on that. If the element isn’t in a form, then the NoSuchElementException will be thrown:

element.submit();

Moving Between Windows and Frames

窗口和框架之间的移动

Some web applications have many frames or multiple windows. WebDriver supports moving between named windows using the “switchTo” method:

一些web应用程序有很多帧或多个窗口。WebDriver支持移动名为windows之间使用“switchTo”的方法:

driver.switchTo().window("windowName");

All calls to driver will now be interpreted as being directed to the particular window. But how do you know the window’s name? Take a look at the javascript or link that opened it:

所有调用驱动程序现在将被视为被定向到特定的窗口。但是你怎么知道窗口的名字吗?看一下打开它的javascript或链接:

<a href="somewhere.html" target="windowName">Click here to open a new window</a>

Alternatively, you can pass a “window handle” to the “switchTo().window()” method. Knowing this, it’s possible to iterate over every open window like so:

另外,您可以通过“窗口句柄”到“switchTo().window()方法。知道了这一点,可以遍历所有打开的窗口如下所示:

for (String handle : driver.getWindowHandles()) {
    driver.switchTo().window(handle);
}

You can also switch from frame to frame (or into iframes):

您还可以切换从帧到帧(或iframes):

driver.switchTo().frame("frameName");

Popup Dialogs

弹出对话框

Starting with Selenium 2.0 beta 1, there is built in support for handling popup dialog boxes. After you’ve triggered an action that opens a popup, you can access the alert with the following:

从Selenium2.0 beta 1,内建支持处理弹出对话框。触发一个动作之后,打开一个弹出,您可以访问以下警告:

Alert alert = driver.switchTo().alert();

This will return the currently open alert object. With this object you can now accept, dismiss, read its contents or even type into a prompt. This interface works equally well on alerts, confirms, and prompts. Refer to the JavaDocs or RubyDocs for more information.

这将返回当前打开警报对象。这个对象现在可以接受,解雇,阅读其内容,甚至类型到一个提示。这个接口同样适用于警报、确认和提示。参考JavaDocs或RubyDocs获得更多信息。

Navigation: History and Location

导航:历史和位置

Earlier, we covered navigating to a page using the “get” command ( driver.get("http://www.example.com")) As you’ve seen, WebDriver has a number of smaller, task-focused interfaces, and navigation is a useful task. Because loading a page is such a fundamental requirement, the method to do this lives on the main WebDriver interface, but it’s simply a synonym to:

早些时候,我们覆盖导航到一个页面使用“get”命令(driver.get(“http://www.example.com”)如您所见,WebDriver有许多小,以任务为中心的界面,和导航是一个有用的任务。因为加载一个页面是一个基本的要求,

这个方法做这个生活在主WebDriver接口,但它只是一个同义词:

driver.navigate().to("http://www.example.com");

To reiterate: “navigate().to()” and “get()” do exactly the same thing. One’s just a lot easier to type than the other!

重申:navigate().to()” and “get()”做的是相同的事情,只是另一个更容易一点。

The “navigate” interface also exposes the ability to move backwards and forwards in your browser’s history:

“导航”界面也暴露前后移动的能力在你的浏览器的历史:

driver.navigate().forward();
driver.navigate().back();

Please be aware that this functionality depends entirely on the underlying browser. It’s just possible that something unexpected may happen when you call these methods if you’re used to the behaviour of one browser over another.

请注意,此功能完全取决于底层的浏览器。只是可能会发生一些意想不到的事当你调用这些方法如果你使用一个浏览器的行为。

Cookies

Before we leave these next steps, you may be interested in understanding how to use cookies. First of all, you need to be on the domain that the cookie will be valid for. If you are trying to preset cookies before you start interacting with a site and your homepage is large / takes a while to load an alternative is to find a smaller page on the site, typically the 404 page is small (http://example.com/some404page)

在我们离开这些步骤之前,你可能有兴趣了解如何使用cookie。首先,你需要在域的Cookie的有效期,如果你想预定cookie在你开始与网站交互和你的主页是大/需要一段时间来加载另一种方法是在网站上找到一个小页面,通常404页的小

// Go to the correct domain
driver.get("http://www.example.com");

// Now set the cookie. This one's valid for the entire domain
Cookie cookie = new Cookie("key", "value");
driver.manage().addCookie(cookie);

// And now output all the available cookies for the current URL
Set<Cookie> allCookies = driver.manage().getCookies();
for (Cookie loadedCookie : allCookies) {
    System.out.println(String.format("%s -> %s", loadedCookie.getName(), loadedCookie.getValue()));
}

// You can delete cookies in 3 ways
// By name
driver.manage().deleteCookieNamed("CookieName");
// By Cookie
driver.manage().deleteCookie(loadedCookie);
// Or all of them
driver.manage().deleteAllCookies();

Changing the User Agent

修改用户代理 

This is easy with the Firefox Driver:

FirefoxProfile profile = new FirefoxProfile();
profile.addAdditionalPreference("general.useragent.override", "some UA string");
WebDriver driver = new FirefoxDriver(profile);

Drag And Drop

拖拽

Here’s an example of using the Actions class to perform a drag and drop. Native events are required to be enabled.

这里有一个例子使用行为的类进行拖拽。本地事件需要启用。

WebElement element = driver.findElement(By.name("source"));
WebElement target = driver.findElement(By.name("target"));

(new Actions(driver)).dragAndDrop(element, target).perform();

Driver Specifics and Tradeoffs

Driver的细节和权衡

Selenium-WebDriver’s Drivers

WebDriver is the name of the key interface against which tests should be written, but there are several implementations. These include:

WebDriver是测试的关键接口的名字应该写,但有几个实现。这些包括:

HtmlUnit Driver

This is currently the fastest and most lightweight implementation of WebDriver. As the name suggests, this is based on HtmlUnit. HtmlUnit is a java based implementation of a WebBrowser without a GUI. For any language binding (other than java) the Selenium Server is required to use this driver.

这是目前最快的和最轻量级WebDriver的实现。顾名思义,这是基于HtmlUnit。HtmlUnit是一个基于java的浏览器没有GUI的实现。对任何语言绑定(除了java)Selenium服务器需要使用这个驱动程序。

Usage

使用

WebDriver driver = new HtmlUnitDriver();
Pros
  • Fastest implementation of WebDriver最快的实现WebDriver

  • A pure Java solution and so it is platform independent.纯Java的解决方案,所以它是平台独立的。

  • Supports JavaScript支持JS

Cons
  • Emulates other browsers’ JavaScript behaviour (see below)模拟其他浏览器的JavaScript行为(见下文)

JavaScript in the HtmlUnit Driver

None of the popular browsers uses the JavaScript engine used by HtmlUnit (Rhino). If you test JavaScript using HtmlUnit the results may differ significantly from those browsers.

没有一个流行的浏览器使用使用的JavaScript引擎HtmlUnit(犀牛)。如果您使用的测试JavaScript HtmlUnit结果从这些浏览器可能差别很大。

When we say “JavaScript” we actually mean “JavaScript and the DOM”. Although the DOM is defined by the W3C each browser has its own quirks and differences in their implementation of 

当我们说“JavaScript”实际上意味着“JavaScript和DOM”。虽然由W3C DOM定义每个浏览器都有自己的怪癖和差异实施,DOM和JavaScript是如何进行交互的。

the DOM and in how JavaScript interacts with it. HtmlUnit has an impressively complete implementation of the DOM and has good support for using JavaScript, but it is no different from any 

HtmlUnit有一个令人印象深刻的完整实现使用JavaScript DOM和具有良好的支持,但是它对其他浏览器没有任何区别。

other browser: it has its own quirks and differences from both the W3C standard and the DOM implementations of the major browsers, despite its ability to mimic other browsers.

它有自己的怪癖和差异来自W3C标准和主流浏览器的DOM实现,尽管其模仿其他浏览器的能力。

With WebDriver, we had to make a choice; do we enable HtmlUnit’s JavaScript capabilities and run the risk of teams running into problems that only manifest themselves there, or do we leave

WebDriver,我们不得不做出选择;我们让HtmlUnit的JavaScript功能和运行团队遇到的风险问题,只有表现,

 JavaScript disabled, knowing that there are more and more sites that rely on JavaScript? We took the conservative approach, and by default have disabled support when we use HtmlUnit. With 

知道越来越多的依赖于JavaScript的网站吗?我们采取了保守的方法,当我们使用HtmlUnit默认情况下禁用支持

each release of both WebDriver and HtmlUnit, we reassess this decision: we hope to enable JavaScript by default on the HtmlUnit at some point.

WebDriver和HtmlUnit每个版本,我们评估这个决定:我们希望在HtmlUnit默认启用JavaScript。

Enabling JavaScript

If you can’t wait, enabling JavaScript support is very easy:

HtmlUnitDriver driver = new HtmlUnitDriver(true);

This will cause the HtmlUnit Driver to emulate Firefox 3.6’s JavaScript handling by default.

Firefox Driver

Controls the Firefox browser using a Firefox plugin. The Firefox Profile that is used is stripped down from what is installed on the machine to only include the Selenium WebDriver.xpi (plugin). A few settings are also changed by default (see the source to see which ones) Firefox Driver is capable of being run and is tested on Windows, Mac, Linux. Currently on versions 3.6, 10, latest - 1, latest

控制了Firefox浏览器使用Firefox插件。Firefox配置文件使用的是剥夺了从什么是安装在机器上只包括硒WebDriver。xpi(插件)。一些默认设置也改变了Firefox(见源代码看到哪些)驱动程序可以运行和测试的在Windows,Mac,Linux。目前版本3.6,最新10 - 1,最新的。


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI