There tends to be a strong ongoing association between Agile and Open-Source automation tools today. Many make good inroads on the attempt to connect the elaboration of requirements with automated tests, step for step. This way, the test is performing as closely as possible, what the client actually asked for, or agreed in the elaboration process.
One such tool is Cucumber. It is designed to allow you to execute feature documentation written in plain text. In this article, I will detail a concise example, hopefully demonstrating the simplicity and effectiveness of this tool as well as value to the agile methodology.
Prerequisites: It is assumed that before using this example you have the following Installed:
• Ruby
• Watir gem for Ruby
• Cucumber gem for Ruby
I will use a simple example that uses the Google search engine in this case:
Let’s say the user story is something like:
‘When I go to the Google search page, and search for an item, I expect to see some reference to that item in the result summary.’
Cucumber uses the keywords “Given”, “When”, “And” “Then” to build this into useable syntax.
So I create for example, a file, called GoogleSearch.feature containing:
Feature:
‘When I go to the Google search page, and search for an item, I expect to see some reference to that item in the result summary.’
Scenario:
Given that I have gone to the Google page
When I add “cats” to the search box
And click the Search Button
Then “cats” should be mentioned in the results
Scenario:
Given that I have gone to the Google page
When I add “dogs” to the search box
And click the Search Button
Then “dogs” should be mentioned in the results
Now I create a file GoogleSearch.rb containing the following
require “rubygems”
require “watir”
Given /^that I have gone to the Google page$/ do
@browser=Watir::IE.new
@browser.goto(“www.google.com”)
end
When /^I add “(.*)” to the search box$/ do |item|
@browser.text_field(:name,”q”).set( item)
end
And /^click the Search Button$/ do
@browser.button(:name, “btnG”).click
end
Then /^”(.*)” should be mentioned in the results$/ do |item|
@browser.text.should include(item)
end
This gives us executable code for every Given, When and Then statement we have used.
To run it we issue the following command line
cucumber GoogleSearch.feature
or for a nice html formatted result
cucumber GoogleSearch.feature –format html –out result.html