Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Defining the behaviour of your web site and testing that it works

This example uses ... Capybara, Cumumber, Gherkin, RSpec, Selenium and Ruby ... yes, you'll need an explanation later on of what all of these are!

Have you ever updates something on a website, only to find that it the process you've broken something else?

I've been teaching Behaviour Driven Development of web sites in Ruby this week - well not strictly "Behaviour driven nor developemnt" because the objective of my customer is to describe the required behaviour of an existing website in a codified way, and then be able to check that it behaves as it's suppose to. Not only behaving as it's supposed to today, but also into the future as the web site is enhanced.

How does this work?

1. I define my behaviour using the Gherkin language - here's a definition of some behaviours I would like to see from the website I'm working on:

  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
  
  Given I ask for a quotation for IV14 5RD postcode
  When I visit our quotation web page
  Then I am told it is valid for Aberdeen


2. I run that through cucumber and it tells me where my tests are missing

  WomanWithCat:cc grahamellis$ cucumber website
  Feature: Checking some things on quotations
  
    Scenario: Looking for an onsite quote # website/hello_quotation.feature:5
      Given I ask for a quotation for CA5 1RJ postcode # website/hello_quotation.feature:6
      When I visit our quotation web page # website/hello_quotation.feature:7
      Then I am told it is valid for Whitehaven # website/hello_quotation.feature:8
      Given I ask for a quotation for IV14 5RD postcode # website/hello_quotation.feature:14
      When I visit our quotation web page # website/hello_quotation.feature:15
      Then I am told it is valid for Aberdeen # website/hello_quotation.feature:16
  
  1 scenario (1 undefined)
  6 steps (6 undefined)
  0m0.004s
  
  You can implement step definitions for undefined steps with these snippets:
  
  Given(/^I ask for a quotation for CA(\d+) (\d+)RJ postcode$/) do |arg1, arg2|
    pending # express the regexp above with the code you wish you had
  end
  
  When(/^I visit our quotation web page$/) do
    pending # express the regexp above with the code you wish you had
  end
  
  Then(/^I am told it is valid for Whitehaven$/) do
    pending # express the regexp above with the code you wish you had
  end
  
  Given(/^I ask for a quotation for IV(\d+) (\d+)RD postcode$/) do |arg1, arg2|
    pending # express the regexp above with the code you wish you had
  end
  
  Then(/^I am told it is valid for Aberdeen$/) do
    pending # express the regexp above with the code you wish you had
  end
  
  If you want snippets in a different programming language,
  just make sure a file with the appropriate file extension
  exists where cucumber looks for step definitions.
  
  WomanWithCat:cc grahamellis$


3. I copy and paste the suggested code template into a step definition file, and complete the tests:

  require 'selenium-webdriver'
  require 'capybara/cucumber'
  require 'rspec/expectations'
  Capybara.default_driver = :selenium
  
  Given(/^I ask for a quotation for (.*) postcode$/) do |arg1|
          @postcode = arg1
          visit("http://www.wellho.net")
  end
  
  When(/^I visit our quotation web page$/) do
          fill_in('where', :with => @postcode)
          click_button('Go!')
  end
  
  Then(/^I am told it is valid for (.*)$/) do |arg1|
          expect(page.has_text? arg1).to eq true
  end


4. I then run my "cucumber" again to see if it works. Now that I have steps defined, the selenium drivers I have in my source are going to act as a puppeteer to control Firefox - and thus the website is tested. Here's a sample output:



Reading that output (from an extended version wit four rather than two sets of data), I can see that four tests have passed and one has - intentionally - failed. Testing does require that - from time to time - you make things go wrong to check that it's really doing the noral tests correctly!




Gherkin source code - [here]
Ruby steps using Selenium - [here]
Alternative steps using Watir - [here]
(written 2015-05-30)

 
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
  [3426] Automed web site testing scripted in Ruby using watir-webdriver - (2011-09-09)
  [4387] Regression Testing my website - Cucumber and Watir - (2015-01-07)
  [4389] Cucumber example - test::unit, scenario outlines, datafile driven test - (2015-01-09)


Back to
The TransWilts Community Intergrated Transport Corridor
Previous and next
or
Horse's mouth home
Forward to
Reading and parsing a JSON object in Ruby
Some other Articles
Regular Expressions for the petrified - in Ruby
Where does Ruby load modules from, and how to load from current directory
Separating your code for easier testing, understanding and re-use; example in Ruby
Reading and parsing a JSON object in Ruby
Defining the behaviour of your web site and testing that it works
The TransWilts Community Intergrated Transport Corridor
Significant work - beyond helloworld in Ruby
Ruby - where one statement ends and the next begins
Around the world from Melksham
Sunday train times - 17th May to 6th September 2015
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., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/4501_Def ... works.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb