Background in Cucumber

Profile picture for user devraj

What is Background in Cucumber?

Often you find that several Scenarios in the same Feature start with common steps. Background in Cucumber defines a Step or series of Steps common to all Scenarios in the feature file. 

For example, to perform several user-related scenarios, you need to navigate to the login page and enter your username and password. So instead of writing them again and again, it is good practice to mention them in the Background section.

Background Example in Cucumber

Feature file without Background

Feature: Registration, Login and MyAccount

  @smoke
  Scenario: Verify Login Functionality
    Given I am on the homepage
    And I follow "Sign in"
    When  I fill "email address textbox" with "goswami.tarun77@gmail.com"
    Then I fill "password textbox" with "Test1234"
   
  @smoke
  Scenario: Create New User
    Given I am on the homepage
    When I follow "Sign in"
    When I fill "registration email textbox" with "goswami.tarun77+1@gmail.com"
    Then I click "create an account button"

Feature file with Background

In above 2 scenario you can observe first 2 steps in scenario are common, so above can be replaced with:

Feature: Registration, Login and MyAccount

  Background:
    Given I am on the homepage
    And I follow "Sign in"

  @smoke
  Scenario: Verify Login Functionality
    When I fill "email address textbox" with "goswami.tarun77@gmail.com"
    Then I fill "password textbox" with "Test1234"
   
  @smoke
  Scenario: Create New User
    When I fill "registration email textbox" with "goswami.tarun77+1@gmail.com"
    Then I click "create an account button"

The Background section will be executed before each Scenario or Scenario Outline in a feature file. It is good to add your preconditions or steps which starts with given in the Background section. Position of Background also matters; you should declare it before any Scenario.

Advantages of using Background in a Feature File

  1. Avoid Repetition and Duplication of Steps
  2. Improve Readability of Feature File
  3. Maintainability is easy. We have to modify the Steps in one place rather than in all the Scenarios.

Background Title and Description

Generally, people avoid writing Titles and multi-line descriptions for Background as these are optional for Background. 

When to Add Steps in Background 

You have to think carefully before adding steps to the Background because these are common for all the scenarios. There is a possibility that steps are common for a few that are not needed for remaining or not required for one to automate in the future. 

Also, you or the client needs to remember Background Steps when reading the scenarios. If the Background is more than four lines long, consider moving some irrelevant details into higher-level steps.

Multiple Background in Cucumber

There can be only single Background in a Feature file if Rule Section is not used. With Rule, you can have a separate background section, including the one at the feature level.

Before Hook and Background in Cucumber

The critical point to note is @Before hook executes even before the Background section. It is essential to understand the proper usage of Background. It can contain one or more Given steps, which are run before each scenario, but after any Before hooks.

As Hooks give a similar kind of functionality, almost all the tasks can be done by hooks as well. Any feature level dependency should be tied with the Background, and any scenario level dependency should be tied with hooks.

Video Tutorial: Background in Cucumber