ThisCalculator

Everyday

Generate Extent Reports with Selenium in Java

Updated · Standard formula, shown below

This guide walks you through setting up and using Extent Reports with Selenium WebDriver in Java to create detailed, interactive HTML test reports. You’ll learn how to add the dependency, initialize the reporter, log test steps, and generate a report that shows pass/fail status, screenshots, and logs.

What You’ll Need

  • Java Development Kit (JDK) 8 or higher
  • Maven or Gradle build tool
  • An existing Selenium Java project with WebDriver tests
  • Basic knowledge of TestNG or JUnit (examples use TestNG)

Step-by-Step

  1. Add Extent Reports dependency – In your pom.xml (Maven) add:
    <dependency>
        <groupId>com.aventstack</groupId>
        <artifactId>extentreports</artifactId>
        <version>5.1.1</version>
    </dependency>

    For Gradle, add implementation 'com.aventstack:extentreports:5.1.1' to build.gradle.

  2. Create a reporter instance – In your test class or a base class, create an ExtentSparkReporter pointing to the output HTML file:
    ExtentSparkReporter spark = new ExtentSparkReporter("test-output/ExtentReport.html");
  3. Attach the reporter to ExtentReports – Create an ExtentReports object and attach the reporter:
    ExtentReports extent = new ExtentReports();
    extent.attachReporter(spark);
  4. Add system info (optional) – You can add environment details like OS, browser, etc.:
    extent.setSystemInfo("OS", System.getProperty("os.name"));
    extent.setSystemInfo("Browser", "Chrome");
  5. Create a test in your test method – Use extent.createTest() to create a test entry:
    ExtentTest test = extent.createTest("Verify Login");
  6. Log steps and status – Inside your test, log actions and assertions:
    test.log(Status.INFO, "Opening browser");
    test.log(Status.PASS, "Login successful");
    test.log(Status.FAIL, "Element not found");
  7. Attach screenshots on failure (optional) – Capture a screenshot using Selenium and attach it:
    String base64 = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BASE64);
    test.addScreenCaptureFromBase64String(base64);
  8. Flush the report after tests – In an @AfterSuite or @AfterClass method, call:
    extent.flush();
  9. View the report – Open the generated HTML file in a browser. It contains a dashboard with test counts, charts, and detailed logs.

Common Mistakes

  • Forgetting to call flush() – Without it, the report is not written to disk.
  • Creating a new ExtentReports object for each test – This overwrites previous tests. Use a single instance (e.g., in a base class).
  • Not using unique test names – Duplicate names cause tests to merge in the report.
  • Mixing JUnit and TestNG annotations – Ensure your @AfterSuite matches the framework you use.
  • Using the wrong dependency version – Older versions have different APIs (e.g., ExtentHtmlReporter vs ExtentSparkReporter). Stick to v5+.

Tips

  • Use a base class to initialize ExtentReports once and share it across test classes.
  • Log every meaningful step – it makes debugging easier.
  • Use test.assignCategory() to group tests by module or feature.
  • Set the report name and document title via spark.config().setReportName() and setDocumentTitle().
  • For parallel tests, use ThreadLocal<ExtentTest> to avoid race conditions.

Troubleshooting

  • Report file not generated – Check that flush() is called and the output directory exists. If using Maven, ensure the target folder is writable.
  • Tests appear but no logs – Make sure you call test.log() after creating the test, and that you are using the same ExtentTest instance.
  • Report shows blank or broken page – This usually happens if the HTML file is opened before flush() completes, or if the file path contains special characters. Wait for the build to finish and try a simple path.
  • ExtentReports class not found – Dependency not downloaded. Run mvn clean install or refresh Gradle. Check your internet connection and repository settings.
  • Screenshots not attaching – Ensure the driver is a TakesScreenshot instance (e.g., ChromeDriver). For remote WebDriver, you may need to use getScreenshotAs differently.

Frequently Asked Questions

Can I use Extent Reports with JUnit instead of TestNG?

Yes. Use JUnit’s @AfterClass or @AfterAll to call flush(). The rest is the same.

How do I generate a report for each test class separately?

Create a new ExtentSparkReporter with a unique file name per class, and attach it to the same ExtentReports instance, or create a new ExtentReports per class.

What is the difference between ExtentHtmlReporter and ExtentSparkReporter?

ExtentHtmlReporter is from older versions (v3 and below). ExtentSparkReporter is the replacement in v5, with a modern UI and better performance.

Can I include logs from log4j or SLF4J in the report?

Yes, you can manually add those logs using test.log(), but there is no direct integration. You can capture log output and pass it as a string.

How do I attach a screenshot on failure automatically?

Use a TestNG listener (e.g., ITestListener) and in onTestFailure, get the driver from a base class, capture the screenshot, and attach it to the corresponding ExtentTest.

Is Extent Reports free to use?

Yes, the open-source version is free under the Apache 2.0 license. There is also a paid version with additional analytics features.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.