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 + Javaimport 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
}
}