Cucumber Tutorials

 HOME

Cucumber Introduction, Installation, and Configuration

Chapter 1  Introduction of Cucumber Testing Tool (BDD Tool)
Chapter 2 How to install Cucumber Eclipse Plugin
Chapter 3 How to setup Cucumber with Eclipse
Chapter 4 Cucumber – What is Gherkin

Cucumber Scenario, Features & Step Definition

Chapter 1 Cucumber – What is Feature File in Cucumber
Chapter 2 Step Definition in Cucumber
Chapter 3 Cucumber – JUnit Test Runner Class

Cucumber – Hooks & Tags

Chapter 1 Hooks in Cucumber
Chapter 2 Tags in Cucumber
Chapter 3 Conditional Hooks in Cucumber
Chapter 4 What is CucumberOptions in Cucumber?
Chapter 5 Background in Cucumber
Chapter 6 Monochrome in Cucumber
Chapter 7 What is Glue in Cucumber?

Cucumber – Data Driven Testing

Chapter 1 Data Driven Testing using Scenario Outline in Cucumber
Chapter 2 DataTables in Cucumber

Cucumber Integration with Selenium – Maven

Chapter 1 Integration of Cucumber with Selenium and JUnit4
Chapter 2 Integration of Cucumber with Selenium and TestNG
Chapter 3 Page Object Model with Selenium, Cucumber and JUnit
Chapter 4 Page Object Model with Selenium, Cucumber, and TestNG
Chapter 5 Integration of Cucumber7 with Selenium and JUnit5
Chapter 6 Run Cucumber7 with JUnit5 Tests from Maven Command Line
Chapter 7 How to rerun failed tests in Cucumber
Chapter 8 How to create Cucumber Report after rerun of failed tests – NEW
Chapter 9 How to rerun failed tests twice in Cucumber – NEW

Cucumber – Command Line Execution

Chapter 1 Run Cucumber Test from Command Line
Chapter 2 Run Gradle Cucumber Tests from Command Line

Cucumber Integration with Rest API

Chapter 1 Rest API Test in Cucumber BDD
Chapter 2 How To Create Gradle Project with Cucumber to test Rest API

Cucumber Integration with SpringBoot

Chapter 1 Integration Testing of Springboot with Cucumber and JUnit4
Chapter 2 Integration Testing of Springboot with Cucumber and TestNG

Cucumber – Reporting

Chapter 1 Cucumber Tutorial – Cucumber Reports
Chapter 2 Cucumber Report Service
Chapter 3 Implemention of ‘Masterthought’ Reports in Cucumber
Chapter 4 Implemention of ‘Masterthought’ Reports in Cucumber with JUnit4

Cucumber Integration with Allure Reports

Chapter 1 Allure Report with Cucumber5, Selenium and JUnit4
Chapter 2 Allure Report with Cucumber5, Selenium and TestNG
Chapter 3 Integration of Allure Report with Rest Assured and JUnit4
Chapter 4 Integration of Allure Report with Rest Assured and TestNG
Chapter 5 Gradle – Allure Report for Selenium and TestNG

Cucumber Integration with Extent Reports

Chapter 1 ExtentReports Version 5 for Cucumber 6 and TestNG
Chapter 2 How to add Screenshot to Cucumber ExtentReports
Chapter 3 ExtentReports Version 5 for Cucumber 6 and JUnit4
Chapter 4 PDF ExtentReport for Cucumber and TestNG
Chapter 5 ExtentReports Version 5 for Cucumber 7 and TestNG
Chapter 6 Extent Reports Version 5 for Cucumber7 and JUnit5

Cucumber – Parallel Execution

Chapter 1 Parallel Testing in Cucumber with JUnit
Chapter 2 Parallel Testing in Cucumber with TestNG
Chapter 3 Dependency Injection in Cucumber using Pico-Container

Background in Cucumber

HOME

What is the Background in Cucumber?

The background is used in Cucumber to define a step or series of steps that are shared by all the tests in the feature file.  This helps to reduce redundancy and makes the feature file cleaner and easier to read.

A Background provides context for the scenarios that follow it. It may include one or more Given steps, which are executed before each scenario but after any Before hooks.

A Background is placed at the same level of indentation as the first Scenario/Example.

Below is an example of a Background in a Feature file.

Feature: Login to HRM Application

  Background:
    Given User is on HRMLogin page "https://opensource-demo.orangehrmlive.com/"

  @ValidCredentials
  Scenario: Login with valid credentials

    When User enters username as "Admin" and password as "admin123"
    Then User should be able to login successfully and new page open

  @MissingUsername
  Scenario: Login with blank username

    When User enters username as " " and password as "admin123"
    Then User should be able to see a message "Required" below Username

In the above example, we have two different scenarios, where a user is successfully able to log in to the application and an unsuccessful attempt to log in. But the common step is to open the website for both scenarios. This is why we created another Scenario for opening the browser but named it Background rather than a Scenario. So that it executes for both Scenarios.

import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;

public class LoginPageDefinitions {

    @Given("User is on HRMLogin page {string}")
    public void loginTest(String url) {

        System.out.println("I am in Login Page");

    }

    @When("User enters username as {string} and password as {string}")
    public void goToHomePage(String userName, String passWord) {

        System.out.println("Go to Home Page");

    }

    @Then("User should be able to login successfully and new page open")
    public void verifyLogin() {

        System.out.println("Home Page is opened");

    }

    @Then("User should be able to see a message {string} below Username")
    public void verifyMissingUsernameMessage(String message) {

        System.out.println("Login failed with an error message");
    }

}

We need a runner class to execute the feature file. Below is an example of the Runner class.

import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;

@CucumberOptions(tags = "", features = "src/test/resources/features/LoginPage.feature", glue = "org.example.definitions",
        plugin = {})
public class CucumberRunnerTests extends AbstractTestNGCucumberTests {
}

The output of the above program is

Background with Hooks

Background can contain one or more Given steps, which are run before each scenario, but after any Before hooks.

Below is an example of the background with hooks. The feature file is the same. I’m just adding the hooks to the Step Definition class.

import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;

public class LoginPageDefinitions {

    @Before()
    public void beforeScenario(){
        System.out.println("Start the browser and Clear the cookies");
    }

    @Given("User is on HRMLogin page {string}")
    public void loginTest(String url) {

        System.out.println("I am in Login Page");

    }

    @When("User enters username as {string} and password as {string}")
    public void goToHomePage(String userName, String passWord) {

        System.out.println("Go to Home Page");

    }

    @Then("User should be able to login successfully and new page open")
    public void verifyLogin() {

        System.out.println("Home Page is opened");

    }

    @Then("User should be able to see a message {string} below Username")
    public void verifyMissingUsernameMessage(String message) {

        System.out.println("Login failed with an error message");
    }

  @After()
    public void afterScenario(){
        System.out.println("Log out the user and close the browser");
    }

}

The output of the above program is

Points to remember

  • The background should not be used to create complicated states unless the client requires that information.
    For example, if the client doesn’t care about the user and site names, use a higher-level step like Given I am logged in as a site owner
  • Keep the Background section brief.
    When reading the scenarios, the client must remember this information. Consider moving some of the irrelevant details into higher-level steps if the Background is longer than four lines.
  • Make your background section stand out
    Use interesting names and try to tell a story. The human brain remembers stories much better than it remembers names like “User A,” “User B,” “Site 1,” and so on.