This is an old revision of this page, as edited by SunSw0rd (talk | contribs) at 20:43, 25 September 2006 (Seeing browser objects, timing information). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Revision as of 20:43, 25 September 2006 by SunSw0rd (talk | contribs) (Seeing browser objects, timing information)(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.
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.
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.
External Links
- Rubyforge Homepage for the tool
- Watir User Guide
- Current Watir Development Versions
- The Ruby Homepage
- Ruby Gems User Guide