Watir 是一个使用 Ruby 实现的开源Web 自动化测试框架,相对于那些庞大的商业工具来说,它很小巧,也很灵活,提供的功能也足够用。最近抽时间试用了一下,感觉还不错,准备下一步在公司推广使用。
因为 Watir 的网站上用户手册、示例代码以及 FAQ 都维护的不错,所以已有的东西我就不重复了,在这里简单介绍一下,如果同行们有兴趣,可以一起研究一下。
1. 脚本示例
先丢一段脚本给大家看看使用 Watir 来书写脚本是多么的方便。下面的例子是 Watir 自带的一段测试 Google 的搜索功能的脚本,不过我只保留了最主要的部分,以使它看起来更简洁一些:
require
'watir' # the watir controller
# open the IE browser
ie = Watir::IE.new
# Step 1: go to the test site: [url]http://www.google.com[/url]
ie. goto ([url]http://www.google.com[/url])
# Step 2: enter 'pickaxe' in the search text field
ie.text_field(: name, "q"). set("pickaxe") # q is the name of the search field
# Step 3: click the 'Google Search' button
ie.button(: name, "btnG").click # "btnG" is the name of the Search button
# Actual Result: Check that the 'Programming Ruby' link appears on the results page
if ie.contains_text("Programming Ruby")
puts "Test Passed. Found the test string: 'Programming Ruby'. Actual Results match Expected Results."
else
puts "Test Failed! Could not find: 'Programming Ruby'"
end
# End of test: Google search
# open the IE browser
ie = Watir::IE.new
# Step 1: go to the test site: [url]http://www.google.com[/url]
ie. goto ([url]http://www.google.com[/url])
# Step 2: enter 'pickaxe' in the search text field
ie.text_field(: name, "q"). set("pickaxe") # q is the name of the search field
# Step 3: click the 'Google Search' button
ie.button(: name, "btnG").click # "btnG" is the name of the Search button
# Actual Result: Check that the 'Programming Ruby' link appears on the results page
if ie.contains_text("Programming Ruby")
puts "Test Passed. Found the test string: 'Programming Ruby'. Actual Results match Expected Results."
else
puts "Test Failed! Could not find: 'Programming Ruby'"
end
# End of test: Google search