Latest post from this blog
Purpose of the StepData Class in Selenium + Java Cucumber Automation Framework
- Get link
- X
- Other Apps
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
| StepData | Static Variables |
|---|---|
| Scenario-level data | Global data |
| Parallel execution safe | Not parallel-safe |
| Clean framework design | Risk of data leakage |
| Recommended approach | Not 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
Post a Comment