Cucumber with Java – Behavior Driven Development

About Cucumber :

Cucumber is a testing tool that supports Behavior Driven Development (BDD) framework. It defines application behavior using simple English text, defined by a language called Gherkin. Cucumber can be defined as a testing framework, driven by plain English text. It serves as documentation, automated tests, and a development aid – all in one.

Advantages of Cucumber over other tools :

  1. Cucumber supports different languages like and Ruby.
  2. It acts as a bridge between the business and technical language. We can accomplish this by creating a test case in plain English text called feature file.
  3. It allows the test script to be written without knowledge of any code, it allows the involvement of non-programmers as well.
  4. It serves the purpose of end-to-end test framework unlike other tools.
  5. Due to simple test script architecture, Cucumber provides code re usability.

Feature file:

Each independent functionality of the product under test can be termed as a feature when we talk about Cucumber.

A simple feature file consists of the following keywords/parts :

  1. Feature : Name of the feature under test.
  2. Description (optional) : Describe about feature under test.
  3. Scenario : What is the test scenario.
  4. Given : Prerequisite before the test steps get executed.
  5. When : Specific condition which should match in order to execute the next step.
  6. Then : What should happen if the condition mentioned in When is satisfied.
  7. And & But : It can be used with any other keywords like Given, When and Then.
  8. “”” : It represent the Doc Strings.
  9. | : It represents Data Tables.
  10. @ : It represents Tags/Labels to group Scenarios.
  11. <> : It represents placeholder.
  12.  # : It represents Comments.

Cucumber has got the following few annotations:

Given: It defines the prerequisite for the test to be executed

  • Example:
    GIVEN I am at google search page

When: It defines the trigger point for any test scenario execution.

  • Example:
    WHEN I enter “<search-text>”

Then: It holds the expected result for the test to be executed.

  • Example:
    THEN search result should be successfully listed.

And: It provides the logical AND condition between any two statements. AND can be used in conjunction with GIVEN, WHEN and THEN statement.

  • Example:
    WHEN I enter “<search-text>” AND I clicked on “<search button>”

But: It signifies logical OR condition between any two statements. OR can be used in conjunction with GIVEN, WHEN and THEN statement.

  • Example:
    THEN search should be successful. BUT search home page should not be missing

Scenario: It defines the details of the test scenario.

  • Example:
    Given I am a word press blog user
    WHEN I enter blog name in search-text
    AND I click on search button
    Then Search blog page should be opened

Background: It defines the common instructions that will be executed before each                                           scenario runs.

  • Example :
    Background Go to word-press blog page

Scenario outline: It replaces variable/keywords with the value from the table. Each row in                                      the table is considered to be a scenario.

  • Example :
    Scenario Outline: Search available Abhinav Blogs
    When I enter ‘<blogValue>’ in the search text
    And I clicked on search action
    Then ‘<blogName>’ should be opened

| blogValue        | blogName                           |
| TDD              | Software development Process : TDD and BDD |
| Design Pattern   | Chain of responsibility design pattern     |
| Git              | Git commands                               |
| Date             | Date Format Utility with ThreadLocal   |

The example of feature file to test the scenario of searching the given blog in word press:


#Author: Abhinav rana
#Feature Definition for searching available blogs

Feature: Blog Search

Background: User navigates to wordpress site
Given I am on Abhinav Rana blog site

Scenario: Search TDD and BDD blog
When I enter &amp;quot;TDD&amp;quot; in the search text
And I clicked on search action
Then &amp;quot;Software development Process : TDD and BDD&amp;quot; blog should be opened

Scenario: Search Design pattern blog
When I enter &amp;quot;design pattern&amp;quot; in the search text
And I clicked on search action
Then &amp;quot;Chain of Responsibility Design Pattern&amp;quot; blog should be opened

Scenario: Search Git command blog
When I enter &amp;quot;Git&amp;quot; in the search text
And I clicked on search action
Then &amp;quot;Git commands&amp;quot; blog should be opened

Scenario: Search Design pattern blog
When I enter &amp;quot;Date&amp;quot; in the search text
And I clicked on search action
Then &amp;quot;Date Format Utility with ThreadLocal&amp;quot; blog should be opened

&amp;lt;p style=&amp;quot;text-align: justify;&amp;quot;&amp;gt;

Step Definitions file:

Steps definition file stores the mapping between each step of the scenario defined in the feature file with a code of function to be executed.

The example of step definition or glue file for the above given feature file:

package com.abhi.cucumber.glue;

import junit.framework.Assert;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import cucumber.annotation.en.And;
import cucumber.annotation.en.Given;
import cucumber.annotation.en.Then;
import cucumber.annotation.en.When;

public class BlogSearch {

WebDriver webDriver = null;

@Given(&amp;quot;^I am on Abhinav Rana blog site$&amp;quot;)
public void I_am_on_Abhinav_Rana_blog_site() throws Throwable {
webDriver = new ChromeDriver();

@When(&amp;quot;^I enter \&amp;quot;([^\&amp;quot;]*)\&amp;quot; in the search text$&amp;quot;)
public void I_enter_in_the_search_text(String searchTxt) throws Throwable {

@And(&amp;quot;^I clicked on search action$&amp;quot;)
public void I_clicked_on_search_action() throws Throwable {

@Then(&amp;quot;^\&amp;quot;([^\&amp;quot;]*)\&amp;quot; blog should be opened$&amp;quot;)
public void blog_should_be_opened(String arg1) throws Throwable {

Assert.assertEquals(&amp;quot;Blog Title Matched&amp;quot;, webDriver.findElement(By.partialLinkText(arg1)).getText(), arg1);


The example of Runnable file for the above given feature and its glue code is:

package com.abhi.cucumber.runner;

import org.junit.runner.RunWith;

import cucumber.junit.Cucumber;

* @author Abhinav

@Cucumber.Options(format = {&quot;pretty&quot;,&quot;html:target/cucumber&quot;},
features = {&quot;com/abhi/cucumber/feature&quot;},glue = {&quot;com.abhi.cucumber.glue&quot;})
public class BlogSearchRunner {


You can checkout/clone the cucumber example repository from the mentioned path –


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s