0
0
Selenium Javatesting~15 mins

Extent Reports setup in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Setup Extent Reports for Selenium Java Test
Preconditions (3)
Step 1: Create a new ExtentReports instance in the test setup method
Step 2: Attach an ExtentHtmlReporter with a specified report file path
Step 3: Create an ExtentTest object for the test case
Step 4: Log test steps and statuses using the ExtentTest object
Step 5: Flush the ExtentReports instance after test execution to write the report
✅ Expected Result: An HTML report file is generated after test execution showing the logged test steps and their statuses
Automation Requirements - Selenium with TestNG
Assertions Needed:
Verify that the ExtentReports instance is not null after initialization
Verify that the report file is created after flushing the report
Best Practices:
Use @BeforeTest and @AfterTest annotations for setup and teardown
Use explicit waits for Selenium interactions
Use Page Object Model for element locators (if applicable)
Use try-catch blocks to log failures in ExtentReports
Flush the report in @AfterTest to ensure report writing
Automated Solution
Selenium Java
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.

Common Mistakes - 4 Pitfalls
Not calling extent.flush() after tests
Creating ExtentReports instance inside each test method
Not logging test steps or statuses to ExtentTest
Hardcoding absolute file paths for the report
Bonus Challenge

Now add data-driven testing with 3 different URLs and verify their page titles using Extent Reports

Show Hint