| Blog信息 |
|
blog名称: 日志总数:1304 评论数量:2242 留言数量:5 访问次数:7697757 建立时间:2006年5月29日 |

| |
|
[集成测试]Selenium--透明反复推介的集成测试工具(Pragmatic系列) 软件技术
lhwork 发表于 2006/9/6 10:21:25 |
|
国内大部分公司还依靠QA组的MM看着测试用例文档来手工测试,如果钱人有限,又想改变现状,最实在的建议是先编写直接访问数据库的商业层UnitTest和基于Selenium的集成测试这两种实用性最高的测试。
在徐昊指导下,SpringSide2.0 已经全面应用Selenium。
Selenium 能被选为最好集成测试、回归测试方案的原因,是因为:
1.Selenium IDE500)this.width=500'> ,一个FireFox plugin,能自动记录用户的操作,生成测试脚本。
2. 生成的测试脚本可以用Selenium Core500)this.width=500'>手工执行,也能基于Selenium RC500)this.width=500'>放入Java,C#,Ruby的单元测试用例中自动运行。
3. 测试用例调用实际的浏览器(如IE、FireFox)来执行测试。和有些开源方案自行实现Web解释引擎相比,实际的浏览器能模拟更多用户交互和JS语法,顺便还可以测试各浏览器兼容性。
4. 测试脚本语法非常简单,见后。
500)this.width=500'>1. 使用Selenium IDE生成脚本
Selenium IDE500)this.width=500'> 是一个Firefox1.5插件,下载后用Firefox将其打开。
工具->Selenium IDE,点击红色的recorder按钮开始录制,在网站中乱点时可以即时看到每个动作的脚本。
切换Format:显示 HTML,Java,C#,Ruby 语法的脚本。 option里还可以设定Java里Selenium变量的名称,如user。
500)this.width=500'>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();}
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。
脚本中按徐昊的指导,使用user 作为Selenium的变量名,使用例更加易读。 Selenium提供了非常丰富的用户交互函数,但Selenium RC里并没有为Java单列一个函数参考手册,需要阅读公共的Selenium Refrences500)this.width=500'>,再使用同名对应的java函数。
所有函数都是一个locator参数,将操作付诸某个页面上的对象。支持ID,DOM语法,XPath语法,CSS selector语法等,详见参考手册。
如果不会写,最好的老师还是Selenium IDE500)this.width=500'> 。比如那句点击 <a href="user.do?id=0" />修改</a>,就是用IDE得到user.click("//a[contains(@href, 'user.do?id=0')]")的XPath语句。
500)this.width=500'>
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
500)this.width=500'>.
/>
<
antcall
target
="tomcat.stop"
/>
</
sequential
>
</
parallel
>
|
|
|