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

Introduction:

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:

<dependencies>
    <!-- Selenium WebDriver -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.141.59</version>
    </dependency>

    <!-- TestNG -->
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.3.0</version>
    </dependency>
</dependencies>

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.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class MySeleniumTest {
    WebDriver driver;

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

    @Test
    public void myTest() {
        // Your test logic here
        driver.get("https://example.com");
        Assert.assertEquals(driver.getTitle(), "Example Page Title");
    }

    @AfterClass
    public void tearDown() {
        // Close the WebDriver
        if (driver != null) {
            driver.quit();
        }
    }
}
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.

<dependency>
    <groupId>com.aventstack</groupId>
    <artifactId>extentreports</artifactId>
    <version>5.0.7</version>
</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;

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

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

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

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

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

    @AfterMethod
    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());
        }
    }

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

        // Flush the report to save the results
        extent.flush();
    }
}
Conclusion:

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.