Main Content

Regression Testing my website - Cucumber and Watir

Archive - Originally posted on "The Horse's Mouth" - 2015-01-07 22:27:24 - Graham Ellis

Problem - I update my website to make a change and in doing so break something that's worked for years.
Frequency of occurence - All too often!

And as my website grows over the months and years, so the changes of a change breaking something from the past increases, and so the need for testing increases. With all the old things tested as well as the new, and where a new bug is found that's not been spotted by any of the test so far, another test has to be added to the list of what needs to be checked. All rather frightening

Solution There are automated test tools to help you.

Watir-webdriver lets you control, from a ruby program, automated browsing session. Although you're writing the tests in Ruby, the langauge of any web applications you're testing doesn't matter - and indeed you can simply test web pages and content if you want.

Cucumber lets you specify the behaviour of your page - its headline is "Behaviour Driven Developemnt" but although you're encouraged to describe the behaviour first, in practise you'll often describe the behaviour based on an existing web page or application.

Let's see an example ... I'm going to define the features I want to test - let's say it's my quotation page and I want to check that it's picking up the correct locations from the data entered onto a form:

  Feature: Checking some things on quotations
  
  Scenario: Looking for an onsite quote
  Given I ask for a quotation for CA5 1RJ postcode
  When I visit our quotation web page
  Then I am told it is valid for Whitehaven
  
  Scenario: Looking for an onsite quote
  Given I ask for a quotation for 23456 postcode
  When I visit our quotation web page
  Then I am told it is valid for Virginia
  
  Scenario: Looking for an onsite quote
  Given I ask for a quotation for France postcode
  When I visit our quotation web page
  Then I am told it is valid for Paris
  WomanWithCat:s2015 grahamellis$


When I first run cucumber against this feature file (which is written in Gherkin), it complains that the steps aren't defined, and it provides sample code that you can cut and paste into a step definition file. You then add your code which implememts the tests.

  require 'watir-webdriver'
  
  Given(/^I ask for a quotation for (.*) postcode$/) do |arg1|
    @b = Watir::Browser.new
    @b.goto 'www.wellho.net/'
    @postcode = arg1
  end
  
  When(/^I visit our quotation web page$/) do
    @b.text_field(:name => 'where').set @postcode
    @b.button(:value => 'Go!').click
  end
  
  Then(/^I am told it is valid for (.*)$/) do |arg1|
    expect(@b.text.include? arg1).to eq true
    @b.close
  end


This is where watir comes in - it's a library that controls Firefox (can be some other browsers) like a puppeteer, telling it where to go, how to fill in forms, and when buttons should be clicked on. You'll see it browsing on the screem, and it will also produce test output. If it's functioning correctly, my example gives:

  WomanWithCat:s2015 grahamellis$ cucumber wwwWellhoNet/
  Feature: Checking some things on quotations
  
    Scenario: Looking for an onsite quote # wwwWellhoNet/quotation.feature:3
      Given I ask for a quotation for CA5 1RJ postcode # wwwWellhoNet/step_definitions/quotation_steps.rb:5
      When I visit our quotation web page # wwwWellhoNet/step_definitions/quotation_steps.rb:11
      Then I am told it is valid for Whitehaven # wwwWellhoNet/step_definitions/quotation_steps.rb:16
  
    Scenario: Looking for an onsite quote # wwwWellhoNet/quotation.feature:8
      Given I ask for a quotation for 23456 postcode # wwwWellhoNet/step_definitions/quotation_steps.rb:5
      When I visit our quotation web page # wwwWellhoNet/step_definitions/quotation_steps.rb:11
      Then I am told it is valid for Virginia # wwwWellhoNet/step_definitions/quotation_steps.rb:16
  
    Scenario: Looking for an onsite quote # wwwWellhoNet/quotation.feature:13
      Given I ask for a quotation for France postcode # wwwWellhoNet/step_definitions/quotation_steps.rb:5
      When I visit our quotation web page # wwwWellhoNet/step_definitions/quotation_steps.rb:11
      Then I am told it is valid for Paris # wwwWellhoNet/step_definitions/quotation_steps.rb:16
  
  3 scenarios (3 passed)
  9 steps (9 passed)
  0m41.351s
  You have new mail in /var/mail/grahamellis
  WomanWithCat:s2015 grahamellis$


See Watir Browser class documentation.

Feature file - [here]
Step definition file - [here]