Selenium-集成测试工具

Selenium is a portable software testing framework for web applications . Selenium provides a record/playback tool for authoring tests without learning a test scripting language. Selenium provides a test domain specific language (DSL) to write tests in a number of popular programming languages, including Java, Ruby, Groovy, Python, PHP, and Perl. Test playback is in most modern web browsers . Selenium deploys to Windows , Linux , and Macintosh platforms.

Selenium was developed by a team of programmers and testers at ThoughtWorks . It is open source software , released under the Apache 2.0 license and can be downloaded and used without charge. The latest side project is Selenium Grid, which provides a hub allowing the running of multiple Selenium tests concurrently on any number of local or remote systems, thus minimizing test execution time.

Selenium IDE is a complete Integrated Development Environment (IDE) for Selenium tests (previously known as Selenium Recorder). It is implemented as a Firefox extension , and allows recording, editing, and debugging tests.

Scripts may be automatically recorded and edited manually providing autocompletion support and the ability to move commands around quickly.

Features:

  • Record and playback
  • Intelligent field selection will use IDs, names, or XPath as needed
  • Autocomplete for all common Selenium commands
  • Walk through tests
  • Debug and set breakpoints
  • Save tests as HTML , Ruby scripts, or other formats
  • Support for Selenium user-extensions.js file
  • Option to automatically assert the title of every page

[edit ] Selenium Remote Control

Selenium Remote Control (RC ) is a server, written in Java , that accepts commands for the browser via HTTP . RC makes it possible to write automated tests for a web application in any programming language, which allows for better integration of Selenium in existing unit test frameworks. To make writing tests easier, Selenium project currently provides client drivers for Python , Ruby , .NET , Perl , Java , and PHP . The Java driver can also be used with JavaScript (via the Rhino engine).

[edit ] See also

[edit ]

 

Selenium 能被选为最好集成测试、回归测试方案,是因为:

1.Selenium IDE ,一个FireFox plugin,能自动记录用户的操作,生成测试脚本。

2. 生成的测试脚本可以用Selenium Core手工执行,也能基于Selenium RC放入Java,C#,Ruby的单元测试用例中自动运行。

3. 测试用例调用实际的浏览器(如IE、FireFox)来执行测试。和有些开源方案自行实现Web解释引擎相比,实际的浏览器能模拟更多用户交互和JS语法,顺便还可以测试各浏览器兼容性。 软件开发网 www.mscto.com

4. 测试脚本语法非常简单,见后。


1. 使用Selenium IDE生成脚本

  Selenium IDE 是一个Firefox1.5插件,下载后用Firefox将其打开。 软件开发网 www.mscto.com

  工具->Selenium IDE,点击红色的recorder按钮开始录制,在网站中乱点时可以即时看到每个动作的脚本。

  切换Format:显示 HTML,Java,C#,Ruby 语法的脚本。 option里还可以设定Java里Selenium变量的名称,如user。 软件开发网 www.mscto.com

2.测试用例与测试脚本

  测试用例在Selenium IDE生成->Copy Paste的流程下非常的容易。

public class UserManagerTest extends TestCase
{
    private Selenium user;

    public void setUp() throws Exception {
       user= new DefaultSelenium("localhost", SeleniumServer.DEFAULT_PORT, "*iexplore", "http://localhost:8080");
       user.start();
}
protected void tearDown() throws Exception {
        user.stop();
} 软件开发网 www.mscto.com

public void testUserEdit() {
    user.open("/helloworld");
    user.click("//a/[contains(@href, 'user.do?id=0')/]");
    user.waitForPageToLoad("3000");
    user.type("user.name", "calvin");
    user.click("save");
    user.waitForPageToLoad("3000");
    assertTrue(user.isTextPresent("calvin"));
}
  留意setUp中的"*iexplore"参数,设定使用IE作为测试浏览器;如果设为"*firefox",就会在PATH中查找*firefox.exe。

