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.