Showing posts with label cucumber. Show all posts
Showing posts with label cucumber. Show all posts

Friday, August 31, 2012

Cucumber more deeply ...

Cucumber's strength is the ability to write the feature descriptions in plain text and also possible in your native language. Feature is usually a user story. When all scenarios in a feature file is passing, it tells that the story is done. The format of the feature file is:

feature: <short description>

<full description>

background:

scenario:

...

scenario outline:

examples:

...


The feature file starts with a description of the feature. The format of the description is free, but usually e.g. following template is used:

In order to ...
as a ...
I need to ...

For example:

Feature: Application sign-in

In order to sign in
as an end user
I need to enter valid credentials

After the description comes the background. The background contains the preconditions for all scenarios. The format of the background is similar to the format of the scenarios, but the only difference is that the steps in the background will be executed before each scenario. It could for example, be like this:

Background:
   Given a registered user

Then comes the scenarios or scenario outlines. A scenario is like one test case. A scenario is made up of three steps: Given, When and Then.

Given is the precondition for the test case.
When is the action to be taken.
Then is the verification / what should happen.

For example:

Scenario: Successful sign-in
   Given sign-in page to the application
    When I enter valid credentials
     And click on the sign-in button
    Then I'm successfully logged in
     But no error text should be displayed

Notice that And or But can be given in any of these three steps.

The other possibility is to use a scenario outline. It could look like this:

Scenario outline: Application sign-in
   Given  sign-in page to the application
    When I enter username <username> and password <password>
     And click on the sign-in button
    Then I'm <loginstatus> logged in

Examples:
| username    | password   | loginstatus  |
| validuser   | validpwd   | successfully |
| invaliduser | validpwd   | not          |
| validuser   | invalidpwd | not          |

When running the feature, the scenario will be repeated with the values in each row in the examples table. So in this case the scenario will be repeated four times.

You could also have many examples tables for one scenario outline e.g.

Examples: Successful logins
| username    | password   | loginstatus  |
| validuser   | validpwd   | successfully |

Examples: Failed logins
| username    | password   | loginstatus  |
| invaliduser | validpwd   | not          |
| validuser   | invalidpwd | not          |

Now if you save your feature file and run it, you will get an output similar to this:

1 scenario (1 undefined)
5 steps (5 undefined) 

You can implement step definitions for undefined steps with these snippets: 

Given /^sign-in page to the application$/ do 
 pending 
end 

When /^I enter username validuser and password validpwd$/ do
 pending 
end

When /^I enter username invaliduser and password validpwd$/ do
 pending 
end

...

This tells that there are five missing step definitions i.e. you need to have matching step definitions for each line in the feature file in your step definition file. To get started with your step definitions file, copy the above pending step definitions to your step definitions file. The next thing I would do is to combine / group step definitions. As you can notice the step definitions:

When /^I enter username validuser and password validpwd$/ do
 pending 
end

When /^I enter username invaliduser and password validpwd$/ do
 pending 
end 

... are very close to each other. They could be combined to one step definition:

When /^I enter username (.*) and password (.*)$/ do |user,pwd|
end 

After this you need to insert the actual test script code in each step definition. It is also possible to call another step definition from within a step definition e.g.

When /^I enter username (.*) and password (.*)$/ do |user,pwd|
   step "I enter valid credentials"
end 

There are still a couple of things that you need to / is good to know - tags and hooks


Tags


In your feature file you can add tags. Tags are strings starting with @ e.g.

@highpriority
Scenario: ...

If you now wan't to run all scenarios tagged with @highpriority, you can do it like this:

cucumber --tags @highpriority

If you wan't to run all scenarios, except those ones tagged with @highpriority:

cucumber --tags ~@highpriority


Hooks

With the help of hooks you can run some specific script code in different places during the test runs. There are many predefined hooks available in Cucumber. Hooks are defined in files in the support folder, usually env.rb or hooks.rb.

To do something before starting to run any scenario, put that code at the top of the file. To do something when the run ends, use the at_exit hook. To run some code before or after a scenario, use the Before or After hooks. You can also execute some specific code after each step with the AfterStep hook. It is also possible to run different code if scenario passes or fails:

Before do
 # Do something before each scenario
end
 
After do |scenario|
   if(scenario.failed?)
      # Do something if scenario failed
   end
   if(scenario.passed?)
      # Do something if scenario passed
   end 
end 

Then there is also possibility to use tagged hooks i.e. just execute some code for tagged scenarios. For example.

After('@highpriority', '@mediumpriority') do
 # Do something after scenarios tagged with @highpriority OR @mediumpriority
end
 
Before('@highpriority, @mediumpriority') do 
 # Do something before scenarios tagged with @highpriority AND @mediumpriority
end
 

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