Realsolve Logo
Blogs
 

Phil Zoio's Weblog, April 7, 2005

Web Application Testing in Ruby

I've heard and read a bit about Ruby over the last couple of years but never found the need or occasion to learn it. That's changed since I discovered Watir, which stands for Web Application Testing in Ruby. Watir functions a bit like HttpUnit in that it operates as a web client to a remote running web application.

The big difference is that it operates directly through your browser – in particular Internet Explorer (wouldn't it be great if it worked for Firefox as well). In practice, this means that as well as executing scripted tests, you also see the pages whizzing by as the browser navigates through them, something which may impress your cubicle colleagues.

HttpUnit on the other hand doesn't show any browser output. Because HttpUnit involves no browser interaction, simply operating at the HTTP level, its fine for web integration testing only up to a point. It wouldn't necessarily pick up something which is obvious to the naked eye, such as a style sheet error, which means you still need to do the manual page checks.

When you're sanity checking a web site, wouldn't it be great to have a tool which can automatically do all the mouse clicks and buttons to get through the login screens. I had spent quite a bit of time thinking about acquiring such a tool. Watir seems to be the answer. Even if you don't use the tool for testing, it can save you some finger damage. Of course, is a lot more than a glorified web GUI robot. You can use the full power of Ruby in setting up your test environment and writing assertions. The only pity for me is that the tool is not written in Java, which would make it easier to integrate into my existing development environment. Nonetheless, I see myself using this tool a lot, so I guess I had better start learning Ruby to make the most of it.

To get a flavour of how it works, its easiest to look at the packaged example showing how it does a Google search. The example is included in the watir_home/examples directory:

#includes:
require 'watir'   # the watir controller
include Watir

#variables:
testSite = 'http://www.google.com'

#open the IE browser
$ie = IE.new

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

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

puts "Step 2: enter 'pickaxe' in the search text field"
$ie.textField(: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 "
a = $ie.pageContainsText("Programming Ruby") 
if !a 
puts "Test Failed! Could not find: 'Programming Ruby'" 
else
puts "Test Passed. Found the test string: 'Programming Ruby'. Actual Results match Expected Results."
end

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

Pretty straightforward really. Strip away the puts lines and you'll see that the syntax is very compact, which is a real plus for this type of testing. Installation was a doddle – just follow the simple instructions on the user guide. Worked first time for me both times I tried.






Want to comment on this blog? Please email me at philzoio@realsolve.co.uk. At some stage I'll probably add a form, or maybe even a public forum, but for now I'm keeping the site pretty basic.