Extent Reports setup in Selenium Java - Build an Automation Script
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Assert; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; import com.aventstack.extentreports.ExtentReports; import com.aventstack.extentreports.ExtentTest; import com.aventstack.extentreports.reporter.ExtentHtmlReporter; import java.io.File; public class ExtentReportsSetupTest { WebDriver driver; ExtentReports extent; ExtentTest test; @BeforeTest public void setUp() { // Setup ChromeDriver (assumes chromedriver is in system PATH) driver = new ChromeDriver(); // Initialize ExtentReports and attach reporter ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter("extentReport.html"); extent = new ExtentReports(); extent.attachReporter(htmlReporter); // Verify ExtentReports instance is created Assert.assertNotNull(extent, "ExtentReports instance should not be null"); } @Test public void sampleTest() { test = extent.createTest("Sample Test", "Verify Google homepage title"); try { driver.get("https://www.google.com"); test.info("Navigated to Google homepage"); String title = driver.getTitle(); test.info("Page title is: " + title); Assert.assertEquals(title, "Google"); test.pass("Title matched expected value"); } catch (Exception e) { test.fail("Test failed with exception: " + e.getMessage()); Assert.fail(e.getMessage()); } } @AfterTest public void tearDown() { if (driver != null) { driver.quit(); } if (extent != null) { extent.flush(); // Verify report file is created File reportFile = new File("extentReport.html"); Assert.assertTrue(reportFile.exists(), "Extent report file should exist after flush"); } } }
This test class sets up Extent Reports for Selenium tests using TestNG.
@BeforeTest: Initializes the ChromeDriver and ExtentReports with an HTML reporter. It asserts that the ExtentReports instance is not null to confirm setup.
@Test: Creates an ExtentTest to log steps. It navigates to Google, logs info messages, asserts the page title, and logs pass or fail accordingly.
@AfterTest: Quits the browser and flushes the ExtentReports to write the report file. It asserts that the report file exists to confirm report generation.
This structure ensures the report captures test steps and results clearly, and the assertions verify the setup and output.
Now add data-driven testing with 3 different URLs and verify their page titles using Extent Reports
Practice
ExtentHtmlReporter in Selenium Java testing?Solution
Step 1: Understand the role of ExtentHtmlReporter
ExtentHtmlReporter is used to create a visual HTML report that shows test results clearly.Step 2: Differentiate from other components
It does not run tests or interact with browsers; it only formats and outputs reports.Final Answer:
To generate an HTML report for test execution results -> Option CQuick Check:
ExtentHtmlReporter = HTML report generator [OK]
- Confusing ExtentHtmlReporter with WebDriver
- Thinking it runs tests
- Assuming it manages browser actions
Solution
Step 1: Create ExtentHtmlReporter with file path
You must instantiate ExtentHtmlReporter with the report file name or path.Step 2: Attach reporter to ExtentReports instance
Create ExtentReports object and attach the reporter using attachReporter method.Final Answer:
ExtentHtmlReporter reporter = new ExtentHtmlReporter("report.html"); ExtentReports extent = new ExtentReports(); extent.attachReporter(reporter); -> Option BQuick Check:
Initialize reporter with path, then attach to ExtentReports [OK]
- Not passing file path to ExtentHtmlReporter constructor
- Calling attachReporter before creating ExtentReports
- Using non-existent methods like createReporter
ExtentHtmlReporter reporter = new ExtentHtmlReporter("testReport.html");
ExtentReports extent = new ExtentReports();
extent.attachReporter(reporter);
ExtentTest test = extent.createTest("LoginTest");
test.pass("Login successful");
extent.flush();Solution
Step 1: Analyze test creation and logging
The test 'LoginTest' is created and logged as passed with message 'Login successful'.Step 2: Understand flush() role
Calling extent.flush() writes the report to the file including all logged tests.Final Answer:
The report will show a test named 'LoginTest' marked as passed with message 'Login successful'. -> Option DQuick Check:
createTest + pass + flush = Passed test in report [OK]
- Thinking flush() clears tests
- Assuming test is failed without fail() call
- Believing report is empty without flush()
ExtentHtmlReporter reporter = new ExtentHtmlReporter();
ExtentReports extent = new ExtentReports();
extent.attachReporter(reporter);
ExtentTest test = extent.createTest("SearchTest");
test.pass("Search executed");
extent.flush();Solution
Step 1: Check ExtentHtmlReporter initialization
ExtentHtmlReporter requires a file path string in its constructor to specify report output location.Step 2: Verify other method usages
attachReporter, createTest, and flush are used correctly; no multiple reporter restriction or parameter issues.Final Answer:
ExtentHtmlReporter must be initialized with a file path in its constructor. -> Option AQuick Check:
Reporter needs file path on creation [OK]
- Omitting file path in ExtentHtmlReporter constructor
- Misunderstanding createTest parameters
- Calling flush too early
Solution
Step 1: Initialize ExtentHtmlReporter with file path
Instantiate ExtentHtmlReporter with the path to the report file, create an ExtentReports instance, and attach the reporter.Step 2: Create multiple tests and log statuses
Create ExtentTest instances with createTest(), log one with pass("message") and one with fail("custom error message"), then flush().Final Answer:
ExtentHtmlReporter reporter = new ExtentHtmlReporter("multiTestReport.html"); ExtentReports extent = new ExtentReports(); extent.attachReporter(reporter); ExtentTest test1 = extent.createTest("Test1"); test1.pass("Test1 passed"); ExtentTest test2 = extent.createTest("Test2"); test2.fail("Test2 failed due to timeout"); extent.flush(); -> Option AQuick Check:
Reporter with path + attach + pass/fail + flush [OK]
- Not passing file path to ExtentHtmlReporter
- Swapping pass and fail calls
- Calling flush before logging tests
Ready to prove it?
Pass the test and this pattern moves unknown
