-
Book Overview & Buying
-
Table Of Contents
Cucumber Cookbook
By :
When we write Feature files, we write multiple Scenarios. Now all of these Scenarios start from one particular point. If I'm writing home page Scenarios, then I need to start the flow from the login functionality. So it is better to write the repetitive Steps at one place rather than in all Scenarios. Let's understand how to do this in the next Section.
Based on what we have learned so far, this is what our Feature file will look like:
Feature: Home Page
In order to test Home Page of application
As a Registered user
I want to specify the features of home page
Scenario: Home Page Default content
Given a registered user exists
Given user is on GitHub login page
When user enters username
And user enters password
And user clicks on login button
Then user is on Application home page
And user gets a GitHub bootcamp section
Scenario: GitHub Bootcamp Section
Given user is on GitHub loginpage
When user enters username
And user enters password
And user clicks on login button
Then user is on Application home page
When user focuses on GitHub Bootcamp Section
Then user gets an option to setup git
Scenario: Top Banner content
Given user is on GitHub login page
When user enters username
And user enters password
And user clicks on login button
Then user is on Application home page
When user focuses on Top Banner
Then user gets a logout optionThe problem here is that first five statements are repeated in all the Scenarios. This affects the readability of the Feature files, and there is a lot of duplicated effort.
The problems with this way of writing Feature files are:
We are going to update the home_page.feature file and we are going to use the Background keyword to put the common Steps across all the Scenarios in one place:
Feature: Home Page In order to test Home Page of application As a Registered user I want to specify the features of home page Background: flow till home page Given user is on Application home page When user enters username And user enters password And user clicks on login button Then user is on Application home page Scenario: Home Page Default content Then user gets a GitHub bootcamp section Scenario: GitHub Bootcamp Section When user focuses on GitHub Bootcamp Section Then user gets an option to setup git Scenario: Top Banner content When user focuses on Top Banner Then user gets an option of home page
Here, we have used the Background keyword. All the Steps mentioned in the Background keyword will be executed before each Scenario or Scenario Outline in a Feature file. Let's understand this keyword in greater detail:
This is what the output of the preceding file looks like:

Don't use Background to set up a complicated state unless that state is actually something the client needs to know.
Background section short because you expect a person to remember these Steps when you are adding a new ScenarioBackground section vivid, because that way it will be easier for a person to remember itChange the font size
Change margin width
Change background colour