Cucumber Basics Tutorial

In every cucumber project , first of all there should be root folder which will be a placeholder for all the cucumber files.

For every cucumber project there is a single directory at the root of the project named "features". This is where all of your cucumber features will reside. In this directory you will find additional directories, which is step_definition and support directories

What is "Feature File"?

Feature File consist of following components -
  • Feature: A feature would describe the current test script which has to be executed.
  • Scenario: Scenario describes the steps and expected outcome for a particular test case.
  • Scenario Outline: Same scenario can be executed for multiple sets of data using scenario outline. The data is provided by a tabular structure separated by (I I).
  • Given: It specifies the context of the text to be executed. By using datatables "Given", step can also be parameterized.
  • When: "When" specifies the test action that has to performed
  • Then: The expected outcome of the test can be represented by "Then"
Feature: Searching Facebook on Google
Scenario : Discovering facebook site by google search
Given: I am on
When: I search 'facebook' and click on Facebook Link in search Results

Then: I should see home page

What is "Step Definition"

Step definition maps the test case steps in the feature files    (introduced by Given/When/Then) to code, which executes and checks the outcomes from the system under test. For a step definition to be executed, it must match the given compoent in a feature. Step definition is defined in ruby files under "features/step_definitions/*_steps.rb".
Example for Step Definition: Here we will above example of browsing . We will be using the Capybara automation Libararies API.See here for more details.

 We will use features like "When, Then, Given "
Step 1:
Given (/^ I am on$/) do
   visit ""
Step 2:
When (/^ I search 'facebook' and click on Facebook Link in search Results$/) do

  fill_in('gs_htif0', :with => '')#id/name may vary 
  click_button('gbqfba') #click search button
  click_link('Welcome to Facebook - Log In, Sign Up or Learn More') #link text
Step 3:
Then (/^ I should see facebook home page$/) do

  expect(page).to have_title "Welcome to Facebook - Log In, Sign Up or Learn More"

  • You need 2 Files – Features and Step Definition to execute a Cucmber test scenario
  • Features file contain high level description of the test scenario in simple language
  • Steps Definition file contains the actual code to execute the test scenario in the Features file.


Popular posts from this blog

Testing Webservice with SoapUI Having Windows Authentication

Data Driven Testing Using Selenium -WebDriver in Python

How to Re-run Failed Scenarios in Cucumber