I have read about the Cardinal Rule of BDD from various places when writing Gherkin files.
In many of these examples, they have the following scenarios in a feature file:
Feature: Google Searching
Scenario: Search from the search bar
Given a web browser is at the Google home page
When the user enters "panda" into the search bar
Then links related to "panda" are shown on the results page
Scenario: Image search
Given Google search results for "panda" are shown
When the user clicks on the "Images" link at the top of the results page
Then images related to "panda" are shown on the results page
In the example above, it appears that the second scenario is dependent on the first scenario's state. The second scenario needs to have searched for "panda" as in the Given
step, which is essentially the first 2 steps in the first scenario. So, should the Given
step in the second scenario repeat the first 2 steps in the first scenario, or should the second scenario just continue from where the first scenario left off?
Assuming, if they should be run as independent scenarios, does that mean that Given
step in Google search results for "panda" are shown
has to have another step_definition
just to repeat all the steps that were taken in the first scenario?
For example, in the step definitions, should I have something like this?
Given('a web browser is at the Google home page', () => {
// Do a web browser is at the Google home page
}
When('the user enters "panda" into the search bar', () => {
// Do the user enters "panda" into the search bar
}
Then('links related to "panda" are shown on the results page', () => {
// Do links related to "panda" are shown on the results page
}
Given('Google search results for "panda" are shown', () => {
// ** REPEAT the codes in the first 2 steps above for this step?? **
// Do a web browser is at the Google home page
// Do the user enters "panda" into the search bar
}
It feels a little weird to me that I have to repeat all the steps that were already made in the previous scenario to re-establish the state of the app that is already there. Also, in some occasions, going through the same steps may change the state again in the application, giving things more troublesome.
So, should the Given
step in the second scenario repeat the first 2 steps in the first scenario, or should the second scenario just continue from where the first scenario left off?
Did I understand correctly that I should repeat the steps in the previous scenario when my following scenario depends on it?