Main Content

Automed web site testing scripted in Ruby using watir-webdriver

Archive - Originally posted on "The Horse's Mouth" - 2011-09-09 07:10:23 - Graham Ellis

How do you test your web site? With a visit to a few pages, or by waiting for customer feedback if something's not working? That's a very risky approach, and what you really need is a planned and systematic testing regime, with unit tests that must be passed (but will flag fails) run on a regular basis.

There are a number of tools that use Ruby scripting to describe and run tests. Both Selinium and Watir are well know names, and you can also use Cucumber on top of Watir's Webdriver to describe your tests in an English-like way that will be good for your customer to understand. At the end of yesterday's Learning to program in Ruby course, we used the watir-webdriver to automate browsing and test some of the results - choosing the Well House Consultants quotation page to check out.

I wrote my tests into a hash:

  quote_checks = {'Armagh' => "Northern Ireland",
    'E18 4RT' => "London - East",
    "Cumbria" => "Carlisle",
    'SN12 7NY' => "Well House Manor",
    '66554' => "Kansas"}


That's five tests of the onsite quote system, with a check to be made that the location has been identified correctly.

I created a browser object, and this starts Firefox in the background, which is then driven by the watir-webdriver for the rest of the tests.

  browser = Watir::Browser.new

Finally, I drive my browser to pages, fill in forms, submit them, and look at the results

  browser.goto 'www.wellho.net/index.html'
  browser.text_field(:name => 'where').set location
  browser.button(:value => 'Go!').click
  if browser.text =~ /Venue requested:(.*)/


The rest of code is regular ruby - you can find the complete source used to write this article [here].

Screen output, when run:

  munchkin:rs grahamellis$ irb
  >> require 'onsite'
  Testing On site quotation pages from home page
  
  Passed - 66554
  FAILED - 002 [Venue text not matched] Cumbria
  Passed - E18 4RT
  Passed - SN12 7NY
  Passed - Armagh
  
  Passed 4
  FAILED 1
  (Total 5)
  >>


With futher output avaialble in a log file. ([sample])


I installed the watir-webdriver on my Mac using
  sudo gem install watir-webdriver

You may link to our home page (the one on which this script fills in the form to ask for a quotation) [here], and the quotation generated for E18 - the one that's pictured above - is [here]

Testing is important! and with facilities like Cucumber, Watir, and Selenium, you can automate much of this work, with the programatic elements giving you just a whispered background commentary as tests tun correctly and then shouting at you when something doesn't work as specified / required / predicted.