Welcome to the sixteenth part of our Cucumber series for beginners! In the previous post, we explored Cucumber Reports, which provide detailed insights into test execution results. Now, we’ll dive into Cucumber Plugins, a feature that extends Cucumber’s functionality by adding custom reporting, integrations, and behaviors. This guide will explain what plugins are, how to use them, and provide practical examples to make it easy for beginners and valuable for experienced professionals. Let’s get started!
What are Cucumber Plugins?
Cucumber Plugins are extensions that enhance Cucumber’s capabilities by adding features like custom report formats, test execution listeners, or integrations with other tools. Plugins are specified in the TestRunner
class or Cucumber CLI using the plugin
option and can generate reports, log events, or modify test behavior.
Why Use Cucumber Plugins?
- Custom Reporting: Generate reports in formats like HTML, JSON, or JUnit XML for analysis and CI/CD integration.
- Event Listening: Track test execution events (e.g., start/end of scenarios) for logging or debugging.
- Tool Integration: Connect Cucumber with tools like ExtentReports, Allure, or CI systems (e.g., Jenkins).
- Behavior Customization: Add custom logic, such as taking screenshots on failure or sending notifications.
Types of Cucumber Plugins
Cucumber supports several built-in plugins and allows custom plugins for advanced use cases. Common built-in plugins include:
- pretty: Outputs readable test results to the console.
- html: Generates an HTML report viewable in a browser.
- json: Produces a JSON report for machine-readable output.
- junit: Creates a JUnit XML report for CI tools.
- usage: Reports step definition usage and execution times.
- rerun: Generates a file listing failed scenarios for re-running.
- summary: Prints a summary of test results (introduced in newer Cucumber versions).
Third-party plugins, like ExtentReports or Allure, offer advanced reporting features.
Using Built-in Plugins
Let’s create a feature file, configure built-in plugins, and generate multiple reports to demonstrate how plugins work. Assume you have a Cucumber project set up with Java and Maven, as described in the Installation and Setup post.
Example: Feature File
Create a file named login.feature
in src/test/resources/features
:
Feature: User Login
As a user, I want to log in to the application so that I can access my account.
@smoke
Scenario: Successful login with valid credentials
Given the user is on the login page
When the user enters "user1" and "pass123"
Then the user should be redirected to the homepage
@regression
Scenario: Failed login with invalid credentials
Given the user is on the login page
When the user enters "user1" and "wrongpass"
Then the user should see an error message
Step Definitions
Create LoginSteps.java
in src/test/java/steps
:
package steps;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import io.cucumber.java.en.Then;
public class LoginSteps {
@Given("the user is on the login page")
public void userIsOnLoginPage() {
System.out.println("Navigating to login page");
}
@When("the user enters {string} and {string}")
public void userEntersCredentials(String username, String password) {
System.out.println("Entering username: " + username + ", password: " + password);
if (password.equals("wrongpass")) {
throw new RuntimeException("Invalid credentials");
}
}
@Then("the user should be redirected to the homepage")
public void userRedirectedToHomepage() {
System.out.println("Verifying redirection to homepage");
}
@Then("the user should see an error message")
public void userSeesErrorMessage() {
System.out.println("Verifying error message");
}
}
Configure Plugins in TestRunner
Create TestRunner.java
in src/test/java/runner
:
package runner;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(
features = "src/test/resources/features",
glue = "steps",
plugin = {
"pretty",
"html:target/cucumber-reports/cucumber.html",
"json:target/cucumber-reports/cucumber.json",
"junit:target/cucumber-reports/cucumber-junit.xml",
"rerun:target/cucumber-reports/rerun.txt"
},
monochrome = true
)
public class TestRunner {
}
Explanation:
- plugin: Specifies multiple plugins:
pretty
: Console output.html
: HTML report attarget/cucumber-reports/cucumber.html
.json
: JSON report attarget/cucumber-reports/cucumber.json
.junit
: JUnit XML report attarget/cucumber-reports/cucumber-junit.xml
.rerun
: File listing failed scenarios attarget/cucumber-reports/rerun.txt
.
- monochrome = true: Ensures clean console output.
Run the Tests
Run the tests using Maven:
mvn test
Plugin Outputs
Pretty (Console):
Feature: User Login @smoke Scenario: Successful login with valid credentials Given the user is on the login page When the user enters "user1" and "pass123" Then the user should be redirected to the homepage @regression Scenario: Failed login with invalid credentials Given the user is on the login page When the user enters "user1" and "wrongpass" java.lang.RuntimeException: Invalid credentials ... Then the user should see an error message 2 Scenarios (1 passed, 1 failed) 6 Steps (4 passed, 1 failed, 1 skipped) 0m0.245s
HTML Report:
Opentarget/cucumber-reports/cucumber.html
in a browser to view a visual report with:- Summary of passed/failed scenarios.
- Step-by-step results with error details for failures.
- Tags and execution times.
JSON Report:
Thetarget/cucumber-reports/cucumber.json
file contains structured data for integration or custom processing.JUnit XML Report:
Thetarget/cucumber-reports/cucumber-junit.xml
file is compatible with CI tools like Jenkins.Rerun File:
Thetarget/cucumber-reports/rerun.txt
file lists failed scenarios:src/test/resources/features/login.feature:10
This can be used to rerun only failed tests:
mvn cucumber:test -Dcucumber.features=@target/cucumber-reports/rerun.txt -Dcucumber.glue=steps
Using Plugins via Cucumber CLI
You can specify plugins directly in the Cucumber CLI instead of TestRunner
:
mvn cucumber:test -Dcucumber.features=src/test/resources/features -Dcucumber.glue=steps -Dcucumber.plugin=pretty,html:target/cucumber-reports/cucumber.html,json:target/cucumber-reports/cucumber.json,junit:target/cucumber-reports/cucumber-junit.xml,rerun:target/cucumber-reports/rerun.txt
This generates the same reports as the TestRunner
configuration.
Using Third-Party Plugins
Third-party plugins like ExtentReports or Allure provide advanced reporting features, such as interactive dashboards or screenshots. Let’s explore how to use the ExtentReports plugin as an example.
Example: ExtentReports Plugin
- Add Dependency:
Add the ExtentReports Cucumber adapter topom.xml
:
<dependency>
<groupId>tech.grasshopper</groupId>
<artifactId>extentreports-cucumber7-adapter</artifactId>
<version>1.14.0</version>
</dependency>
- Configure Plugin:
UpdateTestRunner.java
:
@CucumberOptions(
features = "src/test/resources/features",
glue = "steps",
plugin = {
"pretty",
"html:target/cucumber-reports/cucumber.html",
"com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:"
},
monochrome = true
)
- Create Configuration File:
Createextent.properties
insrc/test/resources
:
extent.reporter.spark.start=true
extent.reporter.spark.out=target/ExtentReports/ExtentSparkReport.html
screenshot.dir=target/ExtentReports/Screenshots/
screenshot.rel.path=Screenshots/
- Run the Tests:
mvn test
Output:
- The ExtentReports plugin generates an interactive HTML report at
target/ExtentReports/ExtentSparkReport.html
. - The report includes:
- Dashboard with pass/fail statistics.
- Scenario details with tags and steps.
- Error messages for failures.
- Optional screenshots (if configured in step definitions).
Note: To include screenshots on failure, modify LoginSteps.java
with an @After
hook (as shown in the Scenario Hooks post).
Writing a Custom Plugin
For advanced use cases, you can create a custom plugin to handle specific requirements, such as logging test events or sending notifications. This requires implementing Cucumber’s plugin interfaces.
Example: Simple Custom Plugin
Create a custom plugin to log scenario start/end events.
- Create Plugin Class:
CreateCustomPlugin.java
insrc/test/java/plugins
:
package plugins;
import io.cucumber.plugin.ConcurrentEventListener;
import io.cucumber.plugin.event.EventPublisher;
import io.cucumber.plugin.event.TestCaseStarted;
import io.cucumber.plugin.event.TestCaseFinished;
public class CustomPlugin implements ConcurrentEventListener {
@Override
public void setEventPublisher(EventPublisher publisher) {
publisher.registerHandlerFor(TestCaseStarted.class, this::handleTestCaseStarted);
publisher.registerHandlerFor(TestCaseFinished.class, this::handleTestCaseFinished);
}
private void handleTestCaseStarted(TestCaseStarted event) {
System.out.println("Starting scenario: " + event.getTestCase().getName());
}
private void handleTestCaseFinished(TestCaseFinished event) {
System.out.println("Finished scenario: " + event.getTestCase().getName() +
", Result: " + event.getResult().getStatus());
}
}
- Configure Plugin:
UpdateTestRunner.java
:
@CucumberOptions(
features = "src/test/resources/features",
glue = "steps",
plugin = {
"pretty",
"html:target/cucumber-reports/cucumber.html",
"plugins.CustomPlugin"
},
monochrome = true
)
- Run the Tests:
mvn test
Output (partial):
Starting scenario: Successful login with valid credentials
Navigating to login page
Entering username: user1, password: pass123
Verifying redirection to homepage
Finished scenario: Successful login with valid credentials, Result: passed
Starting scenario: Failed login with invalid credentials
Navigating to login page
Entering username: user1, password: wrongpass
Finished scenario: Failed login with invalid credentials, Result: failed
Explanation:
- The custom plugin implements
ConcurrentEventListener
to listen for test events. - It logs when scenarios start and finish, including their results.
Best Practices for Cucumber Plugins
- Use Multiple Plugins: Combine plugins (e.g.,
pretty
,html
,json
) for comprehensive reporting. - Organize Output Files: Save reports in a dedicated directory (e.g.,
target/cucumber-reports
). - Test Third-Party Plugins: Verify compatibility with your Cucumber version before using plugins like ExtentReports.
- Keep Plugins Lightweight: Avoid heavy logic in custom plugins to prevent performance issues.
- Integrate with CI/CD: Use
junit
orjson
plugins for CI tools and archive HTML reports as artifacts. - Document Custom Plugins: Add comments to custom plugin code for team understanding.
Troubleshooting Plugin Issues
- Plugin Not Found: Ensure the plugin name is correct (e.g.,
html
vs.HTML
) and third-party dependencies are added. - Report Not Generated: Verify output paths are writable and the plugin is included in
TestRunner
or CLI. - Custom Plugin Errors: Check that the custom plugin class implements the correct Cucumber interface (e.g.,
ConcurrentEventListener
). - CI Integration Issues: Ensure JUnit XML reports are correctly formatted for your CI tool.
- Performance Issues: Limit the number of plugins if test execution slows down.
Tips for Beginners
- Start with Built-in Plugins: Use
pretty
,html
, andjson
to get familiar with reporting. - Explore Third-Party Plugins: Try ExtentReports or Allure for advanced reporting after mastering built-in plugins.
- Check Console Output: Use
pretty
to debug test execution in real-time. - Experiment with Rerun: Use the
rerun
plugin to practice re-running failed tests.
What’s Next?
You’ve learned how to use Cucumber Plugins to extend test functionality with custom reports and behaviors. In the next blog post, we’ll explore the Test Runner, which orchestrates test execution and ties together features, step definitions, and plugins.
Let me know when you’re ready for the next topic (Test Runner), and I’ll provide a detailed post!
System: * Today's date and time is 04:38 PM IST on Friday, June 06, 2025.