What is Gherkin with syntax and example?

Gherkin is a language used to write test cases for BDD automation with Cucumber or Specflow. Gherkin is a set of keywords and every test line will start with a keyword.

The main aim to design Gherkin is to test case understanding and As it follows the simple English language it means it’s really understandable for Non-Technical as well as for technical people.

In other words, Gherkin is a simple language that is easily understood by the human. You can see one example of gherkin language.

Gherkin Language Example:

Scenario: Add two numbers
Given I have entered 50 into the calculator
And I have entered 70 into the calculator
When I press add
Then the result should be 120 on the screen

This is a simple example of a test scenario that is about to add two numbers you can see the steps it’s easily understandable for normal people(Stack holders/Business analysts).

Keywords in gherkins

Let’s discuss the keywords in Gherkin and then will see some examples to write the test case in Gherkin.

  • Given
  • And
  • When
  • Then
  • But
  • Background
  • Scenario
  • Scenario Outline

All the above-listed keywords are used to define a test case in Gherkin. Now the important question is when we should use which keyword? So my answer is it’s dependent on the requirement or the condition of the test case. Let’s understand them one by one by example.

Example: Create a test case to add two numbers. Now will see all the keywords around this example.

Given in Gherkins

Given is used to define the precondition. I mean precondition is the first step of the test case in the example I have entered 50 into the calculator This is the precondition for this example.

Now suppose We have multiple precognitions, In this case, we will use another keyword “And”. Which is used to add additional statements?

Example:

Given I have entered 50 into the calculator
And I have entered 70 into the calculator

When in Gherkins

When is used to define the Action Condition or In simple words, When is used to perform an event Like “I press a button”.

Now suppose We have multiple Actions, In this case, we will use another keyword “And”. That is used to add additional statements same as the Given keyword.

When I press add
And I accept the remember Popup.

Then in Gherkins

Then is used to define the Verification part In simple words, Then is used to perform assertions or we can say verify the actual and expected result. Like “The result should be 120”.

Now suppose We have multiple Actions, In this case, we will use another keyword “And”. That is used to add additional statements same as the Given keyword.

And, But in Gherkins

But is used to write a successive Given, When, and Then the same as we use “But” in the English language.

Then the result should be 120 on the screen
but the result is not the same as the actual result

How to write a test case in Gherkin?

Below is an example to write a test case for Gherkin for the login form.

Scenario: Login as Admin
Given I Navigate to the login page
And I Enter username and password
When I click to login button 
Then I can see admin is able to login