I was trying to implement a custom formatter using cucumber event listener but I was not able to get official documentation for this class.

When I Googled i got the below link :


But this is for version 4.7.2 , the latest Cucumber version is 6.0.0 RC2

Could someone let me know where to get class definition for cucumber 6.0.0 RC 2 , or the right way to implement reporting in cucumber ?


1 Answer 1


For anyone who is looking to create Test report using extent report :

Here you go :)

TestNg runner:

package cucumber_Runner;

import io.cucumber.testng.AbstractTestNGCucumberTests; import io.cucumber.testng.CucumberOptions; import org.testng.annotations.DataProvider;

//Created a custom plugin
@CucumberOptions(plugin = {"cucumberReport.customReportListener"},
glue={"stepDefinitions","cucumberHooks"},  //Packagename
features = {"src\\featureFiles"} //FolderName

public class RunnerCucumber extends AbstractTestNGCucumberTests {

    @DataProvider(parallel = true)
    public Object[][] scenarios() {
        return super.scenarios();

And plugin class:

package cucumberHooks;

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.gherkin.model.Feature;
import com.aventstack.extentreports.gherkin.model.Given;
import com.aventstack.extentreports.gherkin.model.Scenario;
import com.aventstack.extentreports.reporter.ExtentSparkReporter;
import com.aventstack.extentreports.reporter.configuration.Theme;
import io.cucumber.plugin.EventListener;
import io.cucumber.plugin.event.EventPublisher;
import io.cucumber.plugin.event.PickleStepTestStep;
import io.cucumber.plugin.event.TestCaseStarted;
import io.cucumber.plugin.event.TestRunFinished;
import io.cucumber.plugin.event.TestRunStarted;
import io.cucumber.plugin.event.TestSourceRead;
import io.cucumber.plugin.event.TestStepFinished;
import io.cucumber.plugin.event.TestStepStarted;
import io.cucumber.plugin.event.HookTestStep;
import java.util.HashMap;
import java.util.Map;

public class customReportListener implements EventListener {

    private ExtentSparkReporter spark;
    private ExtentReports extent;

    Map<String, ExtentTest> feature = new HashMap<String, ExtentTest>();
    ExtentTest scenario;
    ExtentTest step;

    public customReportListener() {

    public void setEventPublisher(EventPublisher publisher) {
        // TODO Auto-generated method stub

         * :: is method reference , so this::collecTag means collectTags method in
         * 'this' instance. Here we says runStarted method accepts or listens to
         * TestRunStarted event type
        publisher.registerHandlerFor(TestRunStarted.class, this::runStarted);
        publisher.registerHandlerFor(TestRunFinished.class, this::runFinished);
        publisher.registerHandlerFor(TestSourceRead.class, this::featureRead);
        publisher.registerHandlerFor(TestCaseStarted.class, this::ScenarioStarted);
        publisher.registerHandlerFor(TestStepStarted.class, this::stepStarted);
        publisher.registerHandlerFor(TestStepFinished.class, this::stepFinished);


     * Here we set argument type as TestRunStarted if you set anything else then the
     * corresponding register shows error as it doesn't have a listner method that
     * accepts the type specified in TestRunStarted.class

    // Here we create the reporter
    private void runStarted(TestRunStarted event) {
        spark = new ExtentSparkReporter("./ExtentReportResults.html");
        extent = new ExtentReports();
        // Create extent report instance with spark reporter

    // TestRunFinished event is triggered when all feature file executions are
    // completed
    private void runFinished(TestRunFinished event) {

    // This event is triggered when feature file is read
    // here we create the feature node
    private void featureRead(TestSourceRead event) {
        String featureSource = event.getUri().toString();
        String featureName = featureSource.split(".*/")[1];

        if (feature.get(featureSource) == null) {

            feature.putIfAbsent(featureSource, extent.createTest(featureName));

    // This event is triggered when Test Case is started
    // here we create the scenario node
    private void ScenarioStarted(TestCaseStarted event) {
        String featureName = event.getTestCase().getUri().toString();

        scenario = feature.get(featureName).createNode(event.getTestCase().getName());

    // step started event
    // here we creates the test node
    private void stepStarted(TestStepStarted event) {

        String stepName = " ";
        String keyword = "Triggered the hook :";

        // We checks whether the event is from a hook or step
        if (event.getTestStep() instanceof PickleStepTestStep) {
            // TestStepStarted event implements PickleStepTestStep interface
            // WHich have additional methods to interact with the event object
            // So we have to cast TestCase object to get those methods
            PickleStepTestStep steps = (PickleStepTestStep) event.getTestStep();
            stepName = steps.getStep().getText();
            keyword = steps.getStep().getKeyword();

        } else {
            // Same with HoojTestStep
            HookTestStep hoo = (HookTestStep) event.getTestStep();
            stepName = hoo.getHookType().name();

        step = scenario.createNode(Given.class, keyword + " " + stepName);

    // This is triggered when TestStep is finished
    private void stepFinished(TestStepFinished event) {

        if (event.getResult().getStatus().toString() == "PASSED") {
            step.log(Status.PASS, "This passed");

        } else if (event.getResult().getStatus().toString() == "SKIPPED")


            step.log(Status.SKIP, "This step was skipped ");
        } else {
            step.log(Status.FAIL, "This failed");



<! —  https://mvnrepository.com/artifact/com.aventstack/extentreports →

<! —  https://mvnrepository.com/artifact/io.cucumber/cucumber-testng →

<! —  https://mvnrepository.com/artifact/io.cucumber/cucumber-java →

<! —  https://mvnrepository.com/artifact/com.beust/jcommander →

    <! — https://mvnrepository.com/artifact/org.testng/testng →
<! —  https://mvnrepository.com/artifact/com.beust/jcommander →

<! —  https://mvnrepository.com/artifact/org.testng/testng →

<! —  https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java →



enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.