Integrating Extent Report in Selenium Java Project

Integrating Extent Reports adds visually rich, interactive, and highly detailed reporting capabilities to your automation framework. Extent Reports generate customizable HTML reports that capture:

     Test steps and status logs (pass/fail/info)
     Screenshots
     Execution time
     System/environment information

Steps to Integrate Extent Report in Selenium Project

Step 1: Add Extent Report in Dependency

Search for the Extent Report dependency on mvnrepository.com or search.maven.org. Choose the required version, and copy the dependency into your pom.xml if you are using Maven or build.gradle if you are using Gradle.

Step 2: Create ExtentReportManager.java class in Utils

This class acts as a centralized utility to initialize and manage the ExtentReports instance throughout the test execution.

Sample Code: 

package utils;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.reporter.ExtentSparkReporter;
import com.aventstack.extentreports.reporter.configuration.Theme;
public class ExtentReportManager {
    private static ExtentReports extent;
    public static ExtentTest test;
    public static ExtentReports getInstance() {
        if (extent == null) {
            // Configure the reporter and set report file location
            ExtentSparkReporter sparkReporter = new ExtentSparkReporter("testOutput/ExtentReport.html");
            sparkReporter.config().setTheme(Theme.DARK);
            sparkReporter.config().setDocumentTitle("Automation Report");
            // Create ExtentReports and attach the configured reporter
            extent = new ExtentReports();
            extent.attachReporter(sparkReporter);
        }
        return extent;
    }
    /**
     * Creates a new test entry in the Extent Report with the given test name.
     * Also assigns this test to the static 'test' variable for logging.
     *
     * @param testName The name of the test to be created in the report
     * @return ExtentTest object representing the created test
     */
    public static ExtentTest createTest(String testName) {
        test = getInstance().createTest(testName);
        return test;
    }
    // Flushes the ExtentReports data to the report file.
    public static void flush() {
        if (extent != null) {
            extent.flush();
        }
    }
}


Step 3: Update the BaseTest.java class

Modify your BaseTest class to initialize the report before any test runs and flush the report once tests are completed.

Sample Code: 

package utils;
import com.aventstack.extentreports.ExtentReports;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import static utils.ExtentReportManager.test;
public class BaseTest {
    // Declaring WebDriver instance for interacting with the browser
    protected WebDriver driver;
    protected static final String downloadPath = Paths.get(System.getProperty("user.dir")).toString();
    protected static ExtentReports extent;
    @BeforeClass
    public void setup(){ // Setup method to initialize WebDriver before any tests run
        //Ensures download path exists for extent report
        Path path = Paths.get(downloadPath);
        if (!Files.exists(path)) {
            try {
                Files.createDirectories(path);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //initializing extent report
        extent = ExtentReportManager.getInstance();
        //Create a test entry for test class
        test = ExtentReportManager.createTest(this.getClass().getSimpleName());
        test.info("Starting Test: " + this.getClass().getSimpleName());
        // Setting download path
        ChromeOptions options = new ChromeOptions();
        Map preference = new HashMap<>();
        preference.put("download.default_directory", downloadPath);
        options.setExperimentalOption("prefs", preference);
        driver = new ChromeDriver(); // Initialize Chrome browser driver
        driver.manage().window().maximize(); // Maximize browser window
        driver.get("https://www.qafeast.com/"); // Navigate to the URL
    }
    @AfterClass
    public void tearDown(){ // Tear down method to close browser after all tests finish
        if(driver != null){
            driver.quit(); // Quit the driver only if it was initialized
        }
        ExtentReportManager.flush();
    }
}

 

Step 4: Run the Tests with TestNG

Use your testng.xml suite file or run test classes from your IDE. The ExtentReportManager and BaseTest integration ensures each test class is logged in the report automatically.

Final Report Location

After running the test using testng.xml, the Extent Report will be generated at: ProjectDirectory/test-output/ExtentReport.html

Open this HTML file in your browser to view the complete test report.

 

Related Tutorials

Related Questions






Read more