Selenium 2 入门

在多个浏览器中进行 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 能够帮助确保应用程序按需工作。

时间: 2024-08-26 02:22:13

Selenium 2 入门的相关文章

selenium从入门到应用 - 7,testNG的DataProvider

本系列所有代码 https://github.com/zhangting85/simpleWebtest 本文将介绍一个Java+TestNG+Maven+Selenium的web自动化测试脚本环境下testNG的Dataprovider的使用和数据驱动的自动化测试的实现,并提供全部代码. 首先,DataProvider是什么? 这是testNG提供的一个做数据驱动的自动化测试时用到的标签.详见testNG官网文档. 很多人会在使用testNG的时候把测试数据文件独立出来,有的人会自己去写读文件

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

人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Linux基础入门 小白学 Python 爬虫(4):前置准备(三)Docker基础入门 小白学 Python 爬虫(5):前置准备(四)数据库基础 小白学 Python 爬虫(6):前置准备(五)爬虫框架的安装 小白学 Python 爬虫(7):HTTP 基础 小白学 Python 爬虫(8):网页基

小白学 Python 爬虫(27):自动化测试框架 Selenium 从入门到放弃(上)

人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Linux基础入门 小白学 Python 爬虫(4):前置准备(三)Docker基础入门 小白学 Python 爬虫(5):前置准备(四)数据库基础 小白学 Python 爬虫(6):前置准备(五)爬虫框架的安装 小白学 Python 爬虫(7):HTTP 基础 小白学 Python 爬虫(8):网页基

python+selenium—webdriver入门(一)

一.浏览器最大化 二.设置浏览器分辨率大小 三.打印页面title 四.打印URL 五.控制浏览器前进或后退 #!/usr/bin/env python#-*- coding:utf-8 -*- from selenium import webdriverimport time browser = webdriver.Firefox() #浏览器最大化 browser.maximize_window() #设置浏览器分辨率大小browser.set_window_size(800,600) #访

python selenium webdriver入门基本操作

未经作者允许,禁止转载! from selenium import webdriver import time driver=webdriver.Chrome() driver.get('http://www.baidu.com')#进入百度首页 driver.maximize_window()#窗口最大化 time.sleep(1) driver.set_window_size(480,600)#窗口大小变化 time.sleep(1) driver.maximize_window() tim

python+selenium—webdriver入门(二)

本文中主要介绍webdriver常见的对象定位方法: 一.对象定位的目的 二.常见的对象定位方法 一.对象定位的目的: 1.操作对象 2.获得对象的属性,如:对象的class属性.name属性等 3.获得对象的text 4.获取对象的数量 二.常见的对象定位方法: 1.find element方法: 1.id 2.name 3.class name 4.tag name 5.css定位 6.xpath定位 7.link text 8.partial link text 1 # !/usr/bin

Python自动化测试之selenium从入门到精通

1. 安装selenium 首先确保python安装成功,输入python -V 这里使用pip安装selenium,详情如图所示:

Selenium + Java 入门之环境搭建

最近在研究Java+Selenium的自动化测试,网上的资料比较多,自己测试实践后,整理出来一套相对比较完善的环境资料,因为网上很多下载实践的过程中,发现出现了很多不匹配的问题,什么jdk和eclipse不匹配了,Selenium和Fire Fox不匹配之类的. 一.开发环境: 1.JDK1.7:http://pan.baidu.com/s/1o7OaqvC     nybk 2.Eclipse 32位: 此文件由于分享链接无法使用,就仅在此贴图作为自己的随笔记录了(如有需要的小伙伴可以加我好友

Selenium框架切换-----Selenium快速入门(七)

上一篇说了窗口的切换,本篇说说框架的切换. 切换框架:是指切换html中的iframe标签元素或者frame标签元素,注意,并不包括frameset 以下是常用的方法: 方法 说明 WebDriver switchTo().frame(int index) 通过索引切换到指定的框架,若框架不存在,则抛出异常 NoSuchFrameException WebDriver switchTo().frame(String nameOrId) 通过框架的name或者id切换到指定的框架,若框架不存在,则