Run Failed Scenarios in Cucumber Selenium Java
In Cucumber + Selenium + Java + TestNG, you can run failed scenarios again using the Rerun Plugin. Cucumber provides a built-in rerun formatter that stores failed scenario locations in a text file. You can then execute those scenarios separately.
1. Add Maven Dependencies
Add these dependencies to your pom.xml (use the same compatible Cucumber version for all Cucumber dependencies).
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<cucumber.version>7.20.1</cucumber.version>
</properties>
<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>${cucumber.version}</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.27.0</version>
</dependency>
</dependencies>
2. Configure TestRunner.java
Use two plugins:
pretty: Displays execution details.rerun:target/failed_scenarios.txt: Saves failed scenario locations.
package runner;
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
@CucumberOptions(
features = "src/test/resources/features",
glue = {"stepdefinitions", "hooks"},
plugin = {
"pretty",
"html:target/cucumber-report.html",
"rerun:target/failed_scenarios.txt"
},
monochrome = true
)
public class TestRunner extends AbstractTestNGCucumberTests {
}
3. Create Sample Feature File
Login.feature
Feature: Login functionality
Scenario: Valid login
Given user opens the login page
When user enters valid credentials
Then user should see the dashboard
Scenario: Invalid login
Given user opens the login page
When user enters invalid credentials
Then user should see an error message
Scenario: Empty login
Given user opens the login page
When user clicks on login without credentials
Then validation message should appear
4. Create Failed Scenario Rerun Runner
Create another runner class called FailedScenarioRunner.java.
package runner;
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
@CucumberOptions(
features = "@target/failed_scenarios.txt",
glue = {"stepdefinitions", "hooks"},
plugin = {
"pretty",
"html:target/failed-rerun-report.html"
},
monochrome = true
)
public class FailedScenarioRunner extends AbstractTestNGCucumberTests {
}
The important configuration is:
features = "@target/failed_scenarios.txt"
The @ tells Cucumber to read scenario locations from the rerun file.
5. Execute Test Cases
First Execution
mvn test -Dtest=TestRunner
Suppose three scenarios execute and two fail.
The file target/failed_scenarios.txt might contain:
src/test/resources/features/Login.feature:10
src/test/resources/features/Login.feature:15
Second Execution – Rerun Only Failed Scenarios
mvn test -Dtest=FailedScenarioRunner
Only the failed scenarios will execute again.
6. Execution Flow
- TestRunner.java – Execute all Cucumber scenarios.
- Passed scenarios – Scenarios that pass are not added to the rerun file.
- Failed scenarios – Failed scenarios are recorded in the rerun file.
- failed_scenarios.txt – Stores failed scenario locations.
- FailedScenarioRunner.java – Reads the rerun file and executes only failed scenarios.
- Failed Rerun Report – Generates the HTML report for the rerun execution.
7. Important: Handle WebDriver Properly
When rerunning failed scenarios, make sure every scenario gets a fresh WebDriver instance.
Example Hooks.java:
package hooks;
import io.cucumber.java.Before;
import io.cucumber.java.After;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Hooks {
public static WebDriver driver;
@Before
public void setup() {
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@After
public void tearDown() {
if (driver != null) {
driver.quit();
driver = null;
}
}
}
For parallel execution, use ThreadLocal<WebDriver> instead of a static shared driver.
8. Run Both Executions in One Command
You can also execute both runners sequentially using Maven:
mvn test -Dtest=TestRunner,FailedScenarioRunner
However, ensure the first runner completes and generates the rerun file before the second runner starts. For guaranteed sequential execution, configure separate Maven executions or use a script.
Recommended Framework Structure
CucumberSeleniumFramework
│
├── src/test/java
│ ├── hooks
│ │ └── Hooks.java
│ │
│ ├── stepdefinitions
│ │ └── LoginSteps.java
│ │
│ └── runner
│ ├── TestRunner.java
│ └── FailedScenarioRunner.java
│
├── src/test/resources
│ └── features
│ └── Login.feature
│
├── target
│ ├── failed_scenarios.txt
│ ├── cucumber-report.html
│ └── failed-rerun-report.html
│
└── pom.xml
Interview Tip
Cucumber's
rerunplugin is commonly used to capture failed scenario locations and execute them again through a separate runner. It is different from retrying individual Selenium steps.
You can extend this framework with a retry mechanism that automatically reruns failed scenarios up to 2 or 3 times using TestNG and Cucumber.

No comments:
Post a Comment