Cucumber HTML report

Cucumber provides built-in reporting plugins that generate reports in HTML, JSON, XML, and plain text formats. These reports help visualize test execution results.

In this section, you’ll learn how to:

     1. Generate a basic HTML report using Cucumber’s built-in plugin
     2. Embed screenshots for failed scenarios in the report

Add plugin in @CucumberOptions

The plugin option in the @CucumberOptions annotation is used to generate reports.

To generate an HTML report, include the following:

@CucumberOptions(
    plugin = {
        "pretty",
        "html:target/Reports/cucumber.html"
    },
    glue = "StepDefinitions",
    features = "src/main/resources/features",
    monochrome = true
)


     1. "pretty": Prints the Gherkin steps clearly in the console
     2. "html:target/Reports/cucumber.html": Generates an HTML report in the target/Reports folder

Sample JUnit Runner Class

package test.utils;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(
    plugin = {
        "pretty",
        "html:target/Reports/cucumber.html"
    },
    glue = "StepDefinitions",
    features = "src/main/resources/features",
    monochrome = true
)
public class Runner {
}


After running your tests, open the generated HTML report from: target/Reports/cucumber.html

Supported Formats

Format

Plugin Syntax

HTML

"html:target/Reports/report.html"

JSON

"json:target/Reports/report.json"

JUnit XML

"junit:target/Reports/report.xml"

Plain Text

"summary" or "message"


We can also use multiple format at once

plugin = {
    "pretty",
    "html:target/Reports/cucumber.html",
    "json:target/Reports/cucumber.json",
    "junit:target/Reports/cucumber.xml"
}


Embedding Screenshots in HTML Report 

If a scenario fails, you can attach a screenshot to the HTML report using this code in your Hooks.java file.

import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
@After
public void tearDown(Scenario scenario) {
    if (scenario.isFailed()) {
        byte[] screenshot = ((TakesScreenshot) Hooks.driver).getScreenshotAs(OutputType.BYTES);
        scenario.attach(screenshot, "image/png", "Failed Scenario Screenshot");
    }
    Hooks.driver.quit();
}

 

Related Tutorials