Ruby + watir-Baidu search example

Source: Internet
Author: User

Code: URL, search content, text verification points are all made into variables; open IE, enter www.baidu.com, enter the search content "watir", click submit, and query the results, use the text verification point Content to verify the Content returned by Baidu server.

#-------------------------------------------------------------# # 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

The above script was found on the http://www.51autotest.com forum, the default code is google search, I changed back to Baidu. In addition, the IE. close code is not added at the end of the Code. To improve the code, you must add ie. close.

Returned results:

>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

When running the above script, the system prompts "ruby Watir: IE (NameError)" error, and then adds require "rubygems" and require "watir-classic" to the script ", although the problem is solved, other errors occur. Finally, through the gem list command to view the various versions, we found that watir, commonwatir, watir-classic, win32-process version is higher.

Solution:

The watir version and commonwatir version must be the same, both reduced to 3.0.0
Watir-classic version reduced to 3.0.0
Win32-process version down to 0.6.6

Example:

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

It seems that learning ruby + watir + webdriver is not a matter of one day and two days. Come on!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.