Automated Testing with Watir – Tutorial for Begginers (Part 1)

by Nataliia Vasylyna | February 23, 2011 11:14 am

The purpose of this series of lessons is to show the main approaches to write tests using Watir tool.

Developed script will perform the following steps:

1. Go to google.com;
2. Enter in the search form text Watir;
3. Click on search and wait for the page is loaded;
4. Test the found results.

Writing tests by validation

Let’s consider one of the easiest ways, the most commonly used by beginners in programming. For this approach is very important if your tests will contain comments.

This greatly simplifies the understanding of the code. In ruby, any text on a single line, which follows the # – is a comment and is ignored by the interpreter during the launch.
For example:

#*************************************************************
# First lesson of Watir automated testing tool.
#
# Summary: Basic test of Google search engine.
# Description: the next steps to be automated:
#   - going to google.com,
#   - typing ‘Watir’ into search form,
#   - click ‘Search’,
#   - verify results.
# Author: Andrii Dzynia
#*************************************************************

Next, add the libraries with which we will work:

# Watir IE driver
require 'watir'
# Required in ruby 1.8.7
require 'rubygems'

Create instance of Internet Explorer for tests:

ie = Watir::IE.new

After this we are ready to begin work with our Web application. Let’s describe our scenario.

# text to show on console
puts "Beginning of the test for Google search engine"

Open Google home page

puts "Step 1:  Go to the Google homepage"
ie.goto "http://www.google.com"

Define a variable which will store the text to search

#set a variable
search_text = "Watir"

Recognize the locator of search form

<input maxlength=2048 name=q size=55 title="Google Search" value="">

Type there the text in variable

puts " Step 2: enter "+ search_text +" in the search text field."
ie.text_field(:name, "q").set search_text # "q" is the name of the search field

Recognize the locator of search button

<input maxlength=2048 name=q size=55 title="Google Search" value="">

And click on it

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

The click method after clicking on the button just waits to load a new page.

We display results from the page

puts " Expected Result:"
puts "  A Google page with results should be shown. 'Watir, pronounced water' should be high on the list."

Check the results

puts " Actual Result:"
if ie.text.include? "Watir, pronounced water"    
  puts "  Test Passed. Found the test string: 'Watir, pronounced water'. Actual Results match Expected Results."
else    
  puts "  Test Failed! Could not find: 'Watir, pronounced water'."
end

We will see message “Test Passed” if the text will be found
Otherwise (if ie.text.include? returns false) test will be failed.

After checking we show the message about completion of the script and close the browser.

puts "End of test: Google search."
ie.close

Here is final result:

#*************************************************************
# First lesson of Watir automated testing tool.
#
# Summary: Basic test of Google search engine.
# Description: the next steps to be automated:
#   - going to google.com,
#   - typing ‘Watir’ into search form,
#   - click ‘Search’,
#   - verify results.
# Author: Andrii Dzynia
#*************************************************************

# Required in ruby 1.8.7
require 'rubygems'
# Watir IE driver
require 'watir'

ie = Watir::IE.new

# text to show on console
puts "Beginning of the test for Google search engine"
puts "Step 1:  Go to the Google homepage"
ie.goto "http://www.google.com"

#set a variable
search_text = "Watir"puts "Step 2: enter "+ search_text +" in the search text field."

ie.text_field(:name, "q").set search_text # "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. 'Watir, pronounced water' should be high on the list."

puts " Actual Result:"
if ie.text.include? "Watir, pronounced water"    
 puts "  Test Passed. Found the text string: 'Watir, pronounced water'. Actual Results match Expected Results."
else    
 puts "  Test Failed! Could not find text: 'Watir, pronounced water'."
end

puts "End of test: Google search."
ie.close

Save the test as d:\WatirGoogleEngineFirstTest.rb and run from the console

ruby d:\WatirGoogleEngineFirstTest.rb

We see one by one messages on the screen

Beginning of the test for Google search engine
Step 1:  Go to the Google homepage 
Step 2: enter Watir in the search text field. 
Step 3: click the 'Google Search' button.
Expected Result:A Google page with results should be shown. 'Watir, pronounced water' should be high on the list. 
Actual Result:  Test Passed. Found the text string: 'Watir, pronounced water'. Actual Results match Expected Results.
End of test: Google search.

During the execution of the test see the open browser IE and all programmed actions.

Related Posts:

Source URL: http://blog.qatestlab.com/2011/02/23/automated-testing-with-watir-tutorial-for-begginers-part-1/