开源Web自动化测试框架——Watir试用手记
上一篇 / 下一篇 2009-09-03 15:31:18 / 个人分类:Ruby && Watir
Watir是一个使用Ruby实现的开源Web自动化测试框架,相对于那些庞大的商业工具来说,它很小巧,也很灵活,提供的功能也足够用。最近抽时间试用了一下,感觉还不错,准备下一步在公司推广使用。
7X4fD%l WsF]0因为Watir的网站上用户手册、示例代码以及FAQ都维护的不错,所以已有的东西我就不重复了,在这里简单介绍一下,如果同行们有兴趣,可以一起研究一下。
u6ITS C|i01. 脚本示例
+@S,TP3p9mAaq0先丢一段脚本给大家看看使用Watir来书写脚本是多么的方便。下面的例子是Watir自带的一段测试Google的搜索功能的脚本,不过我只保留了最主要的部分,以使它看起来更简洁一些:51Testing软件测试网 KH9cq ?Hs
require 'watir' # the watir controller51Testing软件测试网j{d.K C^,V1Qu}
# open the IE browser51Testing软件测试网5tH9{n?o:|
ie = Watir::IE.new
i m]~h*KU0# Step 1: go to the test site: http://www.google.com
lg7a%SwF8Pm0ie.goto (http://www.google.com)51Testing软件测试网(?$O6pGu!E9?c
# Step 2: enter 'pickaxe' in the search text field51Testing软件测试网/o~ P8C3V4Uk
ie.text_field(:name, "q").set("pickaxe") # q is the name of the search field51Testing软件测试网'_%{S@ gS5z
# Step 3: click the 'Google Search' button51Testing软件测试网 _xC4c8~9[3g)l
ie.button(:name, "btnG").click # "btnG" is the name of the Search button
$GlA}-TL!K8t0# Actual Result: Check that the 'Programming Ruby' link appears on the results page51Testing软件测试网+rT U1|#Z$J5~
if ie.contains_text("Programming Ruby") 51Testing软件测试网9C!j9|/C)P*iYX
puts "Test Passed. Found the test string: 'Programming Ruby'. Actual Results match Expected Results."
"wL4S(DL9n7p In]0else51Testing软件测试网Vw\O-g
puts "Test Failed! Could not find: 'Programming Ruby'"
em O%mMj Sm0end51Testing软件测试网)m#M|k~+j H
# End of test: Google search
f [f,Ji'n`0这段脚本要做的事情是打开Google的主页,然后在Google唯一的那个文本框内输入“pickaxe”这个字符串,然后按下“Google搜索”按钮,之后验证搜索结果的页面中是否包含了“Programming Ruby”这个字符串,并根据结果使用puts函数在屏幕上打印不同的信息。脚本中“#”后面的绿色部分是注释的内容。简单吗?说实话要比那些商业工具录制的脚本还要简洁和简单。
v/}|{Sn/C02. 所需要的环境51Testing软件测试网:~1p7Z*M5e.?k
Ruby :因为是使用Ruby实现的,脚本也是Ruby的脚本,所以需要在本机安装Ruby。可以点击这里下载。根据文档中说的,最好选择Ruby 1.8.2-14或者更高的版本,我安装的是Ruby 1.8.2-15 Stable Release。
gw1ka o-N$gxp0Watir :可以点击从这里下载,我下载的是Watir 1.4,是一个.zip文件,解压缩以后执行install.rb就可以了,具体的安装和配置请参见Watir用户手册。不要怕,虽然是英文的,但是很简单。
Qk$T&P