Skip to content

Instantly share code, notes, and snippets.

@ZhouMeichen
Last active November 5, 2017 09:27
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZhouMeichen/7593403 to your computer and use it in GitHub Desktop.
Save ZhouMeichen/7593403 to your computer and use it in GitHub Desktop.
整理熟悉的测试框架(Selenium、Watir、Watir WebDriver)

开源自动化测试框架

自动化测试工具有很多,包括开源的和商业的,以下介绍目前比较流行的开源自动化测试工具。

1. Selenium

Selenium是一个用于Web应用程序自动化测试的开源测试框架。Selenium是个庞大的家族,包含Selenium IDE、Selenium Server、Selenium Core、Selenium WebDriver、Selenium RC(Remote Control)、Selenium Grid、Selenium on Rails等。

Selenium 1.0(RC)

简介

Selenium RC是Selenium项目的一部分,它支持多种不同的操作系统,能够启动不同的浏览器实例,同时也支持多种语言编程,包括Java、C sharp、Python、Ruby、PHP、Perl等。

Selenium RC是基于JavaScript实现的,Client Lib将selenese命令传递给Selenium服务器,接着,服务器使用Selenium-Core的JavaScript命令传递给浏览器,浏览器再用自带的JavaScript解释器执行selenese命令。所以支持JavaScript的浏览器都能很好地支持Selenium RC。

工作原理

Selenium RC最初设计要求待测应用和Selenium Core、测试脚本部署到同一台服务器上,以避免触犯浏览器的同源策略(相同协议、端口和域名)和JS沙箱策略,但在实际运用中,这种要求是不可行的。为了解决这个问题,HTTP代理被引入,Selenium服务器位于浏览器和待测应用之间以代理的形式存在,通过修改待测应用的源信息,浏览器会认为待测应用和Selenium Core来自同一个源——代理服务器。

  • 测试脚本通过Client Lib的接口向Selenium服务器发送HTTP请求,要求和服务器建立连接

  • Selenium服务器的Launcher启动浏览器,把Selenium Core加载到浏览器页面中,并把浏览器的代理设置为Selenium服务器的HTTP Proxy

  • 测试脚本通过Client Lib的接口向Selenium服务器发送HTTP请求,Selenium服务器对请求进行解析,然后通过HTTP Proxy发送JS命令通知Selenium Core执行操作浏览器的动作

  • Selenium Core接收到指令后,执行操作(可能引发新的页面请求)

  • 浏览器收到新的页面请求信息,发送HTTP请求,请求新的Web页面,此时Selenium服务器会接收到所有由它启动的浏览器发送的请求

  • Selenium服务器接收到浏览器发送的HTTP请求后,重组HTTP请求,获取对应的Web页面

  • Selenium服务器的HTTP Proxy把接收的Web页面返回到浏览器

下载和安装Selenium RC

