Integrating Extent Report and Screenshots with Listeners


Elevate your TestNG reporting capabilities by integrating Extent Reports and screenshots through listeners. This advanced technique enhances the clarity and depth of your test execution summaries. With Extent Reports and custom listeners, you can provide detailed insights, including screenshots, making it easier to diagnose issues and communicate test results effectively.

What are Extent Reports?

Extent Reports is an open-source reporting library for Java, designed to create interactive and detailed HTML-based reports for test executions. It is widely used in the Selenium WebDriver community to generate comprehensive and visually appealing reports. Extent Reports simplifies the process of tracking and analyzing test results, making it an essential tool for both testers and stakeholders.

Key Features of Extent Reports

  1. Interactive Dashboards

  2. Detailed Test Logs

  3. Screenshots and Media Attachments

  4. Parallel Test Execution Support

Implementing Extent Reports

Step 1: Add Extent Reports Dependency

Make sure you have the Extent Reports dependency in your project's pom.xml file:

<!-- -->

Step 2: Create an EventListener Class for Screenshots and Logs

Create an EventListener class and extend it to the BaseClass (assuming your driver is present here) and implement the ITestListener interface which contains all the listener methods.


import org.apache.logging.log4j.core.util.internal.HttpInputStreamUtil.Result;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.markuputils.ExtentColor;
import com.aventstack.extentreports.markuputils.MarkupHelper;
import com.aventstack.extentreports.reporter.ExtentSparkReporter;
import com.aventstack.extentreports.reporter.configuration.Theme;
import com.mystore.base.BaseClass;

public class EventListner extends BaseClass implements ITestListener {

    ExtentSparkReporter htmlReporter;
    ExtentReports reports;
    ExtentTest test;

    public void configureReport() {
        htmlReporter = new ExtentSparkReporter("Report.html");
        reports = new ExtentReports();

        htmlReporter.config().setDocumentTitle("Rinaldo Demo");
        htmlReporter.config().setReportName("First Name : Pankaj");


    public static void captureScreenshot(WebDriver driver, String screenshotName) {
        // Convert WebDriver object to TakesScreenshot
        TakesScreenshot ts = (TakesScreenshot) driver;

        // Capture screenshot as File
        File source = ts.getScreenshotAs(OutputType.FILE);

        // Define destination for the screenshot
        String destination = System.getProperty("user.dir") + "\\ScreenShot\\" + screenshotName + ".png";
        File target = new File(destination);

        try {
            // Copy file to the destination
            FileUtils.copyFile(source, target);
        } catch (IOException e) {

    public void onTestStart(ITestResult result) {
        System.out.println("Test Started: " + result.getName());

    public void onTestSuccess(ITestResult result) {
        System.out.println("Test Passed: " + result.getName());

    public void onTestFailure(ITestResult result) {
        test = reports.createTest(result.getName());
        test.log(Status.FAIL, MarkupHelper.createLabel("Name of failed test" + result.getName(), ExtentColor.RED));
        captureScreenshot(driver, result.getName());
        test.addScreenCaptureFromPath("F:\\Selenium\\EcomAutomationProject\\MyStore\\ScreenShot\\" + result.getName() + ".png", result.getName());
//        test = reports.createTest(result.getStatus());
        System.out.println("Test Failed: " + result.getName());

    public void onTestSkipped(ITestResult result) {
        System.out.println("Test Skipped: " + result.getName());

    public void onStart(ITestContext context) {
        System.out.println("Test Suite Started: " + context.getName());

    public void onFinish(ITestContext context) {
        System.out.println("Test Suite Finished: " + context.getName());

Step 3: Configure the testing.xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "">
<suite name="Suite">
        <listener class-name="com.mystore.utility.EventListner"></listener>
  <test name="Test">
      <class name="com.mystore.testcases.LoginTest"/> 
  </test> <!-- Test -->
</suite> <!-- Suite -->

Step 4: Run the testing.xml file

Right-click on the testing.xml file and Click on "Run As" the select "TestNG Suite".

If you want to check out the entire source code of the project click here.


Integrating Extent Reports and screenshots enriches TestNG reporting, fostering better collaboration and providing a holistic view for quicker issue resolution. Upgrade your testing suite for more informative and actionable results.