How to Generate Extent Reports in Selenium

Steve WorthamSteve Wortham
4 min read

Reporting is an important step of test automation as it helps stakeholders understand the outcome of the test case, the status of the application’s health and potential issues.

What are extent reports?

Extent Reports is an open-source reporting library used with test automation tools. It can be integrated with testing frameworks like JUnit and TestNG. Extend Reports generates HTML documents that serve as interactive and detailed test execution reports. It provides reports that can depict results as pie charts along with features such as logging and screenshots. Moreover, TestNG provides a report by default, but they are generally less detailed. Additionally, while TestNG provides default reports, they lack the level of detail and interactivity that Extent Reports offer, such as graphical representations, logs, and screenshots.

How to use extent reports in selenium?

Extent Reports has two major components or classes namely:

  1. ExtentReports class

  2. ExtentTest class

Syntax

ExtentReports extentreports = new ExtentReports("Path of directory to store the  HTML report file", true/false);

ExtentTest test = reports.startTest("TestName");

ExtentReports class generates the HTML reports and accepts two arguments.

First argument is the path where the report has to be generated and the second argument is whether the file has to be overwritten or a new file should be created.

True will be the default value to overwrite all the existing data.

The ExtentTest class logs each individual test step to the HTML report, allowing you to track the progress and results of the test case

The ExtentReports class and ExtentTest class can be used with the below built-in methods:

  1. startTest: To execute preconditions of a test case

  2. endTest: To execute postconditions of a test case

  3. Log: To log the status of each step

  4. Flush: Flush: This method clears any previous data from the memory and writes the final report to the designated output file.

How to generate reports?

  1. Add the extentreports-java-2.41.2.jar to the project buildpath.

  2. Create a new java class and add the below code:

package com.testgrid.practice;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;
public class ExtentDemo {


static ExtentTest test;
static ExtentReports report;
@BeforeClass
public static void startTest()
{
report = new ExtentReports(System.getProperty("user.dir")+"/ ExtentReportResults.html");
test = report.startTest("ExtentDemo");
}
@Test
public void extentReportsDemo()
{
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://testgrid.io/");
if(driver.getTitle().equals("TestGrid"))
{
test.log(LogStatus.PASS, "Test Passed");
}
else
{
test.log(LogStatus.FAIL, "Test Failed");
}
}
@AfterClass
public static void endTest()
{
report.endTest(test);
report.flush();
}
}

Test execution begins with the startTest() method, annotated with @BeforeClass, which initializes the Extent Reports object. This method sets up the report by specifying the output file location and initializing the test case.

@Test methods opens the chrome browser and launches the URL and validates the page Title with the expected and logs the test case status as Pass or Fail. endTest() method executes postconditions of the test case

How to log test steps and results?

Test Status can be PASS, FAIL, SKIP and INFO.

report.endTest(test);
test.log(LogStatus.PASS,"Test Passed");
test.log(LogStatus.FAIL,"Test Failed");
test.log(LogStatus.SKIP,"Test Skipped");
test.log(LogStatus.INFO,"Test Info");

The log() method accepts two arguments, first is test status and second is message to be displayed in the report.

How to Add Screenshots to Extent Report?

Screenshots can be captured in the report if a test fails.

// Taking a screenshot and adding to the report
File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
String path = "screenshot.png";
FileUtils.copyFile(src, new File(path));
test.log(LogStatus.FAIL, "Screenshot on failure: " + test.addScreenCapture(path));

Benefits of using extent reports

  1. It is open source.

  2. It can be customized as per the requirements.

  3. It provides pictorial and pie chart representations of test results

  4. It can be easily integrated with frameworks like Junit , TestNG and Nunit.

  5. It allows users to attach screenshots when the test fails.

  6. It logs the test report for a detailed summary of the tests.

Conclusion:

The significance of reporting in Selenium using ExtentReports is to provide comprehensive and customizable reports. Extent Reports help to communicate and track defects effectively with stakeholders. The final goal is to lead to better testing approaches and faster defect resolution.

Source: This article was originally published at https://testgrid.io/blog/extent-reports-in-selenium/.

0
Subscribe to my newsletter

Read articles from Steve Wortham directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Steve Wortham
Steve Wortham

Experienced Software QA Manager passionate about delivering top-quality software, writing intuitive content, and fostering connections with industry peers.