Introduction to Selenium
Selenium WebDriver Basics
WebDriver Commands
Synchronization in Selenium
Working with Different Browsers
Setting up WebDriver for different browsers
Handling Advanced User Interactions
Page Object Model (POM)
Introduction to POM
TestNG Framework
Creating and Running TestNG Tests

Generating HTML reports in Selenium with Java is an essential part of the test automation process. These reports provide a detailed summary of the test execution, including which tests passed or failed, and other relevant details. There are several libraries and frameworks available to generate HTML reports in Selenium with Java, such as:

  1. TestNG: TestNG is a testing framework inspired by JUnit and NUnit but introduces some new functionalities that make it more powerful and easier to use. TestNG has built-in support for generating HTML reports.

2. ExtentReports: ExtentReports is an open-source reporting library that can be integrated with TestNG, JUnit, and other testing frameworks. It provides a rich set of features and customization options for generating interactive and visually appealing HTML reports.

3. Allure: Allure is a flexible, lightweight, and multi-language test reporting tool that provides clear graphical reports. It can be integrated with various testing frameworks and CI/CD tools.

1. Generating HTML Reports with TestNG

TestNG provides a default HTML report that is automatically generated after running the test suite. Here’s how you can use it:

Example:

1. Create a TestNG XML File:

				
					<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
    <test name="Test">
        <classes>
            <class name="com.example.tests.SampleTest"/>
        </classes>
    </test>
</suite>

				
			

2. Create a Sample Test Class:

				
					package com.example.tests;

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

public class SampleTest {

    @Test
    public void testMethod1() {
        Assert.assertTrue(true);
    }

    @Test
    public void testMethod2() {
        Assert.assertTrue(false);
    }
}

				
			

3. Run the TestNG Suite:

  • Execute the TestNG XML file in your IDE or via the command line.
  • After execution, TestNG generates a default HTML report at test-output/index.html

2. Generating HTML Reports with ExtentReports

ExtentReports provides more detailed and visually appealing reports. Here’s how you can use ExtentReports with Selenium and TestNG:

Step-by-Step Guide:

1. Add ExtentReports Dependency:

If you are using Maven, add the following dependencies to your ‘pom.xml‘ file:

				
					<dependencies>
    <dependency>
        <groupId>com.aventstack</groupId>
        <artifactId>extentreports</artifactId>
        <version>5.0.9</version>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.4.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

				
			

2. Create a Test Listener Class:

				
					package com.example.utils;

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.Theme;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

public class ExtentTestNGITestListener implements ITestListener {
    private static ExtentReports extent;
    private static ThreadLocal<ExtentTest> test = new ThreadLocal<>();

    @Override
    public void onStart(ITestContext context) {
        ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter("extent.html");
        htmlReporter.config().setDocumentTitle("Automation Report");
        htmlReporter.config().setReportName("Test Report");
        htmlReporter.config().setTheme(Theme.STANDARD);

        extent = new ExtentReports();
        extent.attachReporter(htmlReporter);
        extent.setSystemInfo("Environment", "Test");
        extent.setSystemInfo("User", "Tester");
    }

    @Override
    public void onTestStart(ITestResult result) {
        ExtentTest extentTest = extent.createTest(result.getMethod().getMethodName());
        test.set(extentTest);
    }

    @Override
    public void onTestSuccess(ITestResult result) {
        test.get().pass("Test passed");
    }

    @Override
    public void onTestFailure(ITestResult result) {
        test.get().fail(result.getThrowable());
    }

    @Override
    public void onTestSkipped(ITestResult result) {
        test.get().skip(result.getThrowable());
    }

    @Override
    public void onFinish(ITestContext context) {
        extent.flush();
    }
}

				
			

3. Update TestNG XML File to Include the Listener:

				
					<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
    <listeners>
        <listener class-name="com.example.utils.ExtentTestNGITestListener"/>
    </listeners>
    <test name="Test">
        <classes>
            <class name="com.example.tests.SampleTest"/>
        </classes>
    </test>
</suite>

				
			

4. Create a Sample Test Class (same as above):

				
					package com.example.tests;

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

public class SampleTest {

    @Test
    public void testMethod1() {
        Assert.assertTrue(true);
    }

    @Test
    public void testMethod2() {
        Assert.assertTrue(false);
    }
}

				
			

5. Run the TestNG Suite:

  • Execute the TestNG XML file in your IDE or via the command line.
  • After execution, an HTML report named ‘extent.html‘ will be generated in the project root directory.

By following these steps, you can generate detailed and visually appealing HTML reports for your Selenium test executions using TestNG and ExtentReports.

Scroll to Top