介绍 WebDriver
Selenium 2.0 最主要的一个新特性就是集成了 WebDriver API。WebDriver 提供更精简的编程几口,以解决 Selenium-RC API 中的一些限制。WebDriver 为那些页面元素可以不通过页面重新加载来更新的动态网页提供了更好的支持。WebDriver 的目标是提供一套精心设计的面向对象的 API 来更好的支持现代高级 web 应用的测试工作。
同 Selenium-RC 相比,WebDriver 如何驱动浏览器的?
Selenium-WebDriver 直接通过浏览器自动化的本地接口来调用浏览器。如何直接调用,和调用的细节取决于你使用什么浏览器。本章后续的内容介绍了每个 “browser driver” 的详细信息。
相比 Selenium-RC ,WebDriver 确实非常不一样。Selenium-RC 在所有支持的浏览器中工作原理是一样的。它将 JavaScript 在浏览器加载的时候注入浏览器,然后使用这些 JavaScript 驱动 AUT 运行 WebDriver 使用的是不同的技术,再一次强调,它是直接调用浏览器自动化的本地接口。
WebDriver 和 Selenium-Server
你可能需要,也可能不需要 Selenium Server,取决于你打算如何使用 Selenium-WebDriver。如果你仅仅需要使用 WebDriver API,那就不需要 Selenium-Server。如果你所有的测试和浏览器都在一台机器上,那么你仅需要 WebDriver API。WebDriver 将直接操作浏览器。
在有些情况下,你需要使用 Selenium-Server 来配合 Selenium-WebDriver 工作,例如:
●你使用 Selenium-Grid 来分发你的测试给多个机器或者虚拟机。
●你希望连接一台远程的机器来测试一个特定的浏览器。
设置一个 Selenium-WebDriver 项目
安装 Selenium 意味着当你创建一个项目,你可以在项目中使用 Selenium 开发。具体怎么做取决于你的项目语言和开发环境。
Java
创建一个 Selenium 2.0 Java 项目最简单的方式是使用 maven。Maven 将下载 Java 绑定(Selenium 2.0 的 Java 客户端)和其所有依赖,并且通过 pom.xml(mvn项目配置)为你创建项目。当你完成这些操作的时候,你可以将 maven 项目导入到你偏好的 IDE 中,例如 IntelliJ IDEA 或 Eclipse。
首先,创建一个用于放置项目的文件夹。然后,在这个文件夹中创建 pom.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.28.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.1</version> <exclusions> <exclusion> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-remote-driver</artifactId> </exclusion> </exclusions> </dependency> </dependencies> </dependencyManagement> </project> |
确保你指定了最新版本。在编写本文档时,范例代码中的即为最新版本。但是,稍后 Selenium 2.0 还会不断有新发布。检查Maven 下载页面 中的最新版本,并修改上述文件中依赖的版本。
命令行进入本目录,运行如下命令:
mvn clean install
该命令会下载 Selenium 和其所有依赖,并添加到这个项目中。
最后,将项目导入到你的 IDE。对于不太熟悉 IDE 的用户,我们提供了附件来说明相关内容。
[
从 Selenium 1.0 迁移
对于那些已经使用 Selenium 1.0 编写测试套件的用户,我们提供了一些迁移的建议。Selenium 2.0 的核心工程师 Simon Stewart 写了一篇关于从 Selenium 1.0 迁移的文章,包含在本文的附件中。
Migrating From Selenium RC to Selenium WebDriver
[
实例介绍 Selenium-WebDriver API
WebDriver 是一个进行 web 应用测试自动化的工具,主要用于验证它们的行为是否符合期望。WebDriver 的目标是提供一套易于掌握的 API,且比 Selenium-RC (1.0) 更易于使用,页能是你的测试更具可读性和维护性。它没有同任何特定的测试框架进行绑定,所以可以在单元测试或者是 main 方法中工作良好。本小节介绍 WebDriver API,并且帮助你熟悉它。如果你还没有任何 WebDriver 项目,请按照上一小节的介绍新建一个。
建好项目后,你可以发现 WebDriver 和任何普通的库一样:它是自包含的,通常不需要进行任何额外的处理或者运行安装。这一点和 Selenium-RC 的代理服务器是不一样的。
注意:使用 Chrome Driver、 Opera Driver、Android Driver 和 iPhone Driver 是需要一些额外操作的。
我们准备了一个简单的例子:在 Google 上搜索 “Cheese”,然偶输出搜索结果页的页面标题到 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) { // 创建了一个 Firefox driver 的实例 // 注意,其余的代码依赖于接口而非实例 WebDriver driver = new FirefoxDriver(); // 使用它访问 Google driver.get("http://www.google.com"); // 同样的事情也可以通过以下代码完成 // driver.navigate().to("http://www.google.com"); // 找到搜索输入框 WebElement element = driver.findElement(By.name("q")); // 输入要查找的词 element.sendKeys("Cheese!"); // 提交表单 element.submit(); // 检查页面标题 System.out.println("Page title is: " + driver.getTitle()); // Google 搜索结果由 JavaScript 动态渲染 // 等待页面加载完毕,超时时间设为10秒 (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { return d.getTitle().toLowerCase().startsWith("cheese!"); } }); //应该能看到: "cheese! - Google Search" System.out.println("Page title is: " + driver.getTitle()); //关闭浏览器 driver.quit(); } } |
在接下来的章节中,你将学习到更多使用 WebDriver 的知识,例如根据浏览器历史记录前进和后退,如何测试 frames 和 windows。针对这些点我们提供了全面的讨论和范例。
Selenium-WebDriver API 和操作
获取一个页面
访问一个页面或许是使用 WebDriver 时你第一件想要做的事情。最常见的是调用 “get” 方法:
driver.get("http://www.google.com");
包括操作系统和浏览器在内的多种因素影响,WebDriver 可能会也可能不会等待页面加载。在某些情况下,WebDriver可能在页面加载完毕前就返回控制了,甚至是开始加载之前。为了确保健壮性,你需要使用 Explicit and Implicit Waits 等到页面元素可用。
查找 UI 元素(web 元素)
WebDriver 实例可以查找 UI 元素。每种语言实现都暴露了 “查找单个元素” 和 “查找所有元素” 的方法。第一个方法如果找到则返回该元素,如果没找到则抛出异常。第二种如果找到则返回一个包含所有元素的列表,如果没找到则返回一个空数组。
“查找” 方法使用了一个定位器或者一个叫 “By” 的查询对象。“By” 支持的元素查找策略如下:
By id
这是最高效也是首选的方法用于查找一个元素。UI 开发人员常犯的错误是,要么没有指定 id,要么自动生成随机 id,这两种情况都应避免。及时是使用 class 也比使用自动生成随机 id 要好的多。
HTML:
<div id="coolestWidgetEvah">...</div>
Java:
WebElement element = driver.findElement(By.id("coolestWidgetEvah"));
By Class Name
"class" 是 DOM 元素上的一个属性。在实践中,通常是多个 DOM 元素有同样的 class 名,所以通常用它来查找多个元素。
HTML: <div class="cheese"><span>Cheddar</span></div><div class="cheese"><span>Gouda</span></div> |
Java:
List<WebElement> cheeses = driver.findElements(By.className("cheese"));
By Tag Name
根据元素标签名查找。
HTML:
<iframe src="..."></iframe>
Java:
WebElement frame = driver.findElement(By.tagName("iframe"));
By Name
查找 name 属性匹配的表单元素。
HTML:
<input name="cheese" type="text"/>
Java:
WebElement cheese = driver.findElement(By.name("cheese"));
By Link Text
查找链接文字匹配的链接元素。
HTML:
<a href="http://www.google.com/search?q=cheese">cheese</a>>
Java:
WebElement cheese = driver.findElement(By.linkText("cheese"));
By Partial Link Text
查找链接文字部分匹配的链接元素。
HTML:
<a href="http://www.google.com/search?q=cheese">search for cheese</a>>
Java:
WebElement cheese = driver.findElement(By.partialLinkText("cheese"));
By CSS
正如名字所表明的,它通过 css 来定位元素。默认使用浏览器本地支持的选择器,可参考 w3c 的 css 选择器。如果浏览器默认不支持 css 查询,则使用 Sizzle。ie6、7 和 ff3.0 都使用了 Sizzle。
注意使用 css 选择器不能保证在所有浏览器里都表现一样,有些在某些浏览器里工作良好,在另一些浏览器里可能无法工作。
HTML:
<div id="food"><span class="dairy">milk</span><span class="dairy aged">cheese</span></div>
Java:
WebElement cheese = driver.findElement(By.cssSelector("#food span.dairy.aged"));
By XPATH
此处略过不译