The right way is to use custom formatter (Plugins)
So what does this means ?
We are going to create our own report plugin using Extent report.
As in TestNG reporting, where we override the TestNG listeners to tell TestNG what to do for each action or events,
Here, we implements Cucumber listener class to listen to cucumber events and tell it what to do for each event.
Cucumber sends events for each action like scenario started, step started, step finished etc to this instance of the listener. So here we handle these events and tell cucumber what to do with these events (In our case we will tell it to log it to extent report).
There are two listener interfaces:
So as the name suggests, concurrentEventListener is thread-safe and the other is not. The implementation is exactly the same so we will use only the concurrentEventListner so that our framework is threadsafe when we have to run things in parallel.
Our TestListener :
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;