在多个浏览器中进行 Web 应用程序的端到端功能测试

Selenium 是一款有名的 Web 应用程序测试框架,用于进行功能测试。新版本 Selenium 2 结合了 Selenium 1 和 WebDriver(Selenium 的并行项目)中的最佳特性。在本文中,我们将介绍如何轻松地从 Selenium 1 过渡到 Selenium 2,并用一些示例介绍如何使用 Selenium 2,如何进行远程测试,以及如何将书面测试从 Selenium 1 迁移到 Selenium 2 中。

简介

Selenium 是用于测试 Web 应用程序用户界面 (UI) 的常用框架。它是一款用于运行端到端功能测试的超强工具。您可以使用多个编程语言编写测试,并且 Selenium 能够在一个或多个浏览器中执行这些测试。

Selenium(以下简称为 Selenium 1)并不是能够在浏览器中自动化功能测试的惟一工具。由 Simon Stewart(来自 Google)创建的 WebDriver 是一个具有类似目标的项目。要控制浏览器,需要依赖采用本机支持的独立客户端。WebDriver 仅提供 Java 绑定,并不能支持 Selenium 1 所能支持的那么多浏览器。

Selenium 1 + WebDriver = Selenium 2

Selenium 1 和 WebDriver 合并成一款性能更佳的产品 Selenium 2(或 Selenium WebDriver),该款产品发行于 2011 年。Selenium 2 具有来自 WebDriver 的清晰面向对象 API,并能以最佳的方式与浏览器进行交互。Selenium 2 不使用 JavaScript 沙盒,它支持多种浏览器和多语言绑定。在撰写本文时,Selenium 2 为下列程序提供驱动程序:

  • Mozilla Firefox
  • Google Chrome
  • Microsoft Internet Explorer
  • Opera
  • Apple iPhone
  • Android browsers

借助 Selenium 2,您可使用 Java、C#、Ruby、和 Python 编写测试。Selenium 2 还提供基于 HtmlUnit 的无外设驱动,是用于测试 Web 应用程序的 Java 框架。HtmlUnit 运行速度特别快,但它不是一个真正与真实浏览器相关联的驱动。

目前,Selenium 2 仍处于开发阶段,还有些细节问题正在处理。当前版本为 2.9。针对 Safari 和 Blackberry 的驱动将会在近期集成到产品中。

在本文当中,我们将学习如何利用 Selenium 2 来测试 Web 应用程序。示例展示了如何远程实现测试。我们还将学习如何将编写好的测试从 Selenium 1 转移到 Selenium 2 中。

下载本文中所使用的源代码。

 

Selenium 2 入门

在本节,我们将学习如何将 Selenium 2 框架用于 Web 应用程序的一个相对简单的测试中。开发环境采用 Java 语言。您还需要包括 Java 绑定的 selenium-java-<version>.jar(参阅 参考资料并下载)。在一个 Maven 项目中,您只需要在 pom.xml 中包含正确的依赖性,如 清单 1所示。

清单 1. Selenium-java 依赖性
 <dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.9.0</version>
</dependency>

现在,可以开始编写测试。WebDriver API 中的主要组件是 WebDriver接口。这一公共接口在每个有效浏览器中都能实现。例如,类FirefoxDriver将用于控制 Mozilla Firefox。清单 2展示了如何在测试当中实例化一个特定的实现。您可以采用最符合您需求的测试框架,比如 JUnit 或者 TestNG。

清单 2. 实例化的 FirefoxDriver
 public class Selenium2Example1Test {
@Test
public void test() {
// Instantiate a webDriver implementation
WebDriver webdriver = new FirefoxDriver();
}
}

