Gherkin
All things gherkin¶
This page will cover all the basics you need to know about bringing user stories to life using gherkin.
External Links¶
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
Given
When
Then
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"
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:
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:
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.