Cucumber Tutorial
Gherkin Keywords
What Are Gherkin Keywords In Feature File ?
In cucumber feature file, we notice some colored words. These words are Ghekin keywords and each keyword holds a meaning. Below is the list of Gherkin keywords :
Primary Keywords -
Feature
Scenario/Example
Given
When
Then
Background
And
But
Scenario Outlines/Scenario Template
*
Secondary Keywords -
" " "
|
@
#
Feature
It is a starting point of a Gherkin file that defines the logical test functionality you will test in your feature file. This keyword provides description of a software feature.
e.g.
Feature : Find the odd one
The numbers are related to each other in some logic except the one number.
Find it and the task is over
Here, the text comes after the keyword feature is considered as the feature description.
Scenario
This keyword is used to define a particular test within feature. In feature each scenario defines different test and feature contains a number of scenarios.
e.g.
Feature : User account test
This will test the functionality of user acoount
Scenario : Login to user account and validate their credentials
Scenario : Search a product and add it to the bag
Given, When, Then
'Given' defines precondition to the test that descibes the initial context of the test and 'When' keyword describes an event or an action whereas 'Then' describes the expected outcome.
e.g.
Feature : User account test
This will test the functionality of user acoount
Scenario : Login and Logout to user account
Given User is on Home Page
When User is Logged In
Then User is Logged Out
Background
If same steps are repeating in all the scenarios that means they are commom in all, then 'Background' keyword is used to define these steps.
e.g.
Feature : Add product to bag
This will test the functionality of adding products to the user wishlist.
Background :
Given User is on Home Page
When User is logged In
Scenario : Add product in bag
GIven Search a product
When Select the third product in serach result
Then Add to the bag
And, But
And and But are used to add conditions to the steps. The difference between these two is that 'And' adds positive conditions while 'But' adds negative conditions.
e.g.
Feature : User account credentials test
This will validate the user credentials
Scenario :Validate user email address and password
Given User is on Home Page
When User navigate to login page
And User enters email address and password
But Password is wrong
Then Message displayed Enter valid details
Scenario Outlines
This keyword is used to run the same scenario multiple times with different set of values. We can run a single scenario on different-different combinations of values by passing the values in parameter.
e.g.
Feature : Scenario outline example
Scenario Outline : Distance from school to library
Given The school is <final> km far away to library
When A student covers <covered>km distance
Then <left>km distance is left to be covered
Examples :
| final | covered | left |
| 1 | 0.4 | 0.6 |
| 1.3| 1.1 | 0.2 |
Here, we can see parameter can be declared within <> and their values are mentioned within Examples keyword.
* : Keywords
* can be used at the place of any normal step keywords. The purpose of * is to express the list of steps effectively if the list has much steps.
e.g.
Feature : User account test
This will test the functionality of user acoount
Scenario : Login and Logout to user account
* User is on Home Page
* User is Logged In
* User is Logged Out