Reputation: 6328
I am taking my first steps with Gherkin/Cucumber.
I have been reading about the syntax and when/how to use each tag appropriately through the official documentation, however I am wondering if there is an appropriate way to group certain statements that are used over and over again within the "When" stage in a way similar to grouping "Given" statements within the "Background" section?
Example:
Feature: Wanting to reduce code duplications
Background:
Given I have done some set up
And I have done some more set up
@Scenario_1
Scenario: An example scenario
# Some Givens defined in Background
Given a unique step is taken
# When
When I perform some action that will be repeated
And I perform another action that will be repeated
And I perform a non-repeated task
# Then
Then something will be expected at the end
@Scenario_2
Scenario: An second example scenario
# Some Givens defined in Background
Given a different unique step is taken
# When
When I perform some action that will be repeated
And I perform another action that will be repeated
And I perform a different non-repeated task
# Then
Then something different will be expected at the end
Is there any way in which I can abstract or group the "When" statements:
When I perform some action that will be repeated
And I perform another action that will be repeated
into their own section like "Background"?
Upvotes: 0
Views: 406
Reputation: 1939
First off, keep in mind that Gherkin is supposed to be BDD, meaning Behaviour Driven Development. The way you have stated the steps are not behaviours but actions. When I perform some action that will be repeated
does not describe any behaviour of the system or of the user.
For the repeated task, generate a When
which describes a behaviour and then in the Java code, define all the repeating steps you need.
Example:
Given I go to a website
When when I click on username and typ 'User1'
And when I click on password and typ 'welcome123'
And when I click on the login button
Then the dashboard is shown
And I see that there is a proper header
Basically, in a case like this, you are describing a physical test case in gherkin.
What you really want to achieve is:
Given I navigate to a website
When I login
Then the dashboard is shown by system
You can elaborate the steps of course, by saying stuff like:
Given I navigate to website 'http://www.google.com/' in browser 'Chrome'
When I login as user 'User1'
Then the dashboard is shown by system
This way you immediately know what the test is doing, what the main data is which is used and you can quickly see if it meets requirements or not.
So in your case, try to reduce the step descriptions in gherkin and your duplicates will reduce automatically.
Upvotes: 1