使用Selenium RC之前,必须下载并安装Selenium服务器(目前可用于Selenium1.0和2.0),可从SeleniumHQ(http://www.seleniumhq.org/download) 获得。Selenium服务器是一个JAR文件,需使用JRE运行。

下载完成后进行解压缩,可以通过以下命令来启动Selenium服务器:

java -jar selenium-server-standalone-x.x.x

下载SeleniumRC,以下用Java和FireFox作为示例,所以选择Java的下载链接,下载完成后进行解压缩,可以看到其中有名为selenium-java-x.x.x.jar的文件,待后续使用。

使用Selenium RC

打开Java IDE(如:Eclipse),创建一个项目,new->Java Project,注意:JRE选择jdk中的JRE。

导入需要的包,Build Path->Use as Source Folder,选择selenium-server-standalone-x.x.x.jar和selenium-java-x.x.x.jar。另外,需要使用由Selenium IDE录制的脚本,则需要导入Junit.jar。

新建类SeleniumDemo,代码如下:

import com.thoughtworks.selenium.DefaultSelenium;  
public class SeleniumDemo {
    public static void main(String[] args) {
        String host = "localhost";  
        int port = 4444;  
        String url = "http://www.baidu.com/";  
        String browserType = "*firefox";  
        String keyWordsLocator = "document.getElementById('kw')";     
        String search = "document.getElementById('su')";  
        DefaultSelenium selenium = new DefaultSelenium(host,port,browserType,url);  
        selenium.start();  
        selenium.open(url);  
        selenium.type(keyWordsLocator,"java selenium");  
        selenium.click(search);  
        selenium.waitForPageToLoad("50000");  
        selenium.stop();  
    }
}

确保Selenium服务器已经启动,在编译通过后,就可以运行了。

Selenium 2.0(WebDriver)

简介

Selenium2.0增加的主要功能是在Selenium1.0的基础上集成了WebDriver。Selenium RC通过在浏览器中运行JavaScript应用来模拟用户操作,WebDriver则是通过原生浏览器支持或浏览器扩展直接控制浏览器。

WebDriver曾经是Selenium1.0的竞争对手,而两个项目的开发人员都认为其各有优势,合并后必会创造更强大的自动化框架。原因如下:

  • WebDriver弥补了Selenium的缺陷(如:绕过JS沙箱)

  • Selenium解决了WebDriver存在的问题(如:支持更广泛的浏览器)

Selenium2.0还包括Selenium Server和Selenium Grid,通过Selenium Grid可以支持分布式测试。新的Selenium Grid不仅支持新的WebDriver API,而且仍然支持原有的Selenium RC API。Selenium IDE 从版本1.1.0开始也支持WebDriver API。

WebDriver针对各个浏览器而开发,取代了 Selenium RC 中嵌入到被测 Web 应用中的 JavaScript,与浏览器的紧密集成可以支持创建更高级的测试,且避免了 JavaScript 安全模型导致的限制。

除了来自浏览器厂商的支持,WebDriver 还利用操作系统级的调用模拟用户输入。WebDriver 支持 Firefox (FirefoxDriver)、IE (InternetExplorerDriver)、Opera (OperaDriver) 和 Chrome (ChromeDriver)。它还支持 Android (AndroidDriver) 和 iPhone (IPhoneDriver) 的移动应用测试。

此外,还包括一个基于 HtmlUnit 的无界面实现,即 HtmlUnitDriver。WebDriver API 可以通过 Python、Ruby、Java 和 C# 访问,支持开发人员使用他们偏爱的编程语言来创建测试。

下载和安装Selenium WebDriver

若想在Java环境使用Selenium WebDriver,则可以继续使用上述已配置好的Eclipse。另外,一些客户端语言(如:Java、Ruby等)是可以直接调用WebDriver的,所以无需启动Selenium Server即可运行。

以下会分别以Java和Ruby描述Selenium WebDriver的使用。Ruby环境请下载:

gem install selenium-webdriver

如果你使用bundler,可以将selenium-webdriver加入gemfile,然后运行bundle install

使用Selenium WebDriver

# Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class WebdriverDemo {
    public static void main(String[] args)
    {  
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.baidu.com");
        WebElement element = driver.findElement(By.id("kw"));
        element.sendKeys("webdriver");
        element.submit();
        driver.quit();
    }
}

# Ruby
require "selenium-webdriver"
driver = Selenium::WebDriver.for :firefox
driver.navigate.to "http://www.baidu.com"
element = driver.find_element(:id, 'kw')
element.send_keys "webdriver"   
element.submit
driver.quit  

Selenium IDE

简介

Selenium IDE是一款FireFox浏览器的插件,它可以进行脚本录制以及测试用例的转换。

下载和安装Selenium IDE

