Generating Test Reports in Selenium Java Using ExtentReports
In test automation, reporting plays a vital role in tracking the success or failure of test cases, identifying bugs, and ensuring test transparency. When working with Selenium in Java, one of the most popular libraries for generating detailed and visually appealing test reports is ExtentReports. This blog will guide you through setting up and using ExtentReports in Selenium-based test automation projects.
What is ExtentReports?
ExtentReports is an open-source reporting library for test automation that allows you to create interactive, media-rich, and customizable HTML reports. It provides detailed logs, step-wise execution results, screenshots on failure, and categorization of test cases based on status, tags, or features.
It integrates seamlessly with TestNG, JUnit, and even Cucumber, making it a flexible choice for Java testers.
Key Features of ExtentReports
HTML report generation with screenshots
Integration with Selenium, TestNG, and JUnit
Categorization based on tags, authors, or test types
Logs with different status levels (PASS, FAIL, INFO)
Custom themes and layout options
How to Integrate ExtentReports with Selenium Java
1. Add ExtentReports Dependency
If you're using Maven, include the following dependency in your pom.xml:
xml
<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports</artifactId>
<version>5.0.9</version>
</dependency>
2. Create Base Test Report Configuration
Before running your tests, initialize ExtentReports in a base class:
java
import com.aventstack.extentreports.*;
import com.aventstack.extentreports.reporter.ExtentSparkReporter;
public class BaseTest {
public static ExtentReports extent;
public static ExtentTest test;
@BeforeSuite
public void setUpReport() {
ExtentSparkReporter spark = new ExtentSparkReporter("target/ExtentReport.html");
extent = new ExtentReports();
extent.attachReporter(spark);
}
@AfterSuite
public void tearDownReport() {
extent.flush(); // Write everything to the report
}
}
3. Log Test Status in Test Cases
You can now use ExtentTest to log test events in your Selenium test classes:
java
public class LoginTest extends BaseTest {
@Test
public void loginWithValidCredentials() {
test = extent.createTest("Login with Valid Credentials");
WebDriver driver = new ChromeDriver();
test.info("Launching browser");
driver.get("https://example.com/login");
test.info("Navigated to login page");
// Login actions
driver.findElement(By.id("username")).sendKeys("user");
test.pass("Entered username");
driver.findElement(By.id("password")).sendKeys("password");
test.pass("Entered password");
driver.findElement(By.id("loginBtn")).click();
test.pass("Clicked login button");
String expectedTitle = "Dashboard";
String actualTitle = driver.getTitle();
if (actualTitle.equals(expectedTitle)) {
test.pass("Login successful, title matched");
} else {
test.fail("Login failed, title mismatch");
}
driver.quit();
test.info("Browser closed");
}
}
4. Adding Screenshots to the Report
In case of test failure, you can capture and attach screenshots:
java
String screenshotPath = "screenshots/failure.png";
test.addScreenCaptureFromPath(screenshotPath);
Capture screenshots with standard Selenium TakesScreenshot logic and save them to your desired directory.
Conclusion
ExtentReports enhances the visibility and readability of your Selenium Java test executions. With just a few lines of code, you can create professional-quality HTML reports that include test steps, logs, and screenshots. This improves debugging, tracking, and overall test transparency. Whether you're working in a small QA team or managing enterprise-level test automation, integrating ExtentReports into your Selenium framework is a smart and efficient move.
Learn Selenium with Java Course in Hyderabad
Read More: Setting Up Selenium Grid Hub and Nodes for Java Tests
Read More: Cross-Browser Testing Using Selenium Grid with Java
Read More: Working with Cookies in Selenium Java Automation
Visit Quality Thought Training Institute Hyderabad
Get Direction
Comments
Post a Comment