For my evaluation of automation tools I’m first going to try WATIR. After all, it’s mentioned in a ot of job descriptions so other companies must find it very useful.

Web Application Testing in Ruby – an open source Ruby library that drives the browser the way users do (clicking links, entering form data, validation). Watir is used with Internet Explorer on Windows.
Ruby can connect to databases, read data files, export XML and structure code into reusable libraries. Watir will not work with ActiveX plugin components, Java Applets, Macromedia Flash, or other plugin applications. Right click on the object and see if the View Source menu option is available – if you can view the HTML source, that object can be automated using Watir.
To get started:
1. Install Ruby
2. Install Watir. After installing Ruby, simply open the command prompt and type:
gem install watir
3. Install IE Developer tool.
Quickies:
#comments use pound symbol
puts (”hello”) # function call
number = 5 #variable declared using assignment
name = ’string’ or “string”
ie.click # method call which is basically object.function
“bookkeeper”.include?(’book’) # methods can have arguments. returns true if book is a substring of bookkeeper
if num ==8
puts “Success” #write string Success
else
puts “Failed. Make sure if, else, end are on separate lines”
end
def five() #function
return 5
end
box = five() #box’s value is 5 from the function
require ‘mathlibrary’ #requires the Ruby library mathlibrary.rb
[1, 'hello!', 27] #arrays can contain any information
array.last is the same as array [2]
array[1] = ‘bt’ changes array to 1, bt, 27
for x in 1..10 do
puts x
end
# this would print 1 thru 10
Regular Expressions allow you to match patterns in strings.
/^as/ =~ ‘alas, no match’ # matches only the beginning with ^
/^.s/ =~ “As if I didn’t know better!” #matches anywhere
In Ruby, anything but the two special values false and nil are considered true for purposes of an if statement. So match results like 0 and 10 count as true.
Dictionaries are key-value pairs (like hashes and associative arrays)
dict = {}
dict[bt] = ‘car’
dict[bt] # retrieves value car
To create a test case:
- Open the text editor.
- Name the test file with a Ruby extension ( test.rb).
- Provide the new file with access to the Watir tool by entering this statement at the beginning of your test script:
require ‘watir’
- Open Internet Explorer and navigate to the application
- Type the corresponding Watir methods into the test script.
- Verify the results.
Link options: select by name, href, id, text
This is what’s seen in view source: Pickaxe
ie.link(:text, “Pickaxe”).click
Checkboxes, Radio buttons, Text fields can set or clear by name or id:
ie.radio(:id, “one”).set
ie.radio(:name, “clickme”).clear
ie.text_field(:name, “typeinme”).set(”Watir World”)
Buttons (including Image buttons) select by Id, name or value
ie.button(:name, “clickme”).click
ie.button(:src, /doit/).click # for
Forms without buttons (ie, hitting ENTER to submit). Use attributes of the form.Id, name, action, method.
ie.form(:action, “login”).submit
ie.form(:method, “get”).submit
Frames and Iframes. Look for html object . Get the Index and Name from IRB:
ie.show_frames #then access them by name, id, src
Click Menu Item
ie.frame(:name, “menu”).link(:text, “Click Menu Item”).click
New Browser Windows
First use attach method, then select by URL or Title
ie2 = Watir::IE.attach(:url, ‘http://www.google.com’)
ie3 = Watir::IE.attach(:title, ‘Test New Window’)
google_browser = Watir::IE.attach(:url , /google/)
google_browser.button(:text , /Search/).click
Text verification:
if ie.contains_text(”Reached test verification point.”)
puts: “Test passed. Page contains the text: Reached test verification point.”
else
puts: “Test failed! Page didn’t contain text: Reached test verification point.”
end
A more powerful way to validate is using xUnit Assertations.
require ‘test/unit’
Then create an instance of the class:
class TC_myTest < Test::Unit::TestCase
# fill in Test Case methods here
end
def test_myTestCase
# fill in method body with Watir code and assertion here
end
JavaScript generated popups such as Alert boxes, Windows Security popups, and other popups that are not contained in a web browser are not accessible the same way HTML pages are. Solution is in the works. In the meantime, try popups_test.rb in the unit tests directory, and use Watir Search for more information.
File Uploads:
Use the file_field method. Run and view the Ruby source in filefield_test.rb in the unit tests folder for usage.
To run without the browser visible, use -b
my_test.rb -b
To run tests concurrently:
concurrent_search.rb
IRB to see objects and attributes: ie.show_all_objects
IRB to check object by flashing it: ie.text_field(:name, “test_text”).flash
FireWatir is an tool that allows WATiR (http://www.openqa.org/watir/) scripts written for IE to work with the Firefox browser as well. This usually requires either no change or very small changes to existing scripts. This tool works with Firefox 1.5 and above.
Duplicate links:
$ie.link(:text => ‘Employees’, :index => 2).click
Tip on bypassing security alert window:
class Browser
def initialize
$ie = Watir::IE.new
end
def goto(url)
@url = url
t = Thread.new(){
puts “winclicker thread started”
wc = WinClicker.new
wc.clearSecurityAlertBox
}
m = Thread.new($ie) {
$ie.goto(@url)
}
m.join
t.join
$ie.bring_to_front
$ie.maximize
end
end
browser = Browser.new
browser.goto(’https://server_name/’)
——————-
Close all IE windows
require ‘watir/close_all’
Watir::IE.close_all
————————
Make Javascript Windows go away:
# A Better Popup Handler using the latest Watir version. Posted by Mark_cain@rl.gov
#
require ‘watir\contrib\enabled_popup’
#
def startClicker( button , waitTime= 9, user_input=nil )
# get a handle if one exists
hwnd = $ie.enabled_popup(waitTime)
if (hwnd) # yes there is a popup
w = WinClicker.new
if ( user_input )
w.setTextValueForFileNameField( hwnd, “#{user_input}” )
end
# I put this in to see the text being input it is not necessary to work
sleep 3
# “OK” or whatever the name on the button is
w.clickWindowsButton_hwnd( hwnd, “#{button}” )
#
# this is just cleanup
w=nil
end
end
#
# MAIN APPLICATION CODE
#
$ie = Watir::IE.start( “c:\test.htm” )
# This is whatever object that uses the click method.
# You MUST use the click_no_wait method.
$ie.image( :id, ‘3′ ).click_no_wait
#
# 3rd parameter is optional and is used for input and file dialog boxes.
startClicker( “OK “, 7 , “User Input” )
#
# Main application code follows
# …
———————————
Use Excel for data driven tests
require ‘win32ole’
excel = WIN32OLE::new(”excel.Application”)
workbook = excel.Workbooks.Open(”c:\\examples\\example_sheet.xls”)
worksheet = workbook.WorkSheets(1) # get first workbook
worksheet.Select # just to make sure macros are executed, if your sheet doesn’t have macros you can skip this step.
value = worksheet.Range(”a12″).Value # get the value at cell a12 in worksheet.
data = worksheet.Range(”a1:c12″).Value # returns 2D array with values starting from cell a1 to cell c12
———————————–
ie.element(how, what)
———————————–
Use Xpath if you can’t get html attributes
ie.select_list(:xpath, “xpath query”)
click me
ie.link(:xpath,”//a[@href='test.htm']/”).text # => “click me”
What to do for elements not having class in Watir?
You can use element_by_xpath function of IE class to get the underlying ole_object. Then you can invoke any method supported by that element.

{ 1 comment… read it below or add one }
Excellent Article! I use watir everyday.
You must log in to post a comment.