Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
Regression Testing my website - Cucumber and Watir

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]
(written 2015-01-07)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
R222 - Ruby - Cucumber / web site testing with watir webdrivers and capybara
  [4501] Defining the behaviour of your web site and testing that it works - (2015-05-30)
  [4389] Cucumber example - test::unit, scenario outlines, datafile driven test - (2015-01-09)
  [3426] Automed web site testing scripted in Ruby using watir-webdriver - (2011-09-09)

Q456 - Object Orientation and General technical topics - Test Driven Development and Behaviour Driven Development
  [4652] Testing new algorithms in PHP - (2016-02-20)
  [4634] Regression testing - via a very short C testing framework - (2016-01-29)
  [4542] The principle of mocking - and the Python Mock package - (2015-10-17)
  [4457] Test framework for TCL - Tcltest - some examples - (2015-03-11)
  [4380] Behaviour Driven Development / Ruby and Cucumber - (2015-01-02)
  [4374] Test driven development, and class design, from first principles (using C++) - (2014-12-30)
  [4346] A behaviour driven example of writing a Java program - (2014-12-09)
  [4336] Test Driven Development - a first example of principle in C - (2014-12-01)
  [4326] Learning to program - comments, documentation and test code - (2014-11-22)


Back to
Guide exercise to help you learn Gherkin, Cucumber and Rspec
Previous and next
or
Horse's mouth home
Forward to
Global Regular Expression matching in Ruby (using scan)
Some other Articles
Refactoring Perl applications to give them a rosy future
Checking MySQL database backups have worked (not failed)
Global Regular Expression matching in Ruby (using scan)
Regression Testing my website - Cucumber and Watir
Guide exercise to help you learn Gherkin, Cucumber and Rspec
A booking that looks too good to be true? It probably is too good to be true!
Installing Cucumber on Ubuntu - cannot load such file -- mkmf (LoadError) message
Improved test in Cucumber with RSpec
Second step Cucumber and Gherkin - beyond Hello World
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/4387_Reg ... Watir.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat