How To Share Common Steps Between Gherkin Scenarios

Gherkin

29/08/2022


If you notice yourself writing the same Given steps over and over again in all scenarios of a feature:

GHERKIN
Scenario A:
Given User is logged in
And User did X
...
Scenario B:
Given User is logged in
When User goes to page Y
...

You can extract those steps using the keyword Background as follows:

GHERKIN
Background:
Given User is logged in
Scenario A:
Given User did X
...
Scenario B:
When User goes to page Y
...

This keyword runs before each scenario, but after any Before hooks.


WRITTEN BY

Code and stuff