Watir (Web Application Testing in Ruby) – yet another tool for automated testing. Watir is an open source automated testing tool for testing Web Applications. WATIR uses the Ruby scripting language and Internet Explorer's COM Interfaces for driving test scripts in IE. Currently, Watir supports only Internet Explorer.
This free to use tool is available for download @ http://www.openqa.org/watir/
You need to install Ruby scripting language before using this tool. You can get the latest version of Ruby scripting language from http://rubyforge.org/frs/?group_id=167
What’s Ruby
Ruby is an interpreted scripting language for quick and easy object-oriented programming created by Yukihiro Matsumoto. It has many features to process text files, multithreading, and can be used to automate system management tasks (as in Perl). It is simple, straight-forward, extensible, and portable. And it’s totally free.
Find more about ruby here http://www.ruby-lang.org/en
Watir…
Watir is a an easy to learn tool, you simply write the scripts in Ruby and tell the Watir controller to run these scripts. It does not have recording features like WinRunner or QuickTestPro tools (Now WatirRecorder is available @ http://www.openqa.org/watir-recorder/). It does not maintain any GUI map file or GUI object repository. It understands the objects based on their property values programmatically supplied by the scripter. You program the script on how to recognize the objects and perform operations on these objects. To do this you need to know the internals of a web page that you are testing. You need to know what objects are used and their identification properties etc. And then use these property values for identification and operation on these objects.
Here is an example of simple Watir script, which opens Microsoft Corporate Site and searches for Internet Explorer Development Home Page Link and opens Internet Explorer Development Home Page.
Test1.rb - Demo Watir Script
#Author: Unmesh Gundecha
#This script opens Microsoft Corporate Site and searches for Internet Explorer String
#and opens Internet Explorer Development Home Page.
#requires:
require 'watir'
#includes:
include Watir
#variables:
testSite = 'http://www.microsoft.com'
#open the IE browser:
$ie = IE.new
puts "## Begining of test: Microsoft Search"
puts " "
puts "Step 1 : Go to the test site: " + testSite
$ie.goto(testSite)
puts "Action: entered " + testSite + " in the address bar."
puts "Step Put 'Internet Explorer' in the search text field"
$ie.textField(:name,"qu").set("Internet Explorer")
puts "Action: entered 'Internet Explorer' in the search text field"
puts "Step click the 'Search' button"
$ie.button(:name, "msviGoButton").click # "msviGoButton" is the name of the button
puts " Action: clicked the Search button."
puts "Expected Result: "
puts " - a Search page with results should be shown. 'Internet Explorer Developer Center' should be on the list."
puts "Actual Result: Check that the 'Internet Explorer Developer Center' link appears on the search results page"
a = $ie.pageContainsText("Internet Explorer Developer Center")
if !a then
puts "Test Failed! Could not find: Internet Explorer Developer Center"
else
puts "Test Passed. Found the test string: Internet Explorer Developer Center. Actual Results match Expected Results."
puts "Step Click on Internet Explorer Developer Center link"
$ie.link(:text, "Internet Explorer Developer Center").click
puts "Action: clicked 'Internet Explorer Developer Center' link in the results list"
puts "Actual Result: Check that the 'Internet Explorer Developer Center' site is opened in Browser Window"
pageTitle = $ie.getDocument.titleputs
if pageTitle == 'Internet Explorer Developer Center' then
puts "Test Passed. Found the title string: Internet Explorer Developer Center. Page Opened"
else
puts "Test Failed! Could not find: Internet Explorer Developer Center Home Page"
end
end
puts " "
##End of test: Microsoft search"
puts
# -end of simple Microsoft search test
Execute this file at command prompt as C:\Test1.rb
You can do parameterization of tests using the DBI and File I/O functions on Ruby Language. You can access Excel through Win32Ole in Ruby.
Another great resource to know more about Ruby and Watir http://www.rubygarden.org/ruby?WebTestingWithRuby
DeveloperIQ Magazine, India has published my article on WATIR in June 2005 edition. This magazine is available in India and selected countries world over.
Recently I had given a presentation on WATIR tool in Computer Society of India, Pune Chapter’s Annual Event “Insights 2005” held at Hotel Le Meridian, Pune, INDIA.