Gherkin Domain Specific Language (DSL)
This section will cover details about Gherkin, it’s keywords and how to use them in feature files.
What is Gherkin Language?
Gherkin is a line-oriented language, like as Python and YAML, It uses indentation to define structure. Line endings terminate statements, Either spaces or tabs may be use for indentation. Most lines start with a keyword.
Conventions of Gherkin Language
-
Single Gherkin source file contains a description of a single feature.
-
Source files will have .feature extension.
Gherkin Keywords
-
Feature
-
Example
-
Rule (as of Gherkin 6)
-
Scenario or Example
-
Given
-
When
-
Then
-
And
-
But
-
Background
-
Scenario Outline or Scenario Template
-
Examples
Others keywords
-
|
(Data Tables) -
@
(Tags) -
#
(Comments)
Example of Gherkin feature file
Feature: Here testing all keyword which we used in our feature File
Background:
Given cleaning the database
And initializing the database
Scenario: This is basic scenario
Given put input here
When the input processing
Then i got the result
Scenario Outline: Login functionality
Given going to the website
And click on login button
When user logs in using Username as <username> and Password as <password>
Then login should be successful
But the dashboard not found
Examples:
|username |password|
|Tom |password1|
|Harry |password2|
|Jerry |password3|
Gherkin Keywords description.
Above we listed all keywords which are using in Gherkin feature files. Now lets details about them.
Feature
Feature keyword gives information about the high level business functionality and the purpose of activities under this test. Everybody should be able to understand the intent of feature file by reading the first Feature step. This part basically kept brief.
Scenario or Example
Basically a scenario represents a particular functionality which is under test. By seeing the scenario user should be able to understand the intent behind the scenario and what the test is all about. Each scenario should follow given, when and then format.
-
Background : Whenever any action/task required to perform in each scenario, those things needs to be placed in Background. For Instance: If user needs to clear database before each scenario then database clear task/action can be put in background.
-
Given : As mentioned above, given specifies the pre-conditions. It is basically a known state.
-
When : This is used when some action is to be performed. As in above example we have seen when the user tries to log in using username and password, it becomes an action.
-
Then : The expected outcome or result should be placed here. For Instance: verify the login is successful, successful page navigation.
-
And : And is used to combine two or more same type of action. We can use it instead of Given.
-
But : We can use it instead of Then.
Scenario Outline or Scenario Template
Scenario outlines are using when same test has to be performed with different data set. Such as we have to test login functionality with multiple different set of username and password.
Examples
Examples is data set of Scenario Outline.