要加载页面进行测试,可利用 get()方法。在 清单 3中,GitHub 主 (https://github.com) 被加载到先前创建的 Firefox 实例中。

清单 3. 在测试下加载页面
 WebDriver webdriver = new FirefoxDriver();
webdriver.get(https://github.com);

您可在刚加载的页面上添加断言。假如您想要测试页面标题是否等于 "GitHub - Social Coding",如下 清单 4所示。WebDriver 提供getTitle()方法;您可利用所选的测试框架来生成断言。

清单 4. 页面标题的断言
 Assert.assertEquals("GitHub - Social Coding", webdriver.getTitle());

完成测试以后,最好利用 quit()方法来终止 WebDriver 实例,如下 清单 5所示。

清单 5. 终止 WebDriver 实例
 webdriver.quit();

FirefoxDriver只是有效的 WebDriver 实现之一。您可以利用 ChromeDrive 在 Chrome 内部运行测试,来执行相同的测试。清单 6展示了利用 ChromeDriver 的完整示例。

清单 6. ChromeDriver 样例
 public class Selenium2Example2Test {
@Test
public void test() {
System.setProperty("webdriver.chrome.driver",
"src/main/resources/drivers/chrome/chromedriver-mac"); // Instantiate a webDriver implementation
WebDriver webdriver = new ChromeDriver(); webdriver.get(https://github.com); Assert.assertEquals("GitHub - Social Coding", webdriver.getTitle());
}
}

在实例化 ChromeDriver 之前,需要正确设置 "webdriver.chrome.driver"系统属性。该属性指出您操作系统的 ChromeDriver 文件位置(参阅 参考资料并下载)。清单 6中的示例使用了针对 Mac 的版本;同样可用针对 Windows 和 Linux 的版本。

要在 Internet Explorer 中执行相同的测试,需要用到 InterentExplorerDriver类的实例,如 清单 7所示。

清单 7. InternetExplorerDriver实例化
 WebDriver webdriver = new InternetExplorerDriver();

当采用 InterenetExplorerDriver时,可能会遇到一个安全问题提示:"Protected Mode must be set to the same value (enabled or disabled) for all zones"。想要解决这一问题,需要设置特定的功能,如 清单 8所示。

清单 8. 为 Internet Explorer 设置安全性功能
 DesiredCapabilities capability=DesiredCapabilities.internetExplorer();
capability.setCapability(
InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_
IGNORING_SECURITY_DOMAINS, true);
WebDriver webdriver = new InternetExplorerDriver(capability);

要在 Opera 中执行测试,需要实例化 OperaDriver类,这是由 Opera 直接开发的。记得要将包含驱动的 JAR 包括到项目中。如果您使用 Maven,则仅需增加 清单 9中的依赖性。

清单 9. OperaDriver依赖性
 <dependency>
<groupId>com.opera</groupId>
<artifactId>operadriver</artifactId>
<version>0.7.3</version>
</dependency>

需要额外配置以实现在 iPhone 或者 Android 浏览器模拟器中运行测试。

 

使用 Selenium 2 进行测试

使用 Selenium 2 可以构建比前节内容更加复杂的测试。在本节中,您将测试到,GitHub 主页的顶部导航共有 5 个列表项:Signup and Pricing、Explore GitHub、Features、Blog 以及 Login。图 1展示了 Github 主页。

图 1. Github 主页

查看顶部导航的 HTML 代码,如 清单 10所示。

清单 10. 顶部导航的 HTML 代码
 <html>
<head>
...
</head>
<body class="logged_out env-production">
<div id="main">
<div id="header" class="true">
...
<div class="topsearch">
<ul class="nav logged_out">
<li class="pricing">
<a href="https://github.com/plans">Signup and Pricing</a>
</li>
<li class="explore">
<a href="https://github.com/explore">Explore GitHub</a>
</li>
<li class="features">
<a href="https://github.com/features">Features</a>
</li>
<li class="blog">
<a href="https://github.com/blog">Blog</a>
</li>
<li class="login">
<a href="https://github.com/login">Login</a>
</li>
</ul>
</div>
...
</div>
...
</div>
...
</body>
</html>

可利用 WebDriver API(从 HTML 代码内部)检索您需要测试的元素。findElement()和 findElements()方法会返回公共接口WebElement的一个实例或者一组实例。WebElement可以以一种清晰的、面向对象的方式应用于页面中的所有元素。在 API 中可以使用许多不同的策略来定位 UI 元素。这些策略由传递至 findElement()和 findElements()方法的不同类型参数所代表。清单 11展示了应用抽象类By的各个方法来实现不同策略的示例。

清单 11. 使用 findElement()方法
 WebElement element1 = webdriver.findElement(By.id("header"));
WebElement element2 = webdriver.findElement(By.name("name"));
WebElement element3 = webdriver.findElement(By.tagName("a"));
WebElement element4 = webdriver.findElement(By.xpath("//a[@title='logo']"));
WebElement element5 = webdriver.findElement(By.cssSelector(".feautures"));
WebElement element6 = webdriver.findElement(By.linkText("Blog"));
WebElement element7 = webdriver.findElement(By.partialLinkText("Ruby"));
WebElement element8 = webdriver.findElement(By.className("login"));

使用 清单 11中的一个策略,就可以开始编写测试来检索第一个元素:UL标记中 LI标记带有 nav类。清单 12使用了 Xpath (By.xpath())。

清单 12. Xpath
 List<WebElement> webElements = webdriver.findElements(By
.xpath("//ul[@class='nav logged_out']/li"));

清单 13使用 CSS 选择器 (By.cssSelector()) 来检索 LI标记。

清单 13. CSS 选择器
 List<WebElement> webElements = webdriver.findElements(By
.cssSelector("ul.nav li"));

此时,可在所检索的项数量上生成断言,如 清单 14所示。

清单 14. 项数量上的断言
 Assert.assertEquals(5, webElements.size());

前面的步骤验证了 LI标记数量等于 5。

下一步是检索每个 LI标记中的每个锚点(A标记)。 清单 15展示了如何在第一个 LI中获取锚点。此用例使用了 tagName (By.tagName()) 策略。

清单 15. 检索第一个 LI 标记中的 A锚点
 WebElement anchor1 = webElements.get(0).findElement(By.tagName("a"));

您可以使用类似的方法收集到所有的 5 个锚点,如 清单 16所示。

清单 16. 检索 LI标记中的所有锚点
 WebElement anchor1 = webElements.get(0).findElement(By.tagName("a"));
WebElement anchor2 = webElements.get(1).findElement(By.tagName("a"));
WebElement anchor3 = webElements.get(2).findElement(By.tagName("a"));
WebElement anchor4 = webElements.get(3).findElement(By.tagName("a"));
WebElement anchor5 = webElements.get(4).findElement(By.tagName("a"));

在这一阶段,您可以验证,锚点内的文本是否与所期望的字符串一致。要检索标记内的文本,WebDriver 提供了 getText()方法。清单 17展示了完整的测试方法,以及测试底部的断言。

清单 17. 完成测试
 @Test
public void test() {
WebDriver webdriver = new FirefoxDriver();
webdriver.get("https://github.com");
List<WebElement> webElements = webdriver.findElements(By
.xpath("//ul[@class='nav logged_out']/li"));
Assert.assertEquals(5, webElements.size()); // Retrieve the anchors
WebElement anchor1 = webElements.get(0).findElement(By.tagName("a"));
WebElement anchor2 = webElements.get(1).findElement(By.tagName("a"));
WebElement anchor3 = webElements.get(2).findElement(By.tagName("a"));
WebElement anchor4 = webElements.get(3).findElement(By.tagName("a"));
WebElement anchor5 = webElements.get(4).findElement(By.tagName("a")); // Assertions
Assert.assertEquals("Signup and Pricing", anchor1.getText());
Assert.assertEquals("Explore GitHub", anchor2.getText());
Assert.assertEquals("Features", anchor3.getText());
Assert.assertEquals("Blog", anchor4.getText());
Assert.assertEquals("Login", anchor5.getText()); webdriver.quit(); }

启动这一测试后,将会打开一个新的 Firefox 窗口,该窗口将会保持开启,直到执行完所有的断言。

 

使用 Selenium Grid 2 进行远程测试

可在 Selenium 2 中进行本地或远程测试。要实现远程运行,该测试需要使用名为 RemoteWebDriver的 WebDriver接口的特定实现。您可以指定浏览器采用 DesiredCapabilities类运行。清单 18显示了相关示例。

清单 18. RemoteWebDriver和 DesiredCapabilities
 DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setBrowserName("firefox");
capabilities.setVersion("7");
capabilities.setPlatform("MAC");
WebDriver webdriver = new RemoteWebDriver(capabilities);

借助 DesiredCapabilities类,您可以指定浏览器的名称、平台和浏览器版本。您还可指定浏览器支持的其他功能。

如果想要远程执行结构化测试,并运行多个浏览器(并且可能是不同的虚拟机),Selenium Grid 提供了很好的解决方案。

Selenium Grid 2 提供了基础设施,其中每个节点代表了不同浏览器将自身注册到 hub 当中。单数的测试将会调用一个 hub,它负责将每个请求分配到正确的浏览器。Hub 和节点可以运行在不同的虚拟机当中。

要实现远程测试,则需要在您将要使用的每台机器上下载 selenium-server-standalone-<version>.jar。要在机器上安装 hub,转到您下载 JAR 所在的文件夹,并运行 清单 19中的命令。

清单 19. 启动 hub
 java -jar selenium-server-standalone-2.9.0.jar ?role hub

您可在 http://localhost:4444/grid/console 访问 Grid 2 控制台,其中会列出所有可用的节点。要注册一个节点,仅需运行一个命令,如 清单 20所示。

清单 20. 在 hub 中注册的节点
 java -jar selenium-server-standalone-2.9.0.jar
-role webdriver ?hub http://localhost:4444/grid/register -port 5556

在默认情况下,清单 20中的命令注册了 7 个浏览器:5 个 Firefox 实例、1 个 Chrome 实例以及一个 Internet Explorer 实例。您可以在特定的端口上定位一个特定浏览器,如 清单 21所示。

清单 21. 在 hub 上注册的 Firefox 7 实例
 java -jar selenium-server-standalone-2.9.0.jar -role webdriver
-hub http://localhost:4444/grid/register -port 5556 -browser
browserName=chrome,version=14,platform=MAC

注册完一些浏览器之后,Selenium Grid 2 控制台会变成如 图 2显示的样子。

图 2. Selenium Grid 2 控制台视图

要使用网格,则需要在测试用例中指定 hub 的 URL 和所要控制的浏览器。清单 22展示了 RemoteWebDriver类构造函数接受了 hub 的 URL 以及定义特定浏览器的 DesiredCapabilities实例。

清单 22. RemoteWebDriver实例化
 DesiredCapabilities capability = new DesiredCapabilities();
capability.setBrowserName("chrome");
capability.setVersion("14");
capability.setPlatform(Platform.MAC);
WebDriver webdriver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),
capability);

在本用例中,hub 将会启动与 Chrome 版本 14(之前已在 清单 21中注册的)相关联的节点。

Selenium Grid 2 还向后兼容 Selenium 1。您可以在 hub 中注册 Selenium 1 RC 节点(这是 hub 中 Selenium 1 基础设施的一部分),如 清单 23所示。

清单 23. Selenium 1 RC 节点注册
 java ?jar selenium-server-standalone-2.9.0.jar
-role rc ?hub http://localhost:4444/grid/register -port 5557
 

将测试从 Selenium 1 迁移到 Selenium 2

如果需要将编写好的测试从 Selenium 1 转移到 Selenium 2,那么这种转移会相当平滑。Selenium 1 API 保留在新的 API 中,使得 Selenium 2 能够完全向后兼容。

将测试从 Selenium 1 转移到 Selenium 2 非常简单,这要归功于 WebDriverBackedSelenium类。它将一个 WebDriver的实例和 URL 作为测试的参数,并且返回一个 Selenium 实例。清单 24展示了与 清单 16相同示例,但是使用集成到 Selenium 2 中的 Selenium 1 API。

清单 24. 将 Selenium 1 集成到 Selenium 2
 @Test
public void test() {
String url = "https://github.com";
WebDriver webdriver = new FirefoxDriver();
webdriver.get(url);
Selenium selenium = new WebDriverBackedSelenium(webdriver, url);
selenium.open(url); // Get the number of LIs
Number lis = selenium.getXpathCount("//ul[@class='nav logged_out']/li"); Assert.assertEquals(5, lis.intValue()); // Retrieve the text inside the anchors
String anchor1Text = selenium.getText("//ul[@class='nav logged_out']/li[1]/a");
String anchor2Text = selenium.getText("//ul[@class='nav logged_out']/li[2]/a");
String anchor3Text = selenium.getText("//ul[@class='nav logged_out']/li[3]/a");
String anchor4Text = selenium.getText("//ul[@class='nav logged_out']/li[4]/a");
String anchor5Text = selenium.getText("//ul[@class='nav logged_out']/li[5]/a"); Assert.assertEquals("Signup and Pricing", anchor1Text);
Assert.assertEquals("Explore GitHub", anchor2Text);
Assert.assertEquals("Features", anchor3Text);
Assert.assertEquals("Blog", anchor4Text);
Assert.assertEquals("Login", anchor5Text); webdriver.quit();
}

Selenium 2 更加关注开发者。它具有比 Selenium 1 更清晰的 API,正如 getText()和 getXpathCount()方法签名所证明的一样。Selenium 2 API 还具有更好的面向对象的特点。例如,不允许您处理 UI 元素对象,仅允许处理字符串。

 

结束语

Selenium 2 标志着浏览器内部自动测试的一个演变过程。它继承了 Selenium 1 与 WebDriver 最优秀的部分,并提供了与多个浏览器的紧密集成。新的 API 更符合开发人员的要求,提供了面向对象的方法,并提供了不同的模型用于编写测试。此外,Selenium 2 还:

  • 克服与相同原始策略相关联的限制。
  • 为弹出窗口提供更好的支持。
  • 有效控制本机的键盘与鼠标的交互。

Selenium Grid 的新版本(包括在 Selenium 2 中)使得远程加载测试更加方便。Selenium 2 能够向后兼容 Selenium 1,因此升级到新版本轻而易举。Selenium 2 能够帮助确保应用程序按需工作。

Selenium 2 入门的更多相关文章

  1. 元素(WebElement)-----Selenium快速入门(三)

    上一篇<元素定位-----Selenium快速入门(二)>说了,如何定位元素,本篇说说找到的元素(WebElement)该怎么用. WebElement常用方法:  返回值  方法名  说 ...

  2. python selenium webdriver入门基本操作

    python selenium webdriver入门基本操作 未经作者允许,禁止转载! from selenium import webdriver import time driver=webdr ...

  3. 隐式等待-----Selenium快速入门(九)

    有时候,网页未加载完成,或加载失败,但是我们后续的代码就已经开始查找页面上的元素了,这通常将导致查找元素失败.在本系列Selenium窗口切换-----Selenium快速入门(六)中,我们就已经出现 ...

  4. selenium webdriver入门

    写在前面:最近在研究UI自动化测试的过程中,发现公司里通常用的是AutomanX框架,而这个框架实际上是基于selenium webdriver框架的,所以在编写测试用例时,很多语法都是直接使用sel ...

  5. Selenium | 基础入门 | 截屏并保存于本地

    可先参考   Selenium | 基础入门 | 利用Xpath寻找用户框 核心代码: //截屏操作 File srcFile = ((TakesScreenshot)driver).getScree ...

  6. Python+Selenium基础入门及实践

    Python+Selenium基础入门及实践 32018.08.29 11:21:52字数 3220阅读 23422 一.Selenium+Python环境搭建及配置 1.1 selenium 介绍 ...

  7. 小白学 Python 爬虫(28):自动化测试框架 Selenium 从入门到放弃(下)

    人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...

  8. Selenium自动化-入门1

    起初写博客是为了妹子学习,现在发现忽略了最基础的Selenium教程,所以:从本博客开始编辑 Selenium 入门知识.(学好Java) Selenium 入门 1:(学好Java) 录制回放,简单 ...

  9. selenium爬虫入门(selenium+Java+chrome)

    selenium是一个开源的测试化框架,可以直接在浏览器中运行,就像用户直接操作浏览器一样,十分方便.它支持主流的浏览器:chrome,Firefox,IE等,同时它可以使用Java,python,J ...

随机推荐

  1. Python中 "+=" 使用时的注意事项

    代码1: >>> l1=range(3) >>> l2=l1 >>> l2+=[4] >>> l1 [0, 1, 2, 4] & ...

  2. 05-String动手动脑问题及课后实验性问题总结

    一.请运行以下实例代码StringPool.java,查看其输出结果.如何解释这样的输出结果?从中你能总结出什么? (1)在Java中,内容相同的字符常量("Hello")只保存一 ...

  3. 实现远程FTP特定时间轨道号MODIS数据的搜索

    private ArrayList alst = new System.Collections.ArrayList();//建立ArrayList对象 int strLength = 0; strin ...

  4. 安装pear

    下载go-pear.phar php go-pear.phar pear install Mail-1.2.0 pear list

  5. 十天学会DIV+CSS(DIV布局)

    一列布局: 一列固定宽度.一列固定宽度居中.一列自适应宽度.一列自适应宽度居中 一列固定宽度 <head> <style type="text/css"> ...

  6. asp.net js获取控件ID

    ClientID是由ASP.Net生成的服务器控件得客户端标识符,当这个控件生成到客户端页面的时候,在客户端代码访问该控件时就需要通过ClientID来访问. 以文本框为例: 一.未使用母版页 js可 ...

  7. Windows Store App JavaScript 开发:小球运动示例

    通过前面内容的学习,相信读者已经对开发基于JavaScript的Windows应用商店应用有了一定的了解,本小节通过一个小球运动的示例来介绍如何新建一个JavaScript的Windows应用商店项目 ...

  8. 全端开发必备!10个最好的 Node.js MVC 框架

      Node.js 是最流行的 JavaScript 服务端平台,它允许建立可扩展的 Web 应用程序.Node.js 包含不同类型的框架,如 MVC 框架.全栈框架.REST API  以及大量的服 ...

  9. blade and soul zone overview

    The world of Blade and Soul, is a vast extension of land containing two continents (the Southern Con ...

  10. Android App的签名打包三步骤

    1.签名的意义 为了保证每个应用程序开发商合法ID,防止部分开放商可能通过使用相同的Package Name来混淆替换已经安装的程序,我们需要对我们发布的APK文件进行唯一签名,保证我们每次发布的版本 ...