Sunday, November 29, 2026

How ExtentReports Supports Parallel Execution in Selenium Automation

How ExtentReports Supports Parallel Execution in Selenium Automation

ExtentReports supports parallel test execution, including Selenium with TestNG, Cucumber, and JUnit.

When multiple test cases execute simultaneously, ExtentReports can capture logs, screenshots, test steps, and execution results from multiple threads in a single HTML report.

However, you must implement thread-safe reporting to avoid test logs getting mixed between parallel test cases.

1. How ExtentReports Handles Parallel Execution

Suppose you execute four test cases using TestNG with parallel execution enabled.

How ExtentReports Supports Parallel Execution in Selenium Automation

 


Parallel Test Execution

  • Test Case 1 - Thread 1 executing
  • Test Case 2 - Thread 2 executing
  • Test Case 3 - Thread 3 executing
  • Test Case 4 - Thread 4 executing

Each thread sends its test results to a thread-safe ExtentReports implementation.

The final output is a single combined ExtentReport.html containing all test results.

Each thread gets its own ExtentTest instance, while all threads share a single ExtentReports instance.

2. Important Classes for Parallel Execution

Class Purpose
ExtentReports Creates and manages the report.
ExtentTest Stores an individual test's logs.
ThreadLocal Maintains separate test instances per thread.
ExtentSparkReporter Generates the HTML report.
flush() Writes the final report to disk.

Most important: Use ThreadLocal<ExtentTest> to prevent parallel tests from writing logs into each other's test instances.

3. Complete Java Implementation Using ThreadLocal

The following implementation uses ExtentReports 5.x with Selenium and TestNG.

Step 1: Maven Dependencies

Add the following dependencies to pom.xml.

<dependencies>
    <dependency>
        <groupId>com.aventstack</groupId>
        <artifactId>extentreports</artifactId>
        <version>5.1.2</version>
    </dependency>

    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.11.0</version>
    </dependency>

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.38.0</version>
    </dependency>
</dependencies>

Step 2: Create ExtentManager.java

This class creates one ExtentReports instance for the complete execution and maintains a separate ExtentTest instance for each thread.

package utilities;

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.reporter.ExtentSparkReporter;

public class ExtentManager {

    private static ExtentReports extent;

    private static ThreadLocal<ExtentTest> test =
            new ThreadLocal<>();

    public static synchronized ExtentReports getExtent() {

        if (extent == null) {

            ExtentSparkReporter spark =
                    new ExtentSparkReporter(
                            "test-output/ExtentReport.html");

            extent = new ExtentReports();
            extent.attachReporter(spark);

            extent.setSystemInfo("Project", "Selenium Automation");
            extent.setSystemInfo("Framework", "TestNG");
            extent.setSystemInfo("Execution", "Parallel");

        }

        return extent;
    }

    public static void createTest(String testName) {

        ExtentTest extentTest =
                getExtent().createTest(testName);

        test.set(extentTest);
    }

    public static ExtentTest getTest() {
        return test.get();
    }

    public static void removeTest() {
        test.remove();
    }
}

Step 3: Create ExtentTestNGListener.java

This listener automatically creates test entries and captures pass, fail, and skip results.

package listeners;

import org.testng.ITestListener;
import org.testng.ITestResult;
import org.testng.IExecutionListener;

import utilities.ExtentManager;

public class ExtentTestNGListener
        implements ITestListener, IExecutionListener {

    @Override
    public void onTestStart(ITestResult result) {

        ExtentManager.createTest(
                result.getMethod().getMethodName());

        ExtentManager.getTest()
                .info("Test execution started");
    }

    @Override
    public void onTestSuccess(ITestResult result) {

        ExtentManager.getTest()
                .pass("Test Passed");

        ExtentManager.removeTest();
    }

    @Override
    public void onTestFailure(ITestResult result) {

        ExtentManager.getTest()
                .fail(result.getThrowable());

        ExtentManager.removeTest();
    }

    @Override
    public void onTestSkipped(ITestResult result) {

        ExtentManager.getTest()
                .skip("Test Skipped");

        ExtentManager.removeTest();
    }

    @Override
    public void onExecutionFinish() {

        ExtentManager.getExtent().flush();
    }
}

Step 4: Create TestNG Test Class

package tests;

import org.testng.Assert;
import org.testng.annotations.Test;

import utilities.ExtentManager;

public class LoginTest {

