DEV Community

Sainath Ramanathan
Sainath Ramanathan

Posted on • Updated on • Originally published at sainathramanathan.xyz

Extent Reports made easy using Cucumber Adapter

Report! Report! and yes we all need reports of whatever we do. This applies to test automation as well and we have a lot of options to do that. Cucumber gives out reporting feature which is readily available inside the cucumber package but it looks pretty old and not insightful. To make our lives easy we have a readymade solution for it and that's called Extent Reports.

Before getting into the details there are two ways to achieve this as mentioned below.

  • Long Route - Using the Extent Reports with customizable configuration

  • Short Route - Using the Extent Reports Adapter corresponding to the Cucumber version

The Long Route

If you are building a Maven-based project, add the following dependencies to your pom.xml

<!-- https://mvnrepository.com/artifact/com.vimalselvam/cucumber-extentsreport -->
<dependency>
    <groupId>com.vimalselvam</groupId>
    <artifactId>cucumber-extentsreport</artifactId>
    <version>3.1.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.aventstack/extentreports -->
<dependency>
    <groupId>com.aventstack</groupId>
    <artifactId>extentreports</artifactId>
    <version>4.1.5</version>
</dependency>
Enter fullscreen mode Exit fullscreen mode

Note: If you are using Java 7 and below you don't need to add the second dependency but for Java 8 and above, you should add both the dependencies.

The next step is to create a configuration XML file. In your folder structure go to src/test/ and create a new folder called 'resources' (src/test/resources). Inside 'resources', create a new file called extent-config.xml (or any other name that you prefer) and add the following code in it. This XML defines how to display your report in the HTML page.

<?xml version="1.0" encoding="UTF-8"?>
<extentreports>
  <configuration>
    <!-- report theme --> <!-- standard, dark -->
    <theme>standard</theme>

    <!-- document encoding -->  <!-- defaults to UTF-8 -->
    <encoding>UTF-8</encoding>

    <!-- protocol for script and stylesheets -->   <!-- defaults to https -->
    <protocol>https</protocol>

    <!-- title of the document -->
    <documentTitle>BDD Cucumber Framework Reports</documentTitle>

    <!-- report name - displayed at top-nav -->
    <reportName>Android - Cucumber Report</reportName>

    <!-- global date format override -->  <!-- defaults to yyyy-MM-dd -->
    <dateFormat>yyyy-MM-dd</dateFormat>

    <!-- global time format override -->   <!-- defaults to HH:mm:ss -->
    <timeFormat>HH:mm:ss</timeFormat>

    <!-- custom javascript -->
    <scripts>
      <![CDATA[
        $(document).ready(function() {
        });
      ]]>
    </scripts>

    <!-- custom styles -->
    <styles>
      <![CDATA[
      ]]>
    </styles>
  </configuration>
</extentreports>
Enter fullscreen mode Exit fullscreen mode

Now you have to configure the Runner class to map with Extent Reports.

import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(
        features= "src/test/resources",
        tags = {"@RegressionTest"},
        monochrome = true,
        glue= {"stepDef"},
        plugin = {"com.cucumber.listener.ExtentCucumberFormatter:target/html/ExtentReport.html"})

public class RunnerTest {

}
Enter fullscreen mode Exit fullscreen mode

Having set the plugin, the next step is to create an 'AfterClass' method to define the reporting parameters and to showcase the test results in the dashboard.

import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(
        features= "src/test/resources",
        tags = {"@RegressionTest"},
        monochrome = true,
        glue= {"stepDef"},
        plugin = {"com.cucumber.listener.ExtentCucumberFormatter:target/html/ExtentReport.html"})

public class RunnerTest {
Reporter.loadXMLConfig(new File("src/test/resources/extent-config.xml"));
Reporter.setSystemInfo("AUT Name", "Make My Trip");
Reporter.setSystemInfo("Environment", "Production");
Reporter.setTestRunnerOutput("BDD Cucumber Framework Reports");
}
Enter fullscreen mode Exit fullscreen mode

That's it! you have set up the reporter and when you execute the cases you can find the results under target/html/ExtentReport.html.

The Short Route

The Aventstack has always made our lives easy and to achieve this they have been releasing adapters for Cucumber versions 1, 2, 3, 4, and now for 5. Mind that 5 is in the test phase as of now since Cucumber is also new to 5, it is better to stick with version 4 of Cucumber at this point. You gotta pick the relevant adapter version corresponding to your Cucumber. I am using Cucumber 4 and I will be using the corresponding adapter which is 'extentreports-cucumber4-adapter'. Also for some, you may need to add the exclusions of 'cucumber-java' and 'cucumber-core' just to avoid the adapter from pulling its dependencies of Cucumber.

<dependency>
            <groupId>com.aventstack</groupId>
            <artifactId>extentreports-cucumber4-adapter</artifactId>
            <version>1.0.8</version>
            <exclusions>
                <exclusion>
                    <groupId>io.cucumber</groupId>
                    <artifactId>cucumber-java</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>io.cucumber</groupId>
                    <artifactId>cucumber-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
Enter fullscreen mode Exit fullscreen mode

As mentioned in the above setup (in The Long Route) you may need to create a resources folder in 'src/test/' and initiate a file called extent.properties. This property file is similar to the configuration file mentioned previously but this is alight weighted file and you need to write just 3 steps to spin your report.

  • Step 1 - Tells the reporter to start the reporting
  • Step 2 - Leave the config field empty as it is pre-defined in the adapter
  • Step 3 - Specify where the report should be placed
extent.reporter.html.start=true
extent.reporter.html.config=
extent.reporter.html.out=test-output/HtmlReport/ExtentHtml.html
Enter fullscreen mode Exit fullscreen mode

You're almost done. Just go to your Runner class and add the following plugin inside the CucumberOptions.

import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(
        features= "src/test/resources",
        tags = {"@RegressionTest"},
        monochrome = true,
        glue= {"stepDef"},
        plugin = {"com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:"})

public class RunnerTest {

}
Enter fullscreen mode Exit fullscreen mode

This is so simple and you don't need to remember a lot of steps to set up the report. I prefer the short route to be employed and of course, if you need the customization, go for the long route. I have also added an example in my GitHub repository.

GitHub logo rksainath / MMT-CukeTest

This is a cucumber test framework with added reporting feature using Extent Report

Top comments (2)

Collapse
 
jeremydidier86 profile image
jeremydidier86

Hi,
thank you for this great presentation! Do you know if there is a way to remove a test from the report ( like in extent reports with removeTest() method)?
Have a nice day!

Jérémy

Collapse
 
rksainath profile image
Sainath Ramanathan

Hi,
As you rightly said, you could use removeTest() method from version 4 onwards. Also, you could skip unfinished tests from getting logged. For further reference check here extentreports.com/docs/versions/3/...