  注意,Selenium使用IE时的Proxy机制比较特殊,如果你同时在本机ADSL modem拨号上网,要先断网。

  脚本中按徐昊的指导,使用user 作为Selenium的变量名,使用例更加易读。

  Selenium提供了非常丰富的用户交互函数,但Selenium RC里并没有为Java单列一个函数参考手册,需要阅读公共的Selenium Refrences,再使用同名对应的java函数。

  所有函数都是一个locator参数,将操作付诸某个页面上的对象。支持ID,DOM语法,XPath语法,CSS selector语法等,详见参考手册。

  如果不会写,最好的老师还是Selenium IDE 。比如那句点击 <a href="user.do?id=0" _fcksavedurl=""user.do?id=0"" _fcksavedurl=""user.do?id=0"" _fcksavedurl=""user.do?id=0"" _fcksavedurl=""user.do?id=0"" _fcksavedurl=""user.do?id=0"" _fcksavedurl=""user.do?id=0"" _fcksavedurl=""user.do?id=0"" />修改</a>,就是用IDE得到user.click("//a[contains(@href, 'user.do?id=0')]")的XPath语句。

3.Ant的运行脚本

  我写的Ant测试脚本一个重要特征是使用<parallel> 并行容器节点,一边同时打开tomcat 和selenium server,一边等待两者打开后执行JUnit。
  如果不使用并行节点,而是用spawn=yes属性后台启动tomcat,屏幕里就看不到tomcat信息,如果测试意外终止的话,就不能关闭tomcat,很不方便。 

<parallel>
    <antcall target="tomcat.start"/>
    <antcall target="selenium.server.start"/>
    <sequential>
        <waitfor maxwait="10" maxwaitunit="minute" checkevery="1" checkeveryunit="second">
            <http url=http://localhost:8080/>
        </waitfor>
        <waitfor maxwait="10" maxwaitunit="minute" checkevery="1" checkeveryunit="second">
            <socket server="localhost" port="4444"/>
       </waitfor>
       <junit..../>
       <antcall target="tomcat.stop"/>


    </sequential>
</parallel>4.SpringSide 中的FunctionalTestCase基类
  SpringSide中抽象了一个FunctionalTestCase基类,抽取了setUp() ,tearDown()函数中selenium server 开闭操作。

  其中浏览器类型默认为"*iexplore", 基本url默认为http://localhost:8080 软件开发网 www.mscto.com

  用户可以在selenium.properties 中重新设定selenium.explorer 和selenium.baseurl 变量。

 

一.Selenium是什么?

  Selenium是ThroughtWorks公司一个强大的开源Web功能测试工具系列,本系列现在主要包括以下4款:

   1.Selenium Core:支持DHTML的测试案例(效果类似数据驱动测试),它是Selenium  IDE和Selenium  RC的引擎。

   2.Selenium IDE:FireFox的一个插件,支持脚本录制。

   3.Selenium RC:Selenium Remote Control。后续的系列文章我会主要针对Selenium RC展开介绍。

   4.Selenium Grid:允许同时并行地、在不同的环境上运行多个测试任务,极大地加快Web应用的功能测试。

  二.选择合适的Selenium工具

  既然Selenium工具有4款这么多,那到底如何选择呢??我从"Selenium官网"这里找了一个表:

 Selenium IDESelenium Remote ControlSelenium CoreSelenium Core HTA
浏览器支持仅Firefox很多所有仅IE
需要远程安装
支持 HTTPS/SSL是*
支持跨域是*
需要Java
将测试结果保存到磁盘
多语言支持仅Selenese很多仅Selenese仅Selenese

   这里没有介绍Selenium Grid,但介绍了另外一个Selenium Core HTA,Selenium Core HTA其实是Selenium Core的额外模式,你只要Selenium Core配置稍加修改,即为HTA模式,Selenium Core HTA可以在IE最高安全等级(特权)下工作,这意味着它仅能在IE下工作,由于限制较大,下面将排除对Selenium Core HTA的讨论。

