The **Background**
keyword in Cucumber is used to define common preconditions or steps that are shared across multiple scenarios in a feature file.
It helps you avoid duplication of repeated steps and improves readability and maintainability.
How It Works:
- The steps inside the
Background
block are executed before every scenario in the feature file. - Only one
Background
block is allowed per feature file. - It runs before each
Scenario
orScenario Outline
but not before Hooks (@Before
).
When to Use Background:
- When multiple scenarios share the same preconditions.
- Example: Launching a browser, opening a URL, or navigating to a common page.
When NOT to Use Background:
- When only one or two scenarios need those steps.
- If the setup is specific to a tag, prefer
@Before("@tag")
hook instead.
============================================
Feature: Application Login
Background:
Given user launches the browser
And user navigates to Gmail login page
#Data Driven:
@backgroundkeyword
Scenario: Gmail login page
Given user signing into Gmail login page
When user login into Gmail application
| Prabhu |
| Manyam |
| Prabhu65656@gmail.com |
Then Gmail homepage is displayed
============================================
package stepDefinitions;
import java.util.List;
import java.util.Map;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
public class MainSteps {
@Given("user launches the browser")
public void launch_browser() {
System.out.println("Browser launched");
}
@Given("user navigates to Gmail login page")
public void navigate_to_facebook() {
System.out.println("Navigated to Gmail");
}
@Given("user signing into Gmail login page")
public void user_signing_into_gmail_login_page() {
// Write code here that turns the phrase above into concrete actions
System.out.println("User logging in Gmail page");
}
@When("user login into Gmail application")
public void user_login_into_gmail_application(List<String> data) {
System.out.println(data.get(0)); // Prints "Hari"
System.out.println(data.get(1)); // Prints "Taraka"
System.out.println(data.get(2)); // Prints "samplemail@gmail.com"
}
//Note: It takes a list of strings (data) and prints out user details such as the name and email address.
@Then("Gmail homepage is displayed")
public void gmail_homepage_is_displayed() {
System.out.println("Gmail homepage is displayed");
System.out.println("=================================================");
}
}
============================================
package cucumberOptions;
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
@CucumberOptions(features="src/test/java/features", glue ="stepDefinitions", tags= "@backgroundkeyword", monochrome=true)
public class TestNGRunner extends AbstractTestNGCucumberTests {
}
============================================
Output:
Browser launched
Navigated to Gmail
User logging in Gmail page
Prabhu
Manyam
Prabhu65656@gmail.com
Gmail homepage is displayed
============================================