Misplaced Pages

Watir

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.

This is an old revision of this page, as edited by SunSw0rd (talk | contribs) at 17:52, 26 September 2006 (External Links). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Revision as of 17:52, 26 September 2006 by SunSw0rd (talk | contribs) (External Links)(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

'WATIR', pronounced "Water", is an acronym standing for "Web Application Testing in Ruby". Watir is an automated test tool which uses the Ruby scripting language to drive the Internet Explorer web browser, and is available as a Ruby Gem.

Ruby is an object oriented programming language started more than a decade ago in Japan (by Yukihiro Matsumoto). It is a very “pure” OO language. It is also an interpreted language, and has influences from multiple earlier OO languages. Ruby gems are packaged libraries.

WATIR makes use of the fact that Ruby has built in OLE capabilities. As such it is possible to drive the Microsoft Internet Explorer browser programmatically. Watir is a toolkit for automated tests to be developed and run against a web browser.

Examples

The google example

# Here we see a very simple WATIR script to drive to google and validate a page
require 'watir'                          # use watir gem
test_site = 'http://www.google.com'     # set a variable
ie = Watir::IE.new                       # open the IE browser
ie.goto(test_site)                       # load url, go to site
ie.text_field(:name, "q").set("pickaxe") # load text "pickaxe" into search field named "q"
ie.button(:name, "btnG").click           # "btnG" is the name of the Search button, click it
if ie.contains_text("Programming Ruby")  
  puts "Test Passed. Found the test string: 'Programming Ruby'."
else
  puts "Test Failed! Could not find: 'Programming Ruby'" 
end

The previous commands can be executed in the Interactive Ruby Shell (irb), or in a Ruby IDE such as FreeRIDE that is installed when Ruby is installed with the once click installer.

Handling popups

# To handle general popups, use WinClicker.
ie.button(:name, "btnG").click_no_wait   # ensure popup won't block Watir
hwnd = ie.enabled_popup(5)               # get a handle if one exists
if (hwnd)                                # yes there is a popup
  w = WinClicker.new
  w.makeWindowActive(hwnd)
  w.clickWindowsButton_hwnd(hwnd, "Yes") # click the "Yes" button
end

This example extends upon the google example by ensuring the google search button click will not wait for the popup. This approach will handle most annoying popups, however it may be necessary to download and install the current development Watir gem rather than using the standard Watir release.

View Browser Objects

Suppose you want to be able to see the various objects within the browser? The following are useful:

# To see objects within the browser
ie.frame("top_frame").text_fields.each { |t| puts t.to_s }
ie.frame("top_frame").spans.each { |s| puts s.to_s }
ie.frame("top_frame").tables.each { |t| puts t.to_s }
ie.frame("top_frame").links.each { |l| puts l.to_s }
ie.frame("top_frame").select_lists.each { |s| puts s.to_s }
ie.frame("top_frame").labels.each { |l| puts l.to_s }

In the above example, this also shows you how you would access information from within a frame. If there were no frame involved, then drop the "frame("top_frame")." part of the commands.

Capture Timings

Suppose you want to capture timing information, because you are running a performance test?

# Performance test timing
beginTime = 0
endTime = 0
beginTime = Time.now
=begin
*****************************
* Body of script goes here
*****************************
=end
endTime = Time.now
p (endTime - beginTime).to_s

Be aware that anything enclosed by an "=begin" and "=end" (on their own lines) is treated as a multi line comment. Typically you might wrap the final button click to submit a page in these commands, and the timing information will then just show you the time from submission to page response.

Write Output to Excel

It may useful to be able to repeatedly record output to an Excel spreadsheet. Data written to a workbook can be displayed graphically in charts. (How to do this will not be provided here, but see the external link called "Ruby and Excel")

Suppose you want to record a sequence of 4 screen to screen transition timings. You might then add the following to your script:

#Read test data input file
arr = IO.readlines("env.txt")
executionEnvironment = arr
acceptableScreen1 = arr.to_f
acceptableScreen2 = arr.to_f
acceptableScreen3 = arr.to_f
acceptableScreen4 = arr.to_f
#Result OK or not
resultValue = "OK"
#Strip line return from tested environment string
executionEnvironment = executionEnvironment.chomp
#open spreadsheet - new one each day this script is run
timeSpreadsheet = File.new( "../PC/" + Time.now.strftime("%d-%b-%y") + ".csv", "a")
=begin
**************************
* Add commands to load data to screen
* Initialize timing variables as in "Capture Timings" example
* Submit the "click" to submit from browser to web server
* Update timing variable as in "Capture Timings" example
**************************
=end
if acceptableScreen1 < actualTime
  resultValue = "NotOK"
end
#Log results
timeSpreadsheet.puts executionEnvironment + ",Expected screen 1 time," + acceptableScreen1.to_s 
+ ",Actual screen 1 time," + (endTime - beginTime).to_s + "," + resultValue
=begin
**************************
* Repeat this 3 times, once for each of the other 3 screens
* Remember to reinitialize resultValue, endTime, and beginTime each time
**************************
=end
#Close the browser
ie.close if ie
#Close the workbook
timeSpreadsheet.close

Some points about this example. First, the commented parts depend upon the previous examples above.

Second, an input file is read to extract test case information. The information should be written on 5 separate lines. The first line contains a reference to the environment in which this test is run, e.g., "dev", "test", "int_test", etc. This first line is loaded into the variable executionEnvironment. The line feed must be chopped off the end of the string, otherwise when written to Excel this will be on its own line, and the other data would go to the next line -- we want everything in the same row in the worksheet.

Third, the acceptableScreen# variables hold a number -- the expected maximum number of seconds for screen to screen response time. These numbers might all be the same, or different, depending on your test. The values are from lines 2 through 5 in the input file.

Fourth and finally, the line above where the spreadsheet is written has a line break in it for formatting for this site, so if you copy the above you see that it is on two lines -- you would want to put it all on one line.

External Links

See also

Category: