Monday, August 27, 2012

Cucumber

Cucumber is an open-source test automation framework for BDD (Behaviour-Driven Development). Cucumber lets software development teams describe how software should behave in plain text. The text is written in business-readable domain-specific language (Gherkin) and serves as documentation, automated tests and development-aid. It supports test scripting in Ruby, Java, C#, Python, PHP, Perl ..

Cucumber is an excellent test automation framework / tool together with e.g. Watir. Cucumber scripts serves as documentation for the whole team and they also helps developer to quickly see that they have implemented everything and it works as expected. Cucumber also has good logging / reporting functionality.




 

Installation


  1. Download Ruby installer from http://www.rubyinstaller.org and run it. During installation select Ruby to be added to PATH and to associate .rb files to this Ruby installation.
  2. Download Ruby Devkit from http://www.rubyinstaller.org and extract it. In the extracted folder, run commands ruby dk.rb init and ruby dk.rb install to setup and install it.
  3. Install Cucumber gem with command: gem install cucumber
  4. Install Syntax gem with command: gem install syntax

Cucumber scripts usually consists of three files - feature, step definitions and support files. Feature file contains the feature description. Step definitions contains the actual test script code. For each line in the feature file there should be a matching step definition in the step definitions file. The support files can contain code for ramp-up, tear-down and hooks for something to be done before / after steps / scenarios.


How to run scripts

 

By running following command:  
cucumber features (--out <filename>.html --format html)


Example script - feature file - cucumber.feature

 


Feature: Google search
  In order to find Cucumber homepage using Google search
  As an end user
  I need to enter a correct keyword in the search field
   
  Background:
    Given Google search page
 
  Scenario: Search for Cucumber   
   Given keywords ‘Cucumber’ in search field
    When I submit form    
    Then a link to Cucumbers’s homepage is found

  

Example script - step definitions file - cucumber.rb

 

Given /^Google search page$/ do
  $browser.goto ‘http://www.google.com’
end
Given /^keyword ‘Cucumber’ in search field$/ do
  $browser.text_field(:name, "q").set 'Cucumber'
end
When /^I submit form$/ do
  $browser.button(:id, ‘gbqfb’).click
end
Then /^a link to Cucumber’s homepage is found$/ do
  Watir::Wait.until { $browser.text.include? 'Making BDD fun‘ }
end


Example script - support file - env.rb

 


require 'rubygems'
require 'watir-webdriver'

$browser = Watir::Browser.new(:firefox)

at_exit do
  $browser.close
end


More information

  


http://cukes.info

No comments:

Post a Comment