Skip to content

Gherkin

All things gherkin

This page will cover all the basics you need to know about bringing user stories to life using gherkin.

Cucumber - https://docs.cucumber.io/

Cucumber Gherkin - https://docs.cucumber.io/gherkin/reference/

Behat - http://behat.org/en/latest/quick_start.html

Basic syntax

When writing in gherkin you will come across the following keyword all the time as they're the foundation of each line as without them they wouldn't really do anything.

The keywords are:

Given
When
And 
Then
But 
Now we will cover what each one does.

Given
This is what goes at the start of all your user stories and is used to define the context or set the scene for the test.

When 
This is used to define an action so for example pressing on a button would be an action and also clicking on a link

Then
This is used to define an outcome of an action and what you should expect to happen when doing something on a website and example would be, clicking on a button and assert the correct thing happened like switching web pages.

And
But

Both of these are used to extend the other keywords, so if you want to do two actions in a row you would first use When and then you would use And for the second action.

Now lets see all this working together, the below example is called a scenario and is used to outline a test case for a website. As you can see the one below is completing a basic log in form.

Scenario: The user can log in 
      Given I am on the login page
      When I fill in the following:
         | UserName   | admin        |       
         | Password   | password     |
      And I follow "Log In"
      Then I should see "Sign Out"
The above has covered the basics of writing gherkin, what we will do next is dive further in and go through more advance areas.

Advanced Gherkin

This next bit will cover what comes after writing a basic scenario, we will cover the following areas

Feature:
Background:
Scenario Outline: 
Examples:

Same as before we will go through each of them and how they work and how to use them.

Feature:
This is used to outline what the following scenario should do and should also give more context about what needs testing.

Example below:

Feature: A user needs to access the system 
      In order to access the system 
      As a user 
      I need to be able to log in 

Background:
Backgrounds are normally used when you have the same step repeating in scenarios and saves on space an example of this would be if you need to be logged in for each scenario this would go into the background

Scenario Outline: The user can log in 
      Given I am on the login page
      When I fill in the following:
         | UserName   | <UserName>     |         
         | Password   | <Password>     |
      And I follow "Log In"
      Then I should see "Sign Out"

Examples:
|UserName |Password   |
|user1    |Password1  |
|user2    |Password1  |

Scenario outlines can take a bit more time to set up and get written but in the long run they help a lot as they can turn 5 very similar scenarios into 1 which will also save space inside your feature file.

As you can see from the example above you will see <Username> inside the scenario outline, what this means is that when the test runs it will find the username column from the Examples: part and use the information in there to complete the field and this also applies to the password field too.

This can be extended as many times as you want to try cover more tests.