Last Updated On
In this tutorial, we will discuss the Gherkin.
Table of Contents
What is Gherkin?
Gherkin uses a set of special keywords to give structure and meaning to executable specifications. Each keyword is translated into many spoken languages; in this reference, we will use English. Gherkin serves multiple purposes:-
- Unambiguous executable specification
- Automated testing using Cucumber
- Document how the system actually behaves
Most lines in the Gherkin document start with one of the keywords. Comment lines are allowed anywhere in the file, starting with a hash sign (#).
Gherkin syntax is written in plain English (or other supported languages), making it understandable to business stakeholders who may not have a technical background.
What is a Feature File?
A file in which we store features, description of the features and scenarios to be tested.
Feature file consists of the below-mentioned keywords:-
1) Feature – It is used to provide a high-level description of test scenarios and to group related scenarios.
The first keyword in the Feature file is the Feature keyword, followed by: and short text that describes the feature.
2) Scenario – This describes the steps followed to test the scenario and the expected outcome of the scenario.
3) Scenario Outline – It is used to run the same scenario multiple times with different sets of values.
4) Background – Suppose we find that the Given steps are the same for all Scenarios in a feature file. Then we should use Background. We can move these Given steps under the Background section.
5) Steps – Each step starts with Given, When, Then, And or But. Cucumber executes each step in a scenario one at a time, in the sequence they have mentioned in the scenario. When Cucumber tries to execute a step, it looks for a matching step definition to execute.
- Given – It is used to describe the initial context of the scenario. When a Given step is executed, it will configure the system to a well-defined state, such as creating & configuring objects or adding data to a test database.
- When – It is used to describe an event or action. This can be a person interacting with the system, or it can be an event triggered by another system.
- Then – It is used to describe the expected outcome of the scenario
- And/But – If we have several Given’s, When’s or Then’s, then we can use And /But.
Below is the sample feature file.
Feature: Book flight ticket for one-way trip
Scenario:flight ticket for one-way trip from Dublin
Given I live in Dublin
And I want to book one way flight ticket from Dublin for 22nd Jan 20
When I search Online
Then TripAdvisor should provide me options of flight for 22nd Jan 20
And Cost of my flight should not be more than 50
And Tickets should be refundable
What is Step Definition?
It is a Java method with an expression, which is used to link it to Gherkin steps. When Cucumber executes a Gherkin step, it will look for a matching step definition to execute.
Cucumber reads the Gherkin script and maps each step to a piece of executable code (known as step definitions) written in a programming language such as Java, Ruby, or Python. These step definitions execute the steps described in Gherkin to verify the application’s behavior.
Feature: Book flight ticket for one-way trip
Scenario: flight ticket for one-way trip from Dublin
Given I live in Dublin
@Given ("I live in Dublin")
public voidVacation()
{
System.out.println("I live in Dublin");
}
When Cucumber encounters a Gherkin step without a matching step definition, it will print a step definition snippet with a matching Cucumber Expression. We can use this as a starting point for a new step definition.
Scenario: Flight ticket for one-way trip from Dublin
Given I live in Dublin
Here, Cucumber didn’t get step definition for the above-mentioned Given step. So, it will provide a snippet as mentioned below:-
@Given ("I live in Dublin")
public void i_live_in_Dublin() {
// Write code here that turns the phrase above into concrete actions
throw new cucumber.api.PendingException();
}
We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!
I'm new to BDD..in learning phase. I have a doubt. How do we run feature file, like any normal Java program by clicking run option?
LikeLike
Nice attempt. I was going through your other blogs and saw there is a section for selenium interview questions. Can you add something like that for cucumber. Cheers
LikeLike
We need to create a Runner file which is used to run the feature file, it is something like below. There will be another tutorial on this topic.@RunWith(Cucumber.class)@CucumberOptions(features = \”src/test/resources/features/Demo.feature\”, tags = \”\”)public class TestRunner {}
LikeLike