How to generate HTML reports in Selenium Tests: A Step-by-Step Guide Using Java and TestNG


In the world of Selenium testing, creating informative and visually appealing test reports is crucial for effective communication and debugging. One powerful way to achieve this is to generate HTML reports in Selenium. In this tutorial, we’ll walk you through the process of creating HTML reports for your Selenium tests using Java and TestNG.

Why to generate HTML Reports in Selenium?

HTML reports offer a comprehensive view of your test results, making it easier to identify issues and share insights with stakeholders. They provide a user-friendly interface with detailed information on test pass/fail status, execution time, and more.

Step 1: Set Up Dependencies

Ensure you have the necessary dependencies in your project. Add the following Maven dependencies:

    <!-- Selenium WebDriver -->

    <!-- TestNG -->

Step 2: Create Test Class

Create a Selenium test class and annotate it with TestNG annotations. Below is a sample test class:

import org.openqa.selenium.WebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class MySeleniumTest {
    WebDriver driver;

    public void setUp() {
        // Set up WebDriver (e.g., ChromeDriver)
        System.setProperty("", "path/to/chromedriver");
        driver = new ChromeDriver();

    public void myTest() {
        // Your test logic here
        Assert.assertEquals(driver.getTitle(), "Example Page Title");

    public void tearDown() {
        // Close the WebDriver
        if (driver != null) {
Step 3: Integrate ExtentReports

Add the ExtentReports library to your project. You can download it from ExtentReports GitHub or add it as a Maven dependency.

Step 4: Generate HTML Reports in Selenium

Enhance your test class to create an HTML report using ExtentReports:

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;

public class MySeleniumTest {
    // ... (previous code)

    ExtentReports extent;
    ExtentTest test;

    public void setUp() {
        // ... (previous setup code)

        // Initialize ExtentReports and attach HTML reporter
        extent = new ExtentReports();
        ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter("test-output/extent-report.html");

        // Start a test
        test = extent.createTest("MySeleniumTest");

    public void myTest() {
        // ... (previous test code)

        // Log test status to the HTML report
        test.log(Status.PASS, "Test passed successfully");

    public void afterMethod(ITestResult result) {
        // Log test status based on the test result
        if (result.getStatus() == ITestResult.FAILURE) {
            test.log(Status.FAIL, "Test failed: " + result.getThrowable());
        } else if (result.getStatus() == ITestResult.SKIP) {
            test.log(Status.SKIP, "Test skipped: " + result.getThrowable());

    public void tearDown() {
        // ... (previous teardown code)

        // Flush the report to save the results

By following these steps, you can seamlessly integrate HTML reporting into your Selenium tests using Java and TestNG and generate HTML reports in Selenium tests. This approach provides clear and detailed insights into your test execution, aiding in effective test management and debugging.

Feel free to customize the HTML report layout and appearance based on your project’s needs. Happy testing!

You may also read

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.