September 5, 2016


Cucumber - Automating scenarios and behaviors

As we all know that Behavior Driven Development is an agile software development technique which helps in building functionality incrementally as per its expected behavior. It is like describing behavior in a business understandable language. It is an extension of TDD.

Complex scenarios are required to be understood by all the stakeholders like BA’s, developers, QAs etc to ensure that the product is built right. BDD helps in preventing the gaps which appear between the stakeholders and it takes into account the real world scenarios.

There are various tools which support BDD like Cucumber, JBehave, FitNesse, SpecFlow etc. In this blog we are going to focus on Cucumber.

Cucumber is a free open source BDD tool which is written in Ruby programming language that runs automated tests. It keeps specifications, automated test scripts, and documentation at the same place. For a complex project, the team first writes the examples and then run them in Cucumber. Cucumber further tells you which ones are working and which ones aren’t. It lets us define application behavior in plain descriptive/meaningful English text using a simple grammar defined by a language called Gherkin. It can be used to “test” the code written in Ruby or other languages.

Cucumber makes everyone in the team to be on the same page, examples and scenarios are based on the behavior and client expectations, hence making it a lot easier for anyone in the team to understand.

Keywords used in Cucumber tests:

An example of Cucumber test is:

Once Cucumber is installed, follow the below steps to create your tests in Eclipse:

  1. Create folder structure by creating a package in Eclipse and then create folders in src
  2. Let’s create a test for the login page. Create a class and write a simple Selenium script for opening the browser, locating username and password fields, and also entering correct values in them, thereby clicking ‘Submit’ button in the end, followed by closing the browser and quitting the driver.
  3. Create a feature file (it can be a simple file in Eclipse) inside a new feature folder having .feature as extension in order to detect the test automatically

    Feature: Log in to your email account

    The sign up should be quick and friendly.

    Scenario: Successful log in to the email account

    Given user already exists for the email account

    When user navigates to login page

    And user enters username and password

    Then he should see a welcome message with user’s name in it

  4. In order to run this test, create a JUnit Test Runner class using annotation runwith.
  5. Click on Run button on Eclipse to run it or simply right click on JUnit class and run it as JUnit Test Application.

Programs can have more complex nature, however the tests appear quite readable and simple. The tests are clear and everyone in the team can understand the test along with their expected behaviors.

Reference Links:

For Cucumber installation refer -