How to Generate Extent Report Version 4 in TestNG Framework

Selenium provides inbuilt reports using frameworks such as JUnit and TestNG.

Although the built-in reports provide information on the steps that are executed as part of the test case, they need more customization to be shared with all the major project stakeholders.

Extent Reports is a customizable HTML report developed by Anshoo Arora which can be integrated into Selenium WebDriver using JUnit and TestNG frameworks.

This post will give you a complete step-by-step guide on how to generate Extent Reports in Selenium WebDrive with example codes.

Migrating from Version 3

If you are migrating from version 3, please note that the core usage remains the same. See the list of breaking changes

API: ChartLocation

Affected type: ChartLocation
ChartLocation is no longer available, and can be removed from your setup code
Suggested fix: removal of ChartLocation

Affected type: EmailTemplate
EmailTemplate has moved from package com.aventstack.extentreports.reporter to com.aventstack.extentreports.reporter.configuration
Suggested fix: Re-import package imports to fix

Mentioned below are the sequence of steps to use Extent Reports 4 in Selenium Webdriver in TestNG

Step #1:
Extent Reports can be directly used in selenium WebDriver by importing the JAR file – extentreports-4.0.6.jar which can be downloaded here.


Here is the link:http://extentreports.com/community-downloads/v4/extentreports-4.0.1.zip
Once the ZIP file is downloaded, extract the contents of the ZIP file into a folder.

Step #2:
Add the jar files present in the ZIP file to the project build path using the option Build Path --> Configure Build Path.

Sample code for Extent Reports

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.annotations.*;
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;

public class NopCommerceTest {
public WebDriver driver;
public ExtentHtmlReporter htmlReporter;
public ExtentReports extent;
public ExtentTest test;

public void setExtent() {
// specify location of the report
htmlReporter = new ExtentHtmlReporter(System.getProperty("user.dir") + "/test-output/myReport.html");

htmlReporter.config().setDocumentTitle("Automation Report"); // Tile of report
htmlReporter.config().setReportName("Functional Testing"); // Name of the report

extent = new ExtentReports();

// Passing General information
extent.setSystemInfo("Host name", "localhost");
extent.setSystemInfo("Environemnt", "QA");
extent.setSystemInfo("user", "pavan");

public void endReport() {

public void setup() {
System.setProperty("webdriver.chrome.driver", "C://Drivers/chromedriver_win32/chromedriver.exe");
driver = new ChromeDriver();

public void noCommerceTitleTest() {
test = extent.createTest("noCommerceTitleTest");
String title = driver.getTitle();
Assert.assertEquals(title, "eCommerce demo store");

public void noCommerceLogoTest() {
test = extent.createTest("noCommerceLogoTest");
boolean b = driver.findElement(By.xpath("//img[@alt='nopCommerce demo store']")).isDisplayed();

public void noCommerceLoginTest() {
test = extent.createTest("noCommerceLoginTest");

test.createNode("Login with Valid input");

test.createNode("Login with In-valid input");

public void tearDown(ITestResult result) throws IOException {
if (result.getStatus() == ITestResult.FAILURE) {
test.log(Status.FAIL, "TEST CASE FAILED IS " + result.getName()); // to add name in extent report
test.log(Status.FAIL, "TEST CASE FAILED IS " + result.getThrowable()); // to add error/exception in extent report
String screenshotPath = NopCommerceTest.getScreenshot(driver, result.getName());
test.addScreenCaptureFromPath(screenshotPath);// adding screen shot
} else if (result.getStatus() == ITestResult.SKIP) {
test.log(Status.SKIP, "Test Case SKIPPED IS " + result.getName());
else if (result.getStatus() == ITestResult.SUCCESS) {
test.log(Status.PASS, "Test Case PASSED IS " + result.getName());

public static String getScreenshot(WebDriver driver, String screenshotName) throws IOException {
String dateName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
TakesScreenshot ts = (TakesScreenshot) driver;
File source = ts.getScreenshotAs(OutputType.FILE);

// after execution, you could see a folder "FailedTestsScreenshots" under src folder
String destination = System.getProperty("user.dir") + "/Screenshots/" + screenshotName + dateName + ".png";
File finalDestination = new File(destination);
FileUtils.copyFile(source, finalDestination);
return destination;


Post a Comment