Selenium IDE可从Selenium HQ(http://docs.seleniumhq.org/download) 上获取,下载完成后,打开FireFox浏览器,在"Tools->Add-ons"中,将selenium-ide-x.x.x.xpi文件直接拖曳到对话框,自动完成安装后,可在Extensions中看到Selenium IDE已存在。

使用Selenium IDE

网上有很多关于Selenium IDE的使用方法,在这不再赘述,推荐一篇文章(http://www.ibm.com/developerworks/cn/web/1209_caimin_seleniumweb) 供大家参考。

Selenium Grid

简介

Selenium Grid,是基于Selenium的一个分布式测试平台,它可以通过服务器端的Hub来控制多个用于提供Selenium脚本运行环境的客户端,从而达到增快测试速度和有效扩充测试环境的目的。

一般地,在测试多浏览器并发,或多版本浏览器兼容性,或不同操作系统下的浏览器时,我们会使用Selenium Grid,另外,它也可帮助减少测试运行时间。

下载和安装Selenium Grid

Selenium Grid是一个包含JAR库文件、示例文件、Ant编译文件和其他文件的文件夹。目前,新版本的Selenium Grid 2.0和Selenium服务端进行了合并,所以仅需要下载一个jar包就可以获得它们。

使用Selenium Grid

网上有很多关于Selenium Grid的使用方法,在这不再赘述,推荐一篇文章(http://www.ibm.com/developerworks/cn/web/wa-seleniumgrid) 供大家参考。

2. Watir

简介

Watir(Web Application Testing in Ruby)是一个轻量级的用Ruby实现的Web自动化测试框架,提供了丰富的开发库,简化了自动化测试程序开发。

Watir仅支持IE浏览器,但是可以利用替代方案Firewatir或者Safariwatir来支持FireFox和Safari浏览器。

Watir还带有AutoIt扩展对JavaScript弹出框或者其他弹出窗口。不过,Watir对于ActiveX插件,JavaApplets,Flash或者其他的插件应用程序基本不支持。若要确定待测页面是否Watir支持,可在页面中右击,出现View Source选项的,则Watir就基本支持。

其实,Watir本质上是一个封装了COM(Component Object Model)接口的Ruby类库,IE的COM接口允许程序访问浏览器的文档对象模型(DOM),由于使用DOM技术,我们不需要担心屏幕位置,而且不需要担心待访问的元素被另一窗口遮挡,所以基于Watir的测试可以在最小化窗口中运行。

下载和安装Watir

gem install watir

如果你使用bundler,可以将watir加入gemfile,然后运行bundle install

使用Watir

以下用Google作为示例,模拟用户搜索"Watir"。

require "watir"
ie = Watir::IE.new
ie.goto("http://www.google.com.hk")
ie.text_field(:name,"q").set "watir"
ie.button(:name,"btnK").click
ie.close

3. Watir WebDriver

简介

Watir WebDriver是基于Ruby语言开发的Ruby库,它不光引进了纯面向对象的Watir API和平行于浏览器DOM的对象模型,同时还使用Selenium WebDriver来提供梦幻般的跨浏览器支持,可谓强强联手。

Watir WebDriver目前仅支持使用Ruby语言编写,不过它支持多种操作系统,主要有Windows、Linux、OS X,同时支持多种浏览器,如Chrome、FireFox、IE、Safari等。

下载和安装Watir WebDriver

gem install watir-webdriver

如果你使用bundler,可以将watir-webdriver加入gemfile,然后运行bundle install

使用Watir WebDriver

以下用FireFox和Google作为示例,模拟搜索"watir webdriver"。

require "watir-webdriver"
browser = Watir::Browser.new :firefox
browser.goto "http://www.google.com.hk"
browser.text_field(:name => "q").when_present.set "watir webdriver"
browser.button(:name => "btnK").when_present.click
browser.close

4. 有用连接

Selenium HQ(http://www.seleniumhq.org)

Selenium WebDriver(http://docs.seleniumhq.org/docs/03_webdriver.jsp)

Watir(http://watir.com)

Watir WebDriver(http://watirwebdriver.com)

RubyGems(http://rubygems.org)

使用Selenium Grid改进Web应用程序的测试(http://www.ibm.com/developerworks/cn/web/wa-seleniumgrid)

使用Selenium实现基于Web的自动化测试(http://www.ibm.com/developerworks/cn/web/1209_caimin_seleniumweb)

使用Watir加速面向Web应用的自动化测试程序的开发(http://www.ibm.com/developerworks/cn/opensource/os-cn-watir)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment