Setup Custom Extent Reports from Scratch

Japneet Sachdeva
4 min readMay 7, 2024

--

Extent Report

Selenium is a great tool when it comes to reporting, as it provides inbuilt reports using various frameworks like TestNG and JUnit. Though a few built-in reports are available, they do not provide information on the steps executed as part of the test suite.

Therefore, custom reporting needs to be implemented to make it convenient for all major project stakeholders. Extent Report is an open-source library used for generating test reports in automation testing. It has been more widely used for report generation than the inbuilt reports in various test frameworks because of its enhanced features and customisation.

Prerequisites for generating extent reports

  1. Java: Download and install Java in the system, if not already than use this link to download.
  2. Selenium: For framing our automation test cases, we need to install Selenium by adding a maven dependency in the pom.xml file.

3. Extent Reports: To integrate our automation framework, we need the Extent Report dependency either as a maven dependency or a jar file. In the case of a maven project, add maven dependency in the pom.xml file.

You can also download the jar file and add the jar to the build path. You can refer to the mvn repository for adding the Extent Report dependency.

4. TestNG: To design and execute the tests, we shall use TestNG by adding the TestNG dependency.

How to generate and customise the Extent Reports?

There are three classes that are used for generating and customizing the Extent Reports in Selenium. They are:

  1. ExtentHtmlReporter
  2. ExtentReports
  3. ExtentTest

The ExtentHtmlReporter is used for creating an HTML file, and it accepts a file path as a parameter. The file path represents the path in which our extent report would be generated.

ExtentHtmlReporter is also used to customize the extent reports. It allows many configurations to be made through the config() method. Some of the configurations that can be made are described below.

In a nutshell,

  • The ExtentHtmlReporter class is used for creating the HTML reports.
  • The ExtentReports class is used for creating the tests.
  • The ExtentTest class is used for generating the logs in the Extent Report.

Use below Configuration, for quick setup:

String reportPath = System.getProperty(“user.dir”)+”/Reports/extentReport.html”;
ExtentSparkReporter sparkReporter = new ExtentSparkReporter(reportPath);

sparkReporter.config().setTimeStampFormat(“EEEE, MMMM dd, yyyy, hh:mm a ‘(‘zzz’)’”); //specific date format
sparkReporter.config().setTheme(Theme.DARK);
sparkReporter.config().setReportName(“Automation Results”);
sparkReporter.config().setDocumentTitle(“Test Results”);

reports = new ExtentReports();
reports.attachReporter(sparkReporter);

reports.setSystemInfo(“Tester is: “, “Japneet Sachdeva”);

So now, we have gone through various configurations that can be made to customize the look of the Extent Reports in Selenium. We can initialize the ExtentHtmlReporter and add the required configurations in the @BeforeTest TestNG annotation.

This can be defined within our BaseTest class file, like below:

Make sure to use static and ThreadLocal to enable Parallel execution within framework:

ThreadLocal: enables us to create standalone thread for each test execution and retain the results. Later after test is completed than those can be reported within our extent report.

Static: when we create a static variables, its value remains constant across all other classes, and we do not have to create an instance to use the variable. This way, we are creating the variable once, so memory is only allocated once.

To update your test results within the report use below code:

For updating Base 64 encoded screenshots within reports for hassle free sharing:

How to use the extent report test object within the test classes:

This setup can be used with local or grid execution within cloud/remote/on-premise.

-x-x-

To learn and implement industry standard Test automation framework, use my latest course: Link

Direction less..? need career guidance, schedule here: Link

Follow me on LinkedIn: https://www.linkedin.com/in/japneet-sachdeva/

#japneetsachdeva

--

--