Cucumber – What is Gherkin

 

What is Gherkin?

Gherkin uses a set of special keywords to give structure and meaning to executable specifications. Each keyword is translated to 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 Gherkin document starts with one of the keywords. Comment lines are allowed anywhere in the file, start with a hash sign (#).

What is Feature File?

A file in which we store features, description about the features and scenarios to be tested.

Feature file consist of below-mentioned keywords:-

1) Feature – It is used provide high level description of test scenario and to group related scenarios

The first keyword in the Feature file is Feature keyword, followed by : and short text that describes the feature.

2) Scenario – It describes the steps followed to test the scenario and expected outcome of the scenario. 

3) Scenario Outline – It is used to run same scenario multiple times with different set of values.

4) Background – Suppose we find that Given steps are same for all Scenarios in a feature file. Then we should use Background. We can move these Given steps under 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.

  1. a) 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.
  2. b) 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.
  3. c) Then – It is used to describe the expected outcome of the scenario
  4. d) And/But – If we have several Given’s, When’s or Then’s, then we can use And /But.

Example of a Cucumber/BDD test:

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.

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!!

Advertisement

3 thoughts on “Cucumber – What is Gherkin

  1. 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

    Like

  2. 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 {}

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s