小试Ruby

昨晚终于决定写一阵子的Ruby了,因为要用Watir来做一些web的自动化测试。
先简单说下环境搭建:
Step 1: Ruby下载
http://www.ruby-lang.org/en/downloads
我下载的是“ Ruby 1.8.6 One-Click Installer (Ruby on Windows) ”

Step 2: Watir下载安装
如果网络好的话执行以下两个命令就可以了:
gem update –system
gem install watir

Step 3: 开始使用
下面是一个简单的例子,在GOOGLE中搜索ideshot,看搜索结果中是否有Suny Tse。

# the Watir controller
require "watir"
# set a variable
test_site = "http://www.google.com/"
content = "Suny Tse"
#open the IE browser
ie = Watir::IE.new
# print some comments
puts "Beginning of test: Google search."
puts " Step 1: go to the test site: " + test_site
ie.goto test_site
puts " Step 2: enter 'watir' in the search text field."
ie.text_field(:name, "q").set "ideashot" # "q" is the name of the search field
puts " Step 3: click the 'Google Search' button."
ie.button(:type, "submit").click # "submit" is the type of the Search button
puts " Expected Result:"
puts " A Google page with results should be shown. '#{content} ' should be high on the list."
puts " Actual Result:"
if ie.text.include? "#{content}"
puts " Test Passed. Found the test string: '#{content} '. Actual Results match Expected Results."
else
puts " Test Failed! Could not find: '#{content} '." 
end
puts "End of test: Google search."

下面这个例子是类似于自动评论机,有loop,以及对弹出框的处理。

4 days ago, this page was being read.

,

Subscribe to Comments