watir安装

上一篇 / 下一篇  2011-02-11 19:04:30 / 个人分类:工具

照http://watir.com/installation/的来。windows系统, 无防火墙。
1. 安装ruby。先是下载了ruby1.8.6,但是后面安装时某一步报1.8.6不行(好像是rubygems-update),要我装1.8.7,于是重新下载再装一遍。安装时选中把ruby添加到路径里,省得后面再设。
2. gem update --system
3. gem install watir 这一步自动装了watir1.7.1
4. 装firefox plugin.

然后就是试用了。ruby有两种执行方式,一种是交互式执行,敲入irb,然后require 'watir',会报no such file to load -- watir的错误,google一下会知道,需要先执行require 'rubygems',再执行require 'watir'就可以了。当然这样敲太麻烦了,适合调试吧。
另一种执行方式是ruby test.rb,test.rb是要执行的代码。可以从http://wiki.openqa.org/download/attachments/14614665/google_search.rb?version=1&modificationDate=1226438427170下载watir的基本的google search例子,我把具体代码贴在文章后面,就是启动ie,操作一些页面元素而已。把这个例子保存为google_search.rb,然后执行ruby google_search.rb,就可以看到自动运行的效果了。

http://www.zhuaijun.cn/archives/53这个链接还给出了中文支持,ide安装等,暂时不需要,就不管了。里面还提到
"watir也有现存的弱点,对Frame和javascript支持不好,当我看完ruby语言,看完watir内置方法,配置好所有的环境准备开工之 时,忽然发现项目的页面全是js调用出来的,从HTML根本看不到任何元素,额的神啊,当我了解这个真相以后,绝望了。。。"。
如果真不能支持javascript,那是很麻烦的。查了一下,http://blog.csdn.net/Testing_is_believing/archive/2010/01/24/5251229.aspx里有提到怎么在watir中调用javascript脚本。不是很确定这样是否能把javascript执行后给出的页面源代码拿出来。有空再仔细研究吧。

google_search.rb代码(已加入require "rubygems"代码):

# Please, when you update this file, update also http://wiki.openqa.org/display/WTR/Example+Test+Case

#-------------------------------------------------------------#
# Demo test for the Watir controller.
#
# Purpose: to demonstrate the following Watir functionality:
#   * entering text into a text field,
#   * clicking a button,
#   * checking to see if a page contains text.
# Test will search Google for the "pickaxe" Ruby book.
#-------------------------------------------------------------#

# the Watir controller
require "rubygems"
require "watir"

# set a variable
test_site = "http://www.google.com"

# 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 'pickaxe' in the search text field."
ie.text_field(:name, "q").set "pickaxe" # "q" is the name of the search field

puts " Step 3: click the 'Google Search' button."
ie.button(:name, "btnG").click # "btnG" is the name of the Search button

puts " Expected Result:"
puts "  A Google page with results should be shown. 'Programming Ruby' should be high on the list."

puts " Actual Result:"
if ie.text.include? "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

puts "End of test: Google search."





TAG:

 

评分:0

我来说两句

日历

« 2024-04-13  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 68818
  • 日志数: 44
  • 文件数: 40
  • 建立时间: 2010-12-06
  • 更新时间: 2011-05-31

RSS订阅

Open Toolbar