Gherkin is a domain particular language which helps you to describe business behaviour without any need for detail implementation. The text acts as documentation and skeleton of our automated tests.
This script has two main purposes:
- Documents user scenarios
- Writing automated tests (BDD)
Gherkin is line-oriented language like YAML and Python. Here every line is called as step and starts with a keyword and end terminals with stop. In this syntax the comment can be added anywhere you want, but it will start with a # sign reads each line when gherkin’s keywords is removed as given, when, then etc.
Example the script is written as:
Feature: Title of the Scenario Given [Preconditions or Initial Context] When [Event or Trigger] Then [Expected output]
The important terms of gherkin are:
- Scenario outline examples
The file has an extension .feature and each feature file must have only one feature the feature keyword starting with Feature and after that add, a space and name of the feature is written.
The feature file may have many scenarios will start with Scenario: followed by scenario name.
Background keyword helps us to add some content to the scenario, but the scenario can be included in the background and the difference is that it should run before scenario.
The main usage of this keyword is to put the system in a familiar state before user interaction starts with the system.
Given - a test step that defines the 'content’
Given I am on "/."
This step is to define action performed by user.
When I perform “Sign In.”
This keyword is to see the outcome after the action in when step.
Then - test step that defines the 'outcome.'
Then I should see "Welcome Tom."
And and But:
Used when we use multiple given, then and when commands. All these are test steps and can be used interchangeably
A But - additional test step which defines the 'action' 'outcome.'
But I should see "Welcome Tom."
And - additional test step that defines the 'action' performed
And I write "Email Address" with "Kimjones@gmail.com."
Example of Gherkin is:
Feature: User Authentication Background:
Given the user is already registered to the website Scenario:
Given the user is on the login page
When the user inputs the correct email address
And the user inputs the correct password
And the user clicks the Login button
Then the user should be authenticated
And user should be redirected to their dashboard
And the user should be presented hit message
Advantages of Gherkin:
- Gherkin is simple easy for non-programmers to understand
- Programmers can use it as a solid base for starting their tests
- By using gherkin user stories can be digested easily.
- The scripts of gherkin can be easily understood by business executives and developers.
- It targets the business requirements.
- A significant proportion of the functional specifications are used as user stories.
- Expertise is not necessary to understand the small Gherkin command set
- The Gherkin language links acceptance tests directly to automated tests.
- Style of writing tests is easier for code reusability in other tests
1. It needs a high level business engagements and collaborations.
2. It is not good or works well with all scenarios.
3. Test maintenance costs is increased due to poorly written tests