  1.浏览器支持:

   (1).Selenium IDE仅可以在Firefox中工作。

   (2).Selenium Remote Control支持很多浏览器,包括最常用的:firefox,ie,safari等N款浏览器。

   (3).Selenium Core支持的浏览器是最广的,这点和它的实现有关。作为IDE和RC的引擎,Selenium Core几乎可以在任何浏览器中工作。

  2.需要远程安装:是否需要在被测网站的服务端安装?

   这里只有Selenium Core需要,这是出于同源策略的原因。这也是Selenium Core一个很大的限制,试问,如果你要测试Google.com,还得在google的服务器上装一个Selenium Core,那是多搞笑的一件事。

   而Selenium IDE和Selenium Core HTA不会被同源策略所限制,因为他们对浏览器扩展了。

   Selenium RC提供一个代码服务器来保证Selenium JS文件看似来自相同的远程服务器,从而符合同源策略;代理服务器欺骗浏览器,让它认为这里的确有像http://www.google.com/selenium/这样的目录。

  3.支持HTTPS/SSL:

   这里不说了,都支持。Selenium RC在“是”后面加*因为它是在最近版本支持的,仅此而已。

  4.需要Java:准确的说是需要JRE

   这项只有Selenium RC需要,上面2中所说的“代理服务器”是一个Java程序,需在跑测试案例前启动。

  5.将测试结果保存到磁盘

   只有Selenium Core不能将任何测试结果写到磁盘上(因为它是用javascript写的,它不允许向磁盘写数据),其解决方案是当然你可以将测试结果发送到另外一台服务器保存。这也是Selenium Core的一大限制。

  6.多语言支持

   (1).Selenium IDE仅支持Selenium语言。

   (2).Selenium RC支持很多语言,如:C#,Java,Python,Ruby等。

   (3).Selenium Core也是仅支持Selenium语言。

  Selenium语言的测试案例如下:

Selenium私房菜系列1 -- Selenium简介

  它的优点是:简单,用(Command,Target,Value)三种元素组成一个行为,并且有辅助录制脚本工具(如:Firefox IDE,Selenium Core等),不懂程序的测试人员都可以轻松地编写测试案例。

  它的缺点是:Selenese有一些严格的限制:它没有条件(没有"if"表达式),并且它没有循环(没有"For"表达式)。这会使编写复杂的测试变得困难甚至不可能。

   OK,现在我们来研究下到底该使用哪款工具开展测试!

    (1).Selenium IDE支持并且只支持Firefox浏览器,支持的浏览器太少,而依附于Firefox也不便于日后开展自动化测试,但是,它的录制快捷好用!并且有代码 转换功能,可以把Selenium语言测试案例转为C#,Java等语言的测试案例,我建议使用Selenium IDE + FireBug进行测试案例的编写,然后转为其他语言的测试案例后,再调用Selenium RC运行测试案例。

   (2).Selenium Core,它的优点是编写测试案例简单,并且支持绝大多数的浏览器,但缺点也同样明显,Selenium Core需要远程安装,Selenese语言也限制了复杂案例的可能性,并且没有良好的外部扩展,这是些都会是致命的问题。因为一个款测试工具不可能 100%满足你测试需求的,当它不能满足你测试需求时候,它必须有一个扩展机制可以让你可以使用其他的方式满足你需求,否则这款测试软件即使功能强大,也 请三思慎用,否则当投入大量资源后才发现某些问题不能解决,那时候已经晚了,这是我的切身体会。

   (3).Selenium RC是我推荐使用的工具,它支持很多浏览器,可以使用C#,Java等语言编写测试案例,易于维护,同时提供了很好的扩展性,所以后续的文档我会以Selenium RC作为默认的测试工具。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值