    @Test
    public void validLoginTest() {

        ExtentManager.getTest()
                .info("Executing valid login test");

        ExtentManager.getTest()
                .info("Entering valid username");

        ExtentManager.getTest()
                .info("Entering valid password");

        Assert.assertTrue(true);

        ExtentManager.getTest()
                .pass("Login successful");
    }

    @Test
    public void invalidLoginTest() {

        ExtentManager.getTest()
                .info("Executing invalid login test");

        ExtentManager.getTest()
                .info("Entering invalid credentials");

        Assert.assertTrue(false);
    }

    @Test
    public void logoutTest() {

        ExtentManager.getTest()
                .info("Executing logout test");

        Assert.assertTrue(true);
    }
}

Step 5: Configure testng.xml

Enable parallel execution using the parallel and thread-count attributes.

<!DOCTYPE suite SYSTEM
 "https://testng.org/testng-1.0.dtd">

<suite name="Parallel Automation Suite"
       parallel="methods"
       thread-count="3">

    <listeners>
        <listener class-name=
        "listeners.ExtentTestNGListener"/>
    </listeners>

    <test name="Parallel Tests">

        <classes>
            <class name="tests.LoginTest"/>
        </classes>

    </test>

</suite>

Step 6: Execute the Tests

Run the following Maven command:

mvn clean test

Expected execution:

Running Parallel Automation Suite

Thread-1: validLoginTest       PASS
Thread-2: invalidLoginTest     FAIL
Thread-3: logoutTest           PASS

================================
Tests run: 3
Passed: 2
Failed: 1
Skipped: 0
================================

Generated report:

test-output/ExtentReport.html

The report contains all three test cases, their execution steps, and their respective statuses.

4. How Does ThreadLocal Prevent Report Conflicts?

Without ThreadLocal

  • Thread 1 creates LoginTest.
  • Thread 2 creates LogoutTest.
  • Both threads access the same ExtentTest reference.
  • Test logs can become associated with the wrong test.

With ThreadLocal

  • Thread 1 gets its own ExtentTest.
  • Thread 2 gets its own ExtentTest.
  • Thread 3 gets its own ExtentTest.
  • Each thread maintains independent test logs.

For ExtentReports 5, the reporting API is designed for multithreaded use. The recommended approach is one shared ExtentReports instance per JVM.

5. Important Best Practices

Component Recommended Approach
ExtentReports One static instance
ExtentTest ThreadLocal instance
WebDriver ThreadLocal instance
Screenshot Unique file per test
Report flush Once after execution
TestNG listener Capture test lifecycle
Cleanup Call ThreadLocal.remove()

Important: ThreadLocal for ExtentTest does not automatically make Selenium WebDriver thread-safe. Maintain a separate ThreadLocal WebDriver when running browser tests in parallel.

6. Interview Questions

Q1. Is ExtentReports thread-safe?

Yes. ExtentReports 5 supports multithreaded execution. Its recommended architecture uses a single ExtentReports instance.

Q2. Why do we use ThreadLocal in ExtentReports?

To maintain an independent ExtentTest object for each executing thread.

Q3. Can multiple threads write into one HTML report?

Yes. Multiple threads can contribute test results to the same report.

Q4. Should we create multiple ExtentReports objects for parallel tests?

Generally, no. Use one instance per JVM execution when you need one consolidated report.

Q5. When should we call flush()?

After all tests have completed, not after every individual test.

Q6. Does ExtentReports support parallel Cucumber execution?

Yes. You can use the same reporting concept with Cucumber hooks, maintaining scenario-specific ExtentTest references using ThreadLocal or scenario-scoped dependency injection.

7. Recommended Framework Architecture

For a Selenium + TestNG + Maven automation framework:

src/test/java
    |
    |-- base
    |     |-- BaseTest.java
    |     |-- DriverFactory.java
    |
    |-- utilities
    |     |-- ExtentManager.java
    |     |-- ScreenshotUtil.java
    |
    |-- listeners
    |     |-- ExtentTestNGListener.java
    |
    |-- tests
          |-- LoginTest.java
          |-- SearchTest.java
          |-- LogoutTest.java

test-output
    |-- ExtentReport.html
    |-- screenshots

My recommendation: Use ThreadLocal<WebDriver> + ThreadLocal<ExtentTest> + a shared ExtentReports instance. This is a good foundation for a parallel Selenium automation framework.

Official reference: ExtentReports 5 Java Documentation.

No comments:

Post a Comment