• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ruby+watir--百度搜索示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

代码:URL、搜索内容、文本验证点都做成了变量;打开IE后,输入www.baidu.com,输入搜索内容“watir”,点击submit,查询出结果后,使用文本验证点Content去验证百度服务器返回内容。

#-------------------------------------------------------------# 
# Demo test for the Watir controller. 
# 
# Simple Google test written by Jonathan Kohl 10/10/04. 
# 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" 
#require "watir-classic"
# set a variable 
test_site = "http://www.baidu.com/"   #search URL  google.com
Search_name = "watir"   #search name 
Content = "download.csdn.net"   #search results 
#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, "wd").set "watir"      # "q" is the name of the search field 
ie.text_field(:name, "wd").set Search_name     #search name
puts " Step 3: click the 'baidu submit' 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."
puts " Last Step Close IE!!"
ie.close

上面脚本是从http://www.51autotest.com论坛上找到的,代码中默认是google搜索,我改回百度的啦。  另外代码结尾中没有加入IE关闭的代码,要完善一些,要加入的ie.close。

返回结果:

>ruby baidu.rb
Beginning of test: Google search.
 Step 1: go to the test site: http://www.baidu.com/
 Step 2: enter 'watir' in the search text field.
 Step 3: click the 'baidu submit' button.
 Expected Result:
 A Google page with results should be shown. 'download.csdn.net ' should be high on the list.
 Actual Result:
 Test Passed. Found the test string: 'download.csdn.net '.Actual Results match Expected Results.
 End of test: Google search.
 Last Step Close IE!!
>Exit code: 0

一开始运行上面脚本时,提示:“ruby Watir::IE (NameError)”的错误,然后再脚本中增加require "rubygems"和require "watir-classic",问题虽然解决,但是出现了其他的错误。最后通过gem list命令查看各个的版本号,发现watir、commonwatir、watir-classic、win32-process的版本高较高。

解决:

watir版本和commonwatir的版本要一致,都降低到3.0.0
watir-classic版本降低到3.0.0
win32-process版本降低到0.6.6

示例:

C:\ruby>gem uninstall watir -v 4.0.2
Successfully uninstalled watir-4.0.2-x86-mingw32

C:\ruby>gem install watir -v 3.0.0

gem uninstall watir-classic -v 3.3.0

gem install watir-classic -v 3.0.0


gem uninstall win32-process -v 0.7.0

gem install win32-process -v 0.6.6

看样子学习ruby+watir+webdriver并非1天2天的事情,加油!


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ruby安装发布时间:2022-07-13
下一篇:
Ruby发送http协议(get、post、https服务器验证、https双向验证)发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap