前两天在一本《程序员》杂志上接触了selenium这个框架,说是可以和Junit整合进行web界面测试,将各模块的功能自动的展现个客户,这无疑吸引了我。花了两天的时间研究了一下。
selenium大致可分为两个版本Selenium Core和SeleniumRC,第一种是以网页的形式编写TestCase,而后一种用Junit,并且支持多种语言,个人表较喜欢后一种,可以和ANT整合用于自动化测试。本人也是初步使用,学的过程中主要结合API和编写好的用例说明,具体的情况可在问官方网站进行了解 http://www.openqa.org/selenium/
seleniumRC下载地址:http://selenium-rc.openqa.org/download.jsp运行测试前在DOS命令下启动selenium-server.jar(无需放到环境目录下):java -jar selenium-server.jar
具体测试用例如下:主要用于form表单的提交测试
package
org.selenium.test;

import
com.thoughtworks.selenium.SeleneseTestCase;


public
class
TestSelenium
extends
SeleneseTestCase
...
{


public void testSelenium() throws Throwable ...{

try ...{
// 每个测试步骤间隔1秒
selenium.setSpeed("1000");
// 打开测试的网页
selenium.open("http://localhost:8080/TestProject/form/selenium.jsp");
// 获取标题
assertEquals("selenium", selenium.getTitle());
assertTrue(selenium.isElementPresent("xpath=//input[@name='username']"));
// 向input中type为text的栏位键入信息
selenium.type("xpath=//input[@name='username']", "chenxu");
selenium.type("xpath=//input[@name='password']", "password");
// 选择下拉列表
selenium.select("xpath=//select[@name='select']", "index=1");
assertEquals("1", selenium.getSelectedIndex("xpath=//select[@name='select']"));
assertEquals("chenxu", selenium.getSelectedValue("xpath=//select[@name='select']"));
// 勾选或去选check/radio
selenium.check("xpath=//input[@id='check2']");
//获取name为check栏位的id属性
selenium.getAttribute("check@id");
assertTrue(selenium.isChecked("xpath=//input[@id='check2']"));
selenium.uncheck("xpath=//input[@id='check2']");
assertFalse(selenium.isChecked("xpath=//input[@id='check2']"));
assertEquals("chenxu", selenium.getValue("xpath=//select[@name='select']"));
// 点击提交按钮
selenium.click("xpath=//input[@type='submit']");
// 等待页面载入
selenium.waitForPageToLoad("3000");
// 获取新页面标题
assertEquals("success", selenium.getTitle());
//确保测试中没有错误出现
checkForVerificationErrors();

} finally ...{
//清空错误信息列表
clearVerificationErrors();
}
}
}
注:1,这里的ElementLocator使用xpath方式,表达式看上去复杂,其实并不难理解,如"xpath=//input[@id='check']"xpath=//是固定模式,input指form表单的input栏位,id自然就是input中的id属性了。selenium可以使用多种元素定位方法(ElementLocator)具体方法可查阅API.
2,这里的测试类继承了SeleneseTestCase,该类继承至Junit的TestCase,其setup()和teardown()方法用于初始化和终止selenium服务,同时覆盖了一些断言(assert)方法。
测试界面代码如下:selenium.jsp
<%
...
@ page language="java" pageEncoding="ISO-8859-1"
%>

<%
...
@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"
%>

<%
...
@ taglib uri="http://struts.apache.org/tags-html" prefix="html"
%>
<
html
>
<
head
>
<
title
>
selenium
</
title
>
</
head
>
<
body
>
<
html:form
action
="/selenium"
>
<
table
>
<
tr
><
td
>
UserName:
</
td
><
td
><
input
type
="text"
name
="username"
/>
</
td
></
tr
>
<
tr
><
td
>
Password:
</
td
><
td
><
input
type
="password"
name
="password"
>
</
td
></
tr
>
<
tr
><
td
>
Select:
</
td
><
td
><
select
name
="select"
>
<
option
value
="javaman"
>
javaman
</
option
>
<
option
value
="chenxu"
>
chenxu
</
option
>
</
select
></
td
></
tr
>
<
tr
><
td
>
Radio:
</
td
><
td
>
man
<
input
type
="radio"
name
="radio"
value
="man"
id
="radio1"
checked
="checked"
/>
woman
<
input
type
="radio"
name
="radio"
value
="woman"
id
="radio2"
/>
</
td
></
tr
>
<
tr
><
td
>
CheckBox:
</
td
><
td
>
javaman
<
input
type
="checkbox"
name
="check"
value
="javaman"
checked
="checked"
id
="check1"
>
chenxu
<
input
type
="checkbox"
name
="check"
value
="javaman"
id
="check2"
>
</
td
></
tr
>
<
tr
><
td
><
input
type
="submit"
value
="submit"
/></
td
></
tr
>
</
table
>
</
html:form
>
<
a
href
="./success.jsp"
id
="link"
>
link to success page
</
a
>
</
body
>
</
html
>
