Extent Reports are the most popular reporting used with Selenium. As we all know, TestNG generates html reports by default but they are not more readable and interactive, we have to put lots of efforts to make it attractive. TestNG has provided an ability to implement 'IReporter' an interface to customize testng report by users.
ExtentReport API makes our life easy to generate interactive report with simple configuartions. It supports almost all Java and .NET test frameworks such as TestNG, JUnit, NUnit etc.
Extent Report Maven Dependency :
<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports</artifactId>
<version>3.1.5</version>
</dependency>
Let's understand with the help of basic example how extent reports work with selenium webdriver And later will look at advanced example on extent report
Step 1 :- Create maven project, create a class 'BasicExtentReport.java' and add the below code : -
package com.extentreport;
import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.SkipException;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.markuputils.ExtentColor;
import com.aventstack.extentreports.markuputils.MarkupHelper;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.ChartLocation;
import com.aventstack.extentreports.reporter.configuration.Theme;
public class BasicExtentReport {
//builds a new report using the html template
ExtentHtmlReporter htmlReporter;
ExtentReports extent;
//helps to generate the logs in test report.
ExtentTest test;
@Parameters({ "OS", "browser" })
@BeforeTest
public void startReport(String OS, String browser) {
// initialize the HtmlReporter
htmlReporter = new ExtentHtmlReporter(System.getProperty("user.dir") +"/test-output/testReport.html");
//initialize ExtentReports and attach the HtmlReporter
extent = new ExtentReports();
extent.attachReporter(htmlReporter);
//To add system or environment info by using the setSystemInfo method.
extent.setSystemInfo("OS", OS);
extent.setSystemInfo("Browser", browser);
//configuration items to change the look and feel
//add content, manage tests etc
htmlReporter.config().setChartVisibilityOnOpen(true);
htmlReporter.config().setDocumentTitle("Extent Report Demo");
htmlReporter.config().setReportName("Test Report");
htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
htmlReporter.config().setTheme(Theme.STANDARD);
htmlReporter.config().setTimeStampFormat("EEEE, MMMM dd, yyyy, hh:mm a '('zzz')'");
}
@Test
public void testCase1() {
test = extent.createTest("Test Case 1", "PASSED test case");
Assert.assertTrue(true);
}
@Test
public void testCase2() {
test = extent.createTest("Test Case 2", "PASSED test case");
Assert.assertTrue(true);
}
@Test
public void testCase3() {
test = extent.createTest("Test Case 3", "PASSED test case");
Assert.assertTrue(true);
}
@Test
public void testCase4() {
test = extent.createTest("Test Case 4", "PASSED test case");
Assert.assertTrue(false);
}
@Test
public void testCase5() {
test = extent.createTest("Test Case 5", "SKIPPED test case");
throw new SkipException("Skipping this test with exception");
}
@Test(enabled=false)
public void testCase6(){
test = extent.createTest("Test Case 6", "I'm Not Ready, please don't execute me");
}
@AfterMethod
public void getResult(ITestResult result) {
if(result.getStatus() == ITestResult.FAILURE) {
test.log(Status.FAIL, MarkupHelper.createLabel(result.getName()+" FAILED ", ExtentColor.RED));
test.fail(result.getThrowable());
}
else if(result.getStatus() == ITestResult.SUCCESS) {
test.log(Status.PASS, MarkupHelper.createLabel(result.getName()+" PASSED ", ExtentColor.GREEN));
}
else {
test.log(Status.SKIP, MarkupHelper.createLabel(result.getName()+" SKIPPED ", ExtentColor.ORANGE));
test.skip(result.getThrowable());
}
}
@AfterTest
public void tearDown() {
//to write or update test information to reporter
extent.flush();
}
}
'ExtentHtmlReporter' creates a rich standalone HTML file. It accepts ExtentHtmlReporter(java.io.File file) and ExtentHtmlReporter(java.lang.String filePath)
. In the above code, we are passing file path where the reports will be saved.
'ExtentHtmlReporter' also allows several configuration options via the config() method. In the above code, we have added few configuration items to change the look and feel of the report like report name, time stamp etc.
In 'AfterMethod', we are getting the status of each test executed and updating the status with Fail, Pass and Skip. If the test is failed, we are getting message string that gives more information about the errorresult.getThrowable()
MarkupHelper - MarkupHelper class is used to create labels like 'PASSED' / 'FAILED' and also to give colors to the different status in the test report using ExtentColor enum.
Step 2 :- Create testng.xml file like below to pass OS and Browser parameters : -
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Main Test Suite" verbose="5">
<parameter name="OS" value="Windows"/>
<parameter name="browser" value="Chrome"/>
<test name="Extent Report Test">
<classes>
<class name="com.extentreport.BasicExtentReport"/>
</classes>
</test>
</suite>
Step 3 :- Add below code to your pom.xml file
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.vsysq</groupId>
<artifactId>extentreport</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!--Test reporting library - extent 3.X-->
<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports</artifactId>
<version>3.1.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.12.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Once we run the program, you should the report like below : -
Below is the output image :-
Below is the Extent Report HTML looks like : -
If you want to view the failure reason, you just need to click on failed test case which will show the status of the test and the reason of the failure (exception details). Below is the failed test case view.
In this example, we have just seen how the basic extent reports work and how the reports look like. We will discuss more details on Extent report in next tutorials using test listeners and selenium webdriver
- ‹ WebDriver tests in PyTest using Fixtures and conftest.py
- Take Screenshot using FileHandler class in Selenium ›
Comments
facing issue in Extent report
Hi Sir,
Am facing below error while excuting the above code. Please help me out for this.
[RemoteTestNG] detected TestNG version 6.10.0
org.testng.TestNGException: java.net.SocketTimeoutException: Read timed out
at org.testng.TestNG.initializeSuitesAndJarFile(TestNG.java:325)
at org.testng.remote.AbstractRemoteTestNG.initialize(AbstractRemoteTestNG.java:137)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:98)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
Add new comment