Gherkin BDD 

Gherkin bdd language is a domain-specific language that is used to define tests in Cucumber. It uses a set of special keywords to write a behavior description. It promotes behavior-driven development because it allows developers, testers, business analysts, project owners, and other stakeholders involved in the project to understand the requirements of the project, which actually written in a simple text. Gherkin also provides scripts for test automation and supports dozens of languages.

It basically serves two purposes:

  1. Creating a project’s documentation
  2. Writing automated tests in BDD format.


Gherkin BDD Syntax

Feature: Title of the Scenario

Given [Preconditions or Initial Context]

When [Event or Trigger]

Then [Expected output]

Gherkin Example

Feature:  Login to Gmail account.

Given:  User have account in Gmail.

And User is in the login page of Gmail

When: User enter username as username.

And User enter the password as the password

Then User should be redirected to the inbox page of Gmail


Gherkin Keywords

Keywords used in Gherkin are used to define complete test cases including pre-condition, test conditions, expected outcome, etc. Here are some of the common keywords used in gherkin language:

  1. Feature.
  2. Scenario
  3. Given, When, Then, And, But
  4. Background.
  5. Scenario outline and Examples

Each keyword has its own meaning used in writing a Gherkin test. Let us understand each one of them in detail.



Each Gherkin file starts with a keyword ‘Feature’ and the feature description. The Feature is followed by :(colon) and an added space, and then there is a short text that describes the feature. The description could be multiple and should be added one after another. But only first-line description is considered during runtime, however others are available in reporting.

Feature: Test the login functionality
Test forget password
Test portal image

The feature description provides a high-level description of a software feature to be tested. The feature keyword is used to group multiple scenarios that are related to each other. Each feature file should have only one feature. The file is saved with extension .feature.



The scenario consists of a list of steps, which starts with the keyword Given. Learn what is Test Scenario.?

Other keywords are  When, Then, But & And. The feature file may have multiple scenarios, and each scenario starts with Scenario: followed by the scenario name. Each scenario contains one or more than one step. 

Feature: Test the login functionality

Scenario: Search tickets

Given user is in the IRCTC Home page
When user searches train by entering the following details

| From | To | Date |
| YESVANTPUR JN - YPR | DURG - DURG | 31-01-2020 |

 Then ticket availability details get display

Best practices for creating a scenario

  • Each scenario should be able to be executed independently without any dependency on any other scenario.
  • Scenarios should be simpler and easier to understand.
  • You can run just a subset of your scenarios and you do not have to worry about the breaking of your test set.
  • Scenarios should be able to run the tests in parallel thereby reducing the amount of time taken to execute all the tests.



The Given keyword is used to describe the preconditions that should be meet before interacting with the system. It also describes about the system state before performing the actions. It is possible to have multiple given conditions.

Given user is in the login page



When keywords describe the action steps to be performed. When conditions can also be multiple. However, it is always recommended to have at least one when step for each scenario.

When user enter a username 
When user enters a password



Then keywords describe the expected result or outcomes. This is where expected behavior of the system is described so that it can be compared to how the software actually performs i.e. actual behavior.

Then user gets successfully logged in to Facebook account


And, But

This is used when there is multiple Given When Then statements

Given user is in the Gmail login page
And user has Gmail account
When user provides a username
And user provides a password
Then user successfully logged in to Gmail account
But user does not see password details in Gmail


Scenario outline and Examples:

The keyword Scenario Outline is used to run the same scenario multiple times with different sets of values. It contains an ‘Examples’ section through which execution is possible. The scenarios that need to be tested with different parameters can be combined together and form Scenario outline. The scenario is expressed by using < > (delimited parameters).

The Scenario Outline is run once for each row in the Examples section beneath. it should be noted that it does not count the first header row.

Feature: Test the login functionality

Scenario Outline: Login with valid username and password

Given user is in the login page
When user provides “<username>” and “<password>”
Then user should be successfully navigated to home page


| username | password |
|   user  | password |
|   user  | password123 |
|   user  | [email protected] |



The Given steps in the different scenarios if are repeated, then it can be grouped together under a Background section. A Background helps to add more context to the scenarios in the feature. Background can also contain one or multiple Given steps.

A Background is executed before every, thus it needs to be put before the first Scenario in the feature file. It is important to note that there can be an only a single set of Background steps for one feature.

Feature: Test the login functionality

Background: User has an account on Facebook
Scenario: Login with valid username and password

Given user is in the login page
When user provides username and password
Then user should be successfully navigated to home page