Google Test Search
This document walks through a very simple test case:. To begin, open it in a text editor. If you do not have that file, download it from . To open it with SciTE, simply right-click the file in Windows Explorer, and select Edit from the popup menu.The format of this example will be to show the Ruby test case scripting code in a box as you would see it in a text editor, with an explanation after it.
Getting Started
Open Internet Explorer, and try out the test case manually on your own:- go to the Google home page
- enter pickaxe in the search text field
- click the Google Search button
Expected Result
A Google page with results should be shown. Programming Ruby should be high on the list.Once you have tried the test case out manually, it's time to automate the test case using Watir. Return to the Google home page and view the page source: right mouse click > View Source. Now you can follow along and see how to automate this test with Watir based on the HTML tags in the Google search web application.
Section 1: Comments
Test cases should be commented, just as program code should be commented. In Ruby, any text on a single line that follows a # is a comment, and is ignored by the Ruby interpreter at run time.What you see in the text editor:
#-------------------------------------------------------------# # 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. #-------------------------------------------------------------#
Section 2: Includes
To use Watir, or any other library in our test case, requires us to tell the program where to find the library.What you see in the text editor:
# the Watir controller require "watir"
Section 3: Declare Variables
If we are going to use something in our script more than once, or something that could change, we can declare it as a variable and reuse it throughout the script. Some objects we can use for testing tend to change, such as URLs for applications we are testing. In this script, we assign the test URL as a variable. If it changes, we only have to change it in one place.What you see in the text editor:
# set a variable test_site = "http://www.google.com"
Section 4: Open an Internet Explorer Browser
To begin driving Internet Explorer, we need to tell Watir to open an instance for testing.What you see in the text editor:
# open the IE browser ie = Watir::IE.new
Section 5: Interacting With Google
Now we are ready to start automating the steps we ran manually using Watir.Beginning the test case
What you see in the text editor:# print some comments puts "Beginning of test: Google search."
Step 1: Go to the Google site
This test case follows a pattern of printing out what we intend Watir to do on a web application, followed by the Watir scripting code to carry out that action. This is a style of test case development that is useful for tracking down test case failures quickly.What you see in the text editor:
puts " Step 1: go to the test site: " + test_site ie.goto test_site
Step 2: Enter Search Term pickaxe in the search field
We need to enter the term to search in the text field on the Google home page.What you see in the web browser:
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
This is the tag in the HTML source with the name attribute we used:
"Google Search" value="">
Step 3: Click the Google Search button
We need to click the search button to activate the Google Search functionality.What you see in the web browser:
puts " Step 3: click the 'Google Search' button." ie.button(:name, "btnG").click # "btnG" is the name of the Search button
This is the tag in the HTML source with the name attribute we used:
"Google Search">
Section 6: Evaluating the Results
The Expected Result
This test case prints out what the Expected Result should be prior to the test case using Watir to evaluate the results.What you see in the text editor:
puts " Expected Result:" puts " A Google page with results should be shown. 'Programming Ruby' should be high on the list."
Verify Results
Using Watir and a little Ruby, we can evaluate the results to verify whether the test case passed or failed.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
- The first line prints out the Actual Result heading to the screen.
- The second line gets the text of the page and then calls the include? method to determine whether Programming Ruby appears on the first page.
- Using an if statement, we evaluate whether the include? method was true or false.
- If include? returns true (or actually anything but false), the text Programming Ruby appears. The test case passes and we print the Test Passed. message.
- Else, if include? returns false, the text Programming Ruby does not appear. The test case fails and we print the Test Failed! message.
- We close the conditional if section with an end statement.
End of Test Case
What you see in the text editor:puts "End of test: Google search."
excellent!!!!!
ReplyDeleteVery Usefulllllll...
thanx really very usefullllll
ReplyDeleteThanks very useful.......As a beginner after looking in to the test case ,I would like to go with Selenium.As watir is limited to IE only and needs to have knowledge in RUBY Scripting
ReplyDelete