Test Overview
This test sets up Extent Reports in a Selenium Java test. It verifies that the report file is created and logs test steps correctly.
This test sets up Extent Reports in a Selenium Java test. It verifies that the report file is created and logs test steps correctly.
import com.aventstack.extentreports.ExtentReports; import com.aventstack.extentreports.ExtentTest; import com.aventstack.extentreports.reporter.ExtentSparkReporter; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.File; public class ExtentReportSetupTest { private static ExtentReports extent; private static ExtentTest test; private static final String reportPath = System.getProperty("user.dir") + "/test-output/ExtentReport.html"; @BeforeAll public static void setup() { ExtentSparkReporter spark = new ExtentSparkReporter(reportPath); extent = new ExtentReports(); extent.attachReporter(spark); test = extent.createTest("Extent Report Setup Test"); test.info("Extent Report initialized"); } @Test public void sampleTest() { test.info("Starting sample test"); assertTrue(true, "Sample assertion always true"); test.pass("Sample test passed"); } @Test public void reportFileExists() { extent.flush(); File reportFile = new File(reportPath); assertTrue(reportFile.exists(), "Extent report file should exist after flush"); test.info("Verified report file exists"); } @AfterAll public static void tearDown() { extent.flush(); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts - JUnit initializes test class and calls @BeforeAll setup method | ExtentSparkReporter created with path 'test-output/ExtentReport.html'. ExtentReports instance attached to reporter. | - | PASS |
| 2 | ExtentTest instance created and logs info 'Extent Report initialized' | Test named 'Extent Report Setup Test' ready to log steps. | - | PASS |
| 3 | JUnit runs sampleTest method | Test logs info 'Starting sample test'. | assertTrue(true) verifies condition is true | PASS |
| 4 | Test logs pass status 'Sample test passed' | Test step recorded in ExtentTest instance. | - | PASS |
| 5 | JUnit runs reportFileExists method | Checks if report file exists at 'test-output/ExtentReport.html' | assertTrue(reportFile.exists()) verifies report file presence | PASS |
| 6 | Test logs info 'Verified report file exists' | Log added to ExtentTest instance. | - | PASS |
| 7 | JUnit calls @AfterAll tearDown method | ExtentReports flush() writes all logs to report file. | - | PASS |