ruby+watir–百度搜尋樣本

來源:互聯網
上載者:User

代碼: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.comSearch_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 nameputs " 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.rbBeginning 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.2Successfully uninstalled watir-4.0.2-x86-mingw32C:\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天的事情,加油!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.