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 test case understanding and As it follows the simple English language it means its really understandable for Non-Technical as well as for technical people.

In other words, Gherkin is a simple language which is easily understood by the human. You can see the 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 the 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 analyst).

Keywords in gherkins

Let’s discuss about the keywords in gherkin and then will see some example 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 its dependence on the requirement or the condition of the test case. Let’s understand them one by one by an example.

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

Given in Gherkins

Given is used to define the precondition. I mean to precondition is the first step of the test case like 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 word, 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 or In simple word, 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 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 the example to write a test case 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

[embedyt] https://www.youtube.com/watch?v=IJNjEua1Xv8[/embedyt]