As we have learned in the cucumber testing, feature files are created with the executable test scripts. The language, in which these executable test scripts are written, is known as Gherkin language.

Basically, Gherkin is a plain English text language used to interpret and execute the test scripts.

When Cucumber supports the English text, why do we need a separate Gherkin language?

It has been discussed several times that Cucumber supports plain English text, then why we need a separate Gherkin language. The answer to this lies in the concept of the BDD (Behavior Driven Development).

As we know, BDD involves various software behavior development possibilities while preparing test scripts. These can be development-related possibilities or business-related possibilities. In order to accomplish these possibilities, we need members from different communities like testers, developers, product owners, and project managers while developing test scripts.

Since these members do not belong to the same community; therefore, it is hardly possible to use the common language by each of them. Due to that, the concept of the test script is at high risk. In order to reduce this risk, Gherkin was developed.

Gherkin offers a common set of keywords in the plain English text, which can be used by members from different communities and can get the same output from the test scripts.

Gherkin offers the following specific keywords to write the common test scripts in the feature file:

  • Feature
  • Scenario
  • Given
  • When
  • Then
  • But
  • And
  • Background
Gherkin Language

Feature

Each feature file of Cucumber testing starts with a feature keyword. It is a standalone unit or functionality to be tested. For example, login feature, payment transfer feature, registration feature, etc.

Example:

Feature: Login 
Scenario: Login verification
Given user navigates to the website javatpoint.com
And user logs in through Login Window by using Username as "USER" and Password as "PASSWORD"
Then login must be successful.

Scenario

Each feature contains the required number of tests to test the feature. Each test is named as a Scenario.

For example, feature login functionality can contain two scenarios, first for a successful login and second for unsuccessful login.

Scenario: Successful Login with Valid entries
Given user navigates to the website javatpoint.com
And user logs in through Login Window by using Username as "USER" and Password as "PASSWORD"
Then login must be successful.

Scenario: Unsuccessful Login with Invalid entries
Given user navigates to the website javatpoint.com
And user logs in through Login Window by using Username as "USER" and Password as "1234erty"
But user entered wrong username and password
Then login must be unsuccessful.

Given

This keyword refers to the pre-condition of the test. For example, to access any web application, the first requirement or precondition is to navigate its home page, because, from the home page, we can navigate to other links such as signuplogin, etc.

Example:

Feature: Login 
Scenario: Login verification
Given user navigates to the website javatpoint.com
And user logs in through Login Window by using Username as "USER" and Password as "PASSWORD"
Then login must be successful.

When

It usually refers to the actions of a user that is to be executed.

Example:

Scenario: Successful Login with Valid entries
Given user navigates to the website javatpoint.com
When User Navigate to Login Page 
And user logs in through Login Window by using Username as "USER" and Password as "PASSWORD"
Then login must be successful.

When user fills "registration email textbox" with "nikita.sharma27@gmail.com".

Then

This keyword refers to the outcome of the previous step or upcoming action.

Example:

Scenario: Successful Login with Valid entries
Given user navigates to the website javatpoint.com
When User Navigate to Login Page 
And user logs in through Login Window by using Username as "USER" and Password as "PASSWORD"
Then login must be successful.

But

This keyword is used to add negative conditions.

Example:

Scenario: Unsuccessful Login with Invalid entries
Given user navigates to the website javatpoint.com
And user logs in through Login Window by using Username as "USER" and Password as "1234erty"
But user entered wrong password
Then login must be unsuccessful.

And

This keyword is used to add more conditions into your steps.

Example:

Given User is on Home Page
And Login Link is displayed
When User Navigates to Login form
And User enters email and Password
Then Login Successfully will be displayed 
And Logout Link should be displayed

Background

This keyword is used to define the steps that are common to all tests in the feature file. For example, Navigation to Home PageClick on the LoginEnter User Name and PasswordClick on Submit button are the common steps in almost all web applications.