Latest post from this blog

How to Manage Test Execution Across Different Browsers and Environments (QA, UAT, Staging)

In real-time automation projects, test execution is never limited to a single browser or a single environment . Applications must be validated across multiple browsers (Chrome, Firefox, Edge) and multiple environments such as QA, UAT, and Staging before going live. A well-designed Selenium + Java + Cucumber automation framework should allow testers to switch browsers and environments easily without changing test scripts . This blog explains how to manage test execution efficiently across different browsers and environments using best practices followed in real projects. Why Multi-Browser and Multi-Environment Testing Is Important Different users use different browsers QA, UAT, and Staging environments have different configurations Bugs may appear only in specific environments or browsers Same test cases must be validated everywhere before production release Common Challenges Testers Face Hardcoded browser names and URLs Maintaining separate test scripts for each environment Browse...

Purpose of the StepData Class in Selenium + Java Cucumber Automation Framework

When working with a Selenium + Java + Cucumber automation framework, one of the most common challenges testers face is sharing data between different step definitions within the same scenario.

For example, a value created in one step—such as a login token or order ID—often needs to be reused in later steps.
This is where the StepData class becomes extremely useful.

In this blog, we will cover:

  • What the StepData class is

  • Why it is required in Cucumber frameworks

  • How it is used in real-time automation projects

  • Best practices for using StepData effectively


What Is the StepData Class?

The StepData class is a simple POJO (Plain Old Java Object) designed to store and share test data across multiple step definition classes within a single Cucumber scenario.

It acts as a central data container that holds scenario-specific information during test execution.


Why Do We Need StepData in Cucumber?

Cucumber follows an object-oriented execution model, which creates certain limitations:

  • Each step definition class is instantiated separately

  • Instance variables cannot be shared across step classes

  • Using static variables can lead to:

    • Data leakage

    • Flaky tests

    • Issues during parallel execution

👉 The StepData class solves this problem by enabling scenario-level data sharing using dependency injection (such as PicoContainer).


Key Purposes of the StepData Class

1. Share Data Between Multiple Steps

In real-world automation scenarios, data often flows across steps.

Examples:

  • Authentication token generated during login

  • Order ID created during checkout

  • Dynamically generated username or email

The StepData class stores these values securely and makes them accessible to other steps in the same scenario.


2. Maintain Scenario-Level State

Each scenario gets its own instance of StepData, which ensures:

  • No data overlap between scenarios

  • Safe and reliable parallel execution

  • Predictable and clean test behavior


3. Improve Framework Maintainability

Using StepData helps to:

  • Avoid static or global variables

  • Keep step definition classes clean and focused

  • Follow object-oriented design principles

  • Make the automation framework easier to scale and maintain


Example: StepData Class

public class StepData {

    private String userName;
    private String orderId;
    private String authToken;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public String getAuthToken() {
        return authToken;
    }

    public void setAuthToken(String authToken) {
        this.authToken = authToken;
    }
}

Using StepData in Step Definition Classes

Cucumber supports Dependency Injection (PicoContainer), which allows the same StepData object to be injected into multiple step definition classes.

Login Step Definition

public class LoginSteps {

    private StepData stepData;

    public LoginSteps(StepData stepData) {
        this.stepData = stepData;
    }

    @Given("user logs in successfully")
    public void userLogsIn() {
        stepData.setAuthToken("abc123");
    }
}

Order Step Definition

public class OrderSteps {

    private StepData stepData;

    public OrderSteps(StepData stepData) {
        this.stepData = stepData;
    }

    @Then("user places an order")
    public void placeOrder() {
        String token = stepData.getAuthToken();
    }
}

Real-Time Use Cases of StepData

  • Storing API authentication tokens

  • Sharing order IDs between multiple steps

  • Handling OTP or verification values

  • Passing data between UI and API automation steps


When to Use StepData (Best Practices)

✅ Use StepData when:

  • Data must be shared across multiple steps

  • Values are generated dynamically at runtime

  • Tests are executed in parallel

  • Scenario-level data isolation is required

❌ Avoid StepData when:

  • Data is used only inside a single step

  • Values are constant or configuration-based


StepData vs Static Variables

StepDataStatic Variables
Scenario-level dataGlobal data
Parallel execution safeNot parallel-safe
Clean framework designRisk of data leakage
Recommended approachNot recommended

Conclusion

The StepData class is a core utility in a well-designed Selenium + Java + Cucumber automation framework.

It helps testers to:

  • Share data cleanly between step definitions

  • Avoid common Cucumber limitations

  • Build scalable, maintainable, and parallel-safe frameworks

If you are designing a real-time automation framework, using the StepData class is a best practice you should not ignore.

Comments

Popular posts from this blog

How to Manage Test Execution Across Different Browsers and Environments (QA, UAT, Staging)

Ensuring Thread Safety in Parallel Test Execution with Selenium, Cucumber, and Java