What are Are Extent Reports in Selenium (and their use)?

Extent Reports in Selenium and How to Generate Them?

Extent Reports in Selenium

Automated testing is an important process in the software development lifecycle, especially with tools like Selenium for testing web applications. However, one of the major challenges many testers face is creating readable and informative test reports. While Selenium itself can run tests efficiently, but it does not provide any advanced reporting feature. Here comes the role of Extent Reports in Selenium.

Extent Reports is a powerful library that improves the test reporting process by generating detailed, interactive, and high-quality reports. In this blog, we’ll learn more about Extent Reports, how to use them with Selenium, and how to generate and customize reports effectively.

What are Extent Reports?

Extent Reports is an open-source reporting library that is useful for high-quality and interactive HTML reports for test execution. It is majorly used with Selenium WebDriver to provide detailed information about test cases, such as:

  • Test results (Pass, Fail, Skip)
  • Execution time
  • Logs and error messages
  • Screenshots of failed tests

Unlike traditional logging methods, Extent Reports provide a more detailed view of the test execution with a user-friendly interface. The tool offers different features like charts, graphs, and easy navigation for faster analysis. This makes it easier for developers and QA engineers to understand the test results. Understanding alert handling in selenium is crucial for generating accurate extent reports.

Key Features of Extent Reports

Some of the primary features of Extent Reports in the software testing domain are:

  • The reports are dynamic and user-friendly, making it easier to understand the test results from the generated report.
  • Includes charts, graphs, and other visual elements to show trends and overall test automation status.
  • The report’s layout and content can be easily customized as per your needs.
  • Automatically captures and attaches screenshots for failed test cases, making debugging simpler.

Using Extent Reports in Selenium

Integrating Extent Reports with Selenium WebDriver is a simple process for the developers. The tool provides a simple API that allows testers to log details of each test step, capture results, and generate a report. It is particularly useful in large-scale test automation, where manually analyzing logs can be difficult.

Setting Up Extent Reports in Selenium

Follow these simple steps to set Extent Reports in Selenium:

Step 1: Firstly, add the Extent Reports dependency to your project. If you are using Maven, you can add the following dependency to your pom.xml file:

<dependency>
    <groupId>com.aventstack</groupId>
    <artifactId>extentreports</artifactId>
    <version>5.1.1</version>
</dependency>

For Gradle, you can add the following line in your build.gradle file:

implementation 'com.aventstack:extentreports:5.1.1'

Step 2: After adding the dependency, start ExtentReports in your test script. You need to create an ExtentReports instance, mention a path for the report, and start an ExtentTest object for each test.

Step 3: You can log various information (such as pass, fail, or skip) using the methods provided by Extent Reports.

Step 4: After all tests have run, finalize the report using flush() to save all logs and generate the final HTML report, which you can download for later.

Example

Consider an example to generate a report including details like the status of the test (Pass/Fail), execution time, and any error messages if the test fails.

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class TestReporting {
    public static void main(String[] args) {

        ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter("extentReport.html");
        ExtentReports extent = new ExtentReports();
        extent.attachReporter(htmlReporter);
        WebDriver driver = new ChromeDriver();
        ExtentTest test = extent.createTest("Google Search Test");
        try {
            driver.get("https://www.google.com");
            test.pass("Navigated to Google");
        } catch (Exception e) {
            test.fail("Test failed due to exception: " + e.getMessage());
        } finally {
            driver.quit();
            extent.flush(); // Save the report
        }
    }
}

How to Generate Extent Reports?

Here are the steps to be followed for generating Extent Reports in Selenium:

Step 1: Set Up Extent HTML Reports

The first step in generating a report is to create an instance of ExtentHtmlReporter, which will create an HTML file for the report. You can customize the file name and location based on your needs.

ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter("extentReport.html");

Step 2: Attach the Reporter to ExtentReports

Once the HTML reporter is set up, attach it to the ExtentReports instance. This is where the test results will be logged.

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

Step 3: Create and Log Test Details

This can be done using the createTest() method of ExtentReports. As you run your test steps, you can log the results (pass, fail, skip, etc.) using various logging methods like test.pass(), test.fail(), or test.skip().

ExtentTest test = extent.createTest("Google Search Test");
test.pass("Navigated to Google");

Step 4: Finalize the Report and View it

Once all the tests are executed, finalize the report by calling the flush() method on the ExtentReports instance. This writes the report data to the HTML file and saves it.

extent.flush();

How to Capture Screenshots in Extent Report

While generating reports for testing, it’s important to capture screenshots for failed tests. This is helpful in checking the issues more quickly and propose a feasible solution. ExtentReports class allows you to attach screenshots for each test failure.

Here are some of the steps to be followed for capturing screenshots in Extent Report:

Step 1: You can capture screenshots by using the TakesScreenshot interface in Selenium and store the screenshot file in a particular folder.

Step 2: After capturing the screenshot, you can attach it to the report using the addScreenCaptureFromPath() method of the ExtentTest object.

Example

In this code, if the test fails, a screenshot will be captured and added to the report.

import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.io.FileHandler;
import java.io.File;
public class TestWithScreenshot {
    public static void main(String[] args) throws Exception {

        ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter("extentReport.html");
        ExtentReports extent = new ExtentReports();
        extent.attachReporter(htmlReporter);

        WebDriver driver = new ChromeDriver();
        ExtentTest test = extent.createTest(“Google Search Test”);

        try {
            driver.get("https://www.google.com");
            test.pass("Navigated to Google");
        } catch (Exception e) {
            File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
            FileHandler.copy(screenshot, new File("screenshot.png"));
            test.fail("Test failed due to exception").addScreenCaptureFromPath("screenshot.png");
        } finally {
            driver.quit();
            extent.flush();
        }
    }
}

Benefits of Using Extent Reports

  1. Improved Test Visualization: Extent Reports provides high-quality, detailed reporting in HTML that make it easier to understand test results instantly.
  2. Faster Debugging: With features like screenshots and detailed error logs, debugging failed tests becomes quicker and more efficient.
  3. Customizability: The reports can be customized in terms of appearance and content, allowing you to add specific details like browser version, environment settings, or any other test metadata.
  4. Real-Time Monitoring: Extent Reports can also be integrated with major testing CI/CD tools like Jenkins, allowing real-time monitoring of test results during automated test runs.

Conclusion

Extent Reports is a reporting library useful for test automation and to easily generate HTML reports with test results, failed tests, etc. With its seamless integration into Selenium, easy setup, and powerful features like test case logs, screenshots, and interactive charts, it improves the overall test reporting process. Even it allows users to modify an existing report or extract its content for better explanation of the test cases.

Further Readings

Written by

The Test Tribe

Leave a Reply

Your email address will not be published.

Related Posts

Check Thrive EdSchool

Advertisement (Know More)

Get Top Community News

    Top Event and other The Test Tribe updates to your Inbox.

     

    Categories

    Tags

    [sibwp_form id=2]
    The Test Tribe Logo
    Privacy Overview

    This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.