Misplaced Pages

Watir: Difference between revisions

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.
Browse history interactively← Previous editNext edit →Content deleted Content addedVisualWikitext
Revision as of 20:36, 28 September 2006 editSunSw0rd (talk | contribs)Extended confirmed users796 editsm Recording Scripts← Previous edit Revision as of 15:48, 29 September 2006 edit undoDRogers (talk | contribs)237 editsmNo edit summaryNext edit →
Line 3: Line 3:
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. 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. 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 to automate browser-based tests during ].


==Examples== ==Examples==

Revision as of 15:48, 29 September 2006

'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 to automate browser-based tests during web application development.

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 (as CSV)

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.

Write Output to Excel (as XLS)

So the above example is very simple, and may be all that is needed, but if you want to format the speadsheet it really needs to be in Excel format. In which case, the opening of the file and the writing to the file needs to be a bit different. Try this modification:

#open spreadsheet
excel = WIN32OLE::new('excel.Application')
workbook = excel.Workbooks.Add
worksheet = workbook.Worksheets(1)
worksheet.SaveAs("spreadsheet.xls")
#Log results
worksheet.range("a1").value = executionEnvironment
worksheet.range("b1").value = "Acceptable Screen1 time"
worksheet.range("c1").value = acceptableScreen1.to_s
worksheet.range("d1").value = "Actual Screen1 time"
worksheet.range("e1").value = actualScreen1.to_s
worksheet.range("f1").value = resultValue
#
# Etcetera...assume the above happens 4 times, for 4 screens...
#
#Format workbook columns
worksheet.range("b1:b4").Interior = 36 #pale yellow
worksheet.columns("b:b").AutoFit
#close the workbook
workbook.save
workbook.close
excel.Quit 

So this will permit you to not only write to Excel workbooks, but format the data as well. However here the writing is on a cell by cell basis rather than to an entire row as can be done in a CSV file.

Recording Scripts

There exists a very simple Ruby script called WatirMaker. See the External Link labeled "WatirMaker" to download it. Usage is very simple. Download it, remove the ".txt" suffix, then execute like:

#Execute WatirMaker
ruby WatirMaker.rb > c:\temp\rscript.rb

That's it. Be aware that it will generate plenty of errors on any complex site. But some quick and judicious editing and you may find this little recorder to be very useful for you. Just don't expect it to compare to a commercial tool. It is merely a time saver to help you produce the framework for your script, which you can then polish up programmatically.

External Links

See also

Category: