0
0
Selenium Javatesting~15 mins

Extent report customization in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Customize Extent Report with Title, Theme, and System Info
Preconditions (2)
Step 1: Initialize ExtentReports and attach ExtentHtmlReporter
Step 2: Set the report document title to 'My Test Report'
Step 3: Set the report theme to DARK
Step 4: Add system information: OS as 'Windows 10' and Tester as 'QA Team'
Step 5: Run a sample test that passes
Step 6: Flush the report to generate the HTML file
✅ Expected Result: The generated Extent report HTML file should have the title 'My Test Report', use the DARK theme, and display system info with OS as 'Windows 10' and Tester as 'QA Team'. The test status should show as passed.
Automation Requirements - Selenium with TestNG and ExtentReports
Assertions Needed:
Verify the report file is created
Verify the report title is 'My Test Report'
Verify the report theme is DARK
Verify system info contains OS and Tester
Verify the test status is PASS
Best Practices:
Use ExtentHtmlReporter for HTML report customization
Set report properties before running tests
Flush the report after tests complete
Use TestNG annotations for setup and teardown
Use explicit waits in Selenium if needed
Automated Solution
Selenium Java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.Theme;

import java.io.File;

public class ExtentReportCustomizationTest {

    ExtentReports extent;
    ExtentTest test;
    WebDriver driver;
    String reportPath = System.getProperty("user.dir") + "/test-output/ExtentReport.html";

    @BeforeClass
    public void setup() {
        // Setup ExtentHtmlReporter
        ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(reportPath);
        htmlReporter.config().setDocumentTitle("My Test Report");
        htmlReporter.config().setTheme(Theme.DARK);

        // Initialize ExtentReports and attach reporter
        extent = new ExtentReports();
        extent.attachReporter(htmlReporter);

        // Add system info
        extent.setSystemInfo("OS", "Windows 10");
        extent.setSystemInfo("Tester", "QA Team");

        // Setup WebDriver (Assuming chromedriver is in PATH)
        driver = new ChromeDriver();
    }

    @Test
    public void sampleTest() {
        test = extent.createTest("Sample Passing Test");
        driver.get("https://www.example.com");

        String title = driver.getTitle();
        test.log(Status.INFO, "Page title is: " + title);

        // Simple assertion
        Assert.assertTrue(title.contains("Example Domain"));
        test.log(Status.PASS, "Title contains 'Example Domain'");
    }

    @AfterClass
    public void teardown() {
        if (driver != null) {
            driver.quit();
        }
        extent.flush();

        // Verify report file exists
        File reportFile = new File(reportPath);
        Assert.assertTrue(reportFile.exists(), "Extent report file should exist");
    }
}

This test class sets up an ExtentHtmlReporter with a custom document title and DARK theme in the @BeforeClass method. It also adds system information like OS and Tester name.

The sampleTest method opens a website, logs the page title, asserts the title contains expected text, and logs the test status as PASS.

In @AfterClass, the WebDriver quits and the ExtentReports flushes to write the report file. Then it asserts the report file was created successfully.

This structure ensures the report is customized and generated properly with the required details.

Common Mistakes - 5 Pitfalls
Not calling extent.flush() after tests
Setting report configurations after attaching reporter
{'mistake': 'Hardcoding file paths without using System properties', 'why_bad': 'Hardcoded paths may not work on different machines or environments.', 'correct_approach': 'Use System.getProperty("user.dir") to build relative paths for portability.'}
Not adding system info to ExtentReports
Not logging test steps or status in ExtentTest
Bonus Challenge

Now add data-driven testing with 3 different URLs and verify their titles in the Extent report

Show Hint