Friday, January 26, 2007

WATIR: Web Application Testing in Ruby

I am so excited about this. It's probably one of the niftiest tools for use in black-box testing websites/web applications that I have ever found.

WATIR is a ruby based system which allows you to create small scripts that drive your installed instance of IE. Using code that looks like thus:

require 'watir' # the watir controller

# set a variable
test_site = 'http://www.google.com'

# open the IE browser
ie = Watir::IE.new

# print some comments
puts "## Beginning of test: Google search"
puts " "

puts "Step 1: go to the test site: " + test_site
ie.goto(test_site)
puts " Action: entered " + test_site + " in the address bar."

puts "Step 2: enter 'pickaxe' in the search text field"
ie.text_field(:name, "q").set("pickaxe") # q is the name of the search field
puts " Action: entered pickaxe in the search field"

puts "Step 3: click the 'Google Search' button"
ie.button(:name, "btnG").click # "btnG" is the name of the Search button
puts " Action: clicked the Google Search button."

puts "Expected Result: "
puts " - a Google page with results should be shown. 'Programming Ruby' should be high on the list."

puts "Actual Result: Check that the 'Programming Ruby' link appears on the results page "
if ie.contains_text("Programming Ruby")
puts "Test Passed. Found the test string: 'Programming Ruby'. Actual Results match Expected Results."
else
puts "Test Failed! Could not find: 'Programming Ruby'"
end

puts " "
puts "## End of test: Google search"

That script listed above actually works, and will drive an IE browser to the Google homepage and force it to search for the term. So, as we build complex web applications, we can build these testing scripts, and when we roll out new capabilities, rather than having to spend hours upon hours doing formal tests for regression, you just spend a little bit running all the scripts again.

No comments:

Blog Widget by LinkWithin