Understanding Background in Cucumber
Background in cucumber is a concept that allows you to specify steps that are pre-requisite to all the scenarios in a given feature file.
Precisely doing what a setup method does in your junit or testNG. For example, in both the scnearios we have written so far the user needs to be on the landing page to start the sign-up process. We can treat the step to start the sign-up process from the landing page as a Background step in the feature file and then write multiple sign-up scenario (positive and negative) in the feature file.
Let us see some code that would make it more clear. This is how our feature file would look like with Background step.
Feature:
As a user
I want to be able to add new clients in the system
So that i can add accounting data for that client
Background:
Given the user is on landing page
When she chooses to sign up
Scenario: Sign up a new user
And she provides the first name as Sukesh
And she provides the last name as Kumar
And she provides the email as [email protected]
And she provides the password as password
And she provides the confirm password again as password
And she signs-up
Then she should be logged in to the application
Scenario Outline: Data driving new user sign-up
And she provides the first name as <firstName>
And she provides the last name as <lastName>
And she provides the email as <email>
And she provides the password as <password>
And she provides the confirm password again as <password>
And she signs-up
Then she should be logged in to the application
Examples:
| firstName | lastName | email | password |
| Sukesh | Kumar | [email protected] | password |
Notice how background is defined in the feature file. Now all the scenarios in this class would run the Background steps before running any scenario.