Web应用自动化验收工具——Selenium系列预研
最近领导要我和小何一起研究一下自动化测试,并找一下开源的WEB界面自动化测试工具,我锁定了Selenium,小何锁定了Watij,下面是我对Selenium的一些研究以及与Watij的对比。这里感谢一下hyddd写的《Selenium私房菜系列》,是我的入门文章,下面某些内容也是参考了此文。欢迎指正...
1.Selenium工具简介
Selenium是ThoughtWorks公司开发的一套基于WEB应用的验收测试工具,直接运行在浏览器中,模拟客户操作。它抽象出一系列命令来模块用户操作,比如open命令表示打开一个URL,click命令表示点击某个按钮。Selenium实际上将这些命令转化成实际的HTTP请求在浏览器中运行。本系列现在主要包括以下4款:
1. Selenium Core,它的优点是编写测试案例简单,并且支持绝大多数的浏览器,但缺点也同样明显,Selenium Core需要远程安装,Selenese 语言也限制了复杂案例的可能性,并且没有良好的外部扩展,这是些都会是致命的问题。
2. Selenium Grid:允许同时并行地、在不同的环境上运行多个测试任务,极大地加快Web 应用的功能测试。
3. Selenium IDE :支持并且只支持Firefox浏览器,属于Firefox的一个插件,支持的浏览器太少,而依附于Firefox 也不便于日后开展自动化测试,但是,它的录制快捷好用!并且有代码转换功能,可以把Selenium 语言测试用例转为C#,Java,PHP,Ruby,Prel,Groovy,Python等语言的测试案例,我建议使用Selenium IDE + FireBug 进行测试案例的编写,然后转为其他语言的测试用例后,再调用Selenium RC运行测试用例。
4. Selenium RC(Remote Control) 是本人推荐使用的工具,它支持很多浏览器,可以使用C#,Java 等语言编写测试案例,易于维护,同时提供了很好的扩展性,所以接下来将针对Selenium RC 作为默认的测试工具进行介绍。
所以这里建议采用Selenium IDE + Selenium RC + Firebug组合搭建Web应用自动化验收测试。
2.Selenium-IDE安装和使用
安装
1. Selenium官网(http://seleniumhq.org/)下载Selenium-IDE作为Firefox的插件进行安装
使用
1.Firefox工具栏,打开Selenium-IDE插件,如下图:
2.选择插件界面中右上角红色录制按钮(开始录制、停止录制都是此按钮),如下图,这里录制登陆集中管理工具的过程。
3.录制完成后,点击回放按钮可以对刚刚录制的脚本进行回放,这里可以调整回放速度。
4.可以将录制的脚本转换成C#,Java,PHP,Ruby,Prel,Groovy,Python等语言,这里选择Java,如下图:
转换后代码如下:
package com.tongweb.selenium.test;import com.thoughtworks.selenium.SeleneseTestCase;/** * 集中管理工具中创建一个名为jdbcpool_test连接池,创建成功后进行删除操作, * 为了更好的展示界面是如何操作的,所以加入不少sleep语句。 * @author pengyao 07/09/2010 * */public class CreateAndDeleteJdbcConnectPoolTestCase extends SeleneseTestCase { public void setUp() throws Exception { setUp("http://localhost:9060/", "*firefox"); } public void testCreateAndDeleteJdbcConnectPool() throws Exception { selenium.windowMaximize(); selenium.open("/gm/login.jsf"); selenium.type("j_username", "tongweb"); selenium.type("j_password", "tongweb"); selenium.click("j_security_check"); selenium.waitForPageToLoad("30000"); selenium.selectFrame("mainFrame"); selenium.click("b1"); // jdbcpool_test JDBC连接池radio在页面的xpath路径 String jdbcpool_test = "//tr[td/a='jdbcpool_test']/td[1]/input"; // 判断如果jdbcpool_test JDBC连接池已存在,则先进行删除操作 boolean isExist = selenium.isElementPresent(jdbcpool_test); if (isExist) { selenium.click("//tr[td/a='jdbcpool_test']/td[1]/input"); Thread.sleep(3000); selenium.click("TongWebIndex:j_id_id273"); Thread.sleep(3000); selenium.waitForPageToLoad("30000"); } // 填写必要参数 selenium.click("TongWebIndex:j_id_id271"); selenium.waitForPageToLoad("30000"); selenium.type("createConnectPool:ConnPoolName", "jdbcpool_test"); selenium.select("createConnectPool:resType", "label=javax.sql.DataSource"); selenium.select("createConnectPool:dataSourceMap", "label=MySQL Connector/J Type 4 Driver for MySQL"); selenium.type("createConnectPool:ConnPoolDesc", "aaa"); selenium.addSelection("createConnectPool:j_id_id137", "label=192.168.11.24:7200"); selenium.click("createConnectPool:j_id_id148"); selenium.waitForPageToLoad("30000"); selenium.type("createConnectPoolProps:username", "root"); selenium.type("createConnectPoolProps:connURL", "jdbc:mysql://192.168.11.24:3306/test?user=root"); selenium.click("add"); selenium.type("propName_0", "ddd"); selenium.type("propValue_0", "ddd"); selenium.click("createConnectPoolProps:j_id_id372"); selenium.waitForPageToLoad("30000"); Thread.sleep(5000); // 判断是否创建成功 assertTrue(selenium.isElementPresent(jdbcpool_test)); selenium.click("//tr[td/a='jdbcpool_test']/td[1]/input"); Thread.sleep(3000); selenium.click("TongWebIndex:j_id_id273"); selenium.waitForPageToLoad("30000"); // 判断是否删除成功 assertFalse(selenium.isElementPresent(jdbcpool_test)); }}