πŸ’‘ If you like this website, please share it with your friends and network! πŸš€
Back to All Scenarios
Scenario 57 of 100
Framework Design
Intermediate

Generating Professional Extent HTML Reports

πŸ“ŠScenario Overview

Generating Professional Extent HTML Reports

Key Takeaways & Cheat Sheet

  • βœ“Utilize ExtentReports and ExtentSparkReporter libraries
  • βœ“Initialize the report engine inside test suite startup hooks (@BeforeSuite)
  • βœ“Create individual ExtentTest logs inside test setup execution hooks
  • βœ“Embed failure screenshots directly inside the report logs

Short Direct Answer

Professional reporting uses engines like ExtentReports or Allure. Set up the `ExtentReports` and `ExtentSparkReporter` objects inside `@BeforeSuite`, create test entries on `@BeforeMethod`, and write failure statuses with embedded screenshots directly into the report inside a custom TestNG listener or `@AfterMethod` hook.

⚠️ Senior Warning (Red Flag)

Avoid generating local static reports that do not support embedded screenshots or lack structural categorization. Without visual evidence of failure states, troubleshooting in CI/CD pipeline runs is nearly impossible.

πŸ’‘ STAR Deep Dive Explanation & Pro Tip

Always ensure to invoke the flush() command at the end of every test execution lifecycle. If flush() is omitted, the physical HTML report file will never be compiled and written to disk.

SeleniumAutomation.java
Selenium 4 + Java
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.reporter.ExtentSparkReporter;

public class BaseTest {
    protected static ExtentReports extent;
    protected ExtentTest test;

    @BeforeSuite
    public void setupReport() {
        ExtentSparkReporter spark = new ExtentSparkReporter("target/ExtentReport.html");
        extent = new ExtentReports();
        extent.attachReporter(spark);
    }

    @BeforeMethod
    public void startTest(Method method) {
        test = extent.createTest(method.getName());
    }

    @AfterMethod
    public void tearDown(ITestResult result) {
        if (result.getStatus() == ITestResult.FAILURE) {
            test.fail("Test Failed: " + result.getThrowable().getMessage());
            // String screenshotPath = captureScreenshot(driver, result.getName());
            // test.addScreenCaptureFromPath(screenshotPath);
        } else if (result.getStatus() == ITestResult.SUCCESS) {
            test.pass("Test Passed Successfully");
        }
        extent.flush(); // Generates the report file
    }
}