Report Generation in Selenium Using Extent Reports

Extent Reports is a powerful reporting library that provides detailed insights into your Selenium test executions. It allows you to create visually appealing HTML reports that include logs, screenshots, and other relevant information.

  • Setting Up Extent Reports

To begin using Extent Reports in your Selenium project, follow these steps:

Add Dependencies: Ensure you have the Extent Reports library added to your project. You can download the JAR files and include them in your build path.

Initialize Extent Reports: Create an instance of ExtentReports and attach a reporter to it.

ExtentReports extent = new ExtentReports(“path/to/report.html”, true);

ExtentSparkReporter spark = new ExtentSparkReporter(“path/to/report.html”);

extent.attachReporter(spark);

  • Creating Test Instances

For each test case, create an ExtentTest instance to log the results.

ExtentTest test = extent.createTest(“Test Case Name”);

  • Logging Test Steps

You can log various test steps and their outcomes using the log method. This includes passing, failing, or skipping tests.

test.log(Status.PASS, “Step description for passing step”);

test.log(Status.FAIL, “Step description for failing step”);

  • Capturing Screenshots

To enhance your reports, you can capture screenshots during test failures. Use the following method to capture and log screenshots:

public static String captureScreenshot(WebDriver driver) throws IOException {

  File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

  String destFile = “path/to/screenshots/” + System.currentTimeMillis() + “.png”;

  FileUtils.copyFile(scrFile, new File(destFile));

  return destFile;

}

// Log screenshot on failure

test.log(Status.FAIL, “Test failed”, MediaEntityBuilder.createScreenCaptureFromPath(captureScreenshot(driver)).build());

  • Finalizing the Report

At the end of your test execution, ensure to flush the report to save all logged information.

extent.flush();

  • Example Code

Here’s a complete example demonstrating how to set up Extent Reports in a Selenium test:

import org.openqa.selenium.By;

import org.openqa.selenium.OutputType;

import org.openqa.selenium.TakesScreenshot;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import com.aventstack.extentreports.ExtentReports;

import com.aventstack.extentreports.ExtentTest;

import com.aventstack.extentreports.Status;

import com.aventstack.extentreports.reporter.ExtentSparkReporter;

import org.apache.commons.io.FileUtils;

import java.io.File;

import java.io.IOException;

public class ExtentReportExample {

  public static void main(String[] args) throws IOException {

    System.setProperty(“webdriver.chrome.driver”, “path/to/chromedriver”);

    WebDriver driver = new ChromeDriver();

     

    ExtentReports extent = new ExtentReports(“path/to/report.html”, true);

    ExtentSparkReporter spark = new ExtentSparkReporter(“path/to/report.html”);

    extent.attachReporter(spark);

     

    ExtentTest test = extent.createTest(“Google Test”);

     

    driver.get(“https://www.google.com”);

    if (driver.getTitle().equals(“Google”)) {

      test.log(Status.PASS, “Navigated to Google successfully”);

    } else {

      test.log(Status.FAIL, “Failed to navigate to Google”, MediaEntityBuilder.createScreenCaptureFromPath(captureScreenshot(driver)).build());

    }

     

    extent.flush();

    driver.quit();

  }

  public static String captureScreenshot(WebDriver driver) throws IOException {

    File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

    String destFile = “path/to/screenshots/” + System.currentTimeMillis() + “.png”;

    FileUtils.copyFile(scrFile, new File(destFile));

    return destFile;

  }

}

Benefits of Using Extent Reports

  • Visual Representation: Provides a clear and visually appealing representation of test results.
  • Detailed Logs: Allows for detailed logging of each test step, making it easier to identify issues.
  • Screenshots: Captures screenshots on failure, aiding in debugging.
  • Customizable: Offers customization options for reports to fit specific needs.

Conclusion

Using Extent Reports in Selenium enhances the reporting capabilities of your test automation framework. By following the steps outlined above, you can create comprehensive reports that provide valuable insights into your test executions.

Leave a comment

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