How to Generate Extent Reports in Selenium Webdriver

In this article, we see one of the most popular and widely used Selenium Reporting Tools (Extent Reports). Selenium Results can be seen using different Selenium Reporting tools. Some of the Selenium WebDriver Reporting tools are as follows.

  • Selenium TestNG Report Generation
  • Selenium JUnit Report Generation
  • Selenium Extent Reports

What are the Extent Reports?

ExtentReports is an open-source reporting library used in selenium test automation. Extent reports become the first choice of Selenium Automation Testers, even though Selenium comes with inbuilt reports using frameworks like JUnit and TestNG. With extent reports, you can offer a more extensive and insightful perspective on the execution of your automation scripts. So let’s see why Automation testers prefer Extent reports to others.

Advantages of Extent Reports:

Some of the advantages of Extent Reports are as follows

  • Extent reports are more customizable than others
  • Extent API can produce more interactive reports, a dashboard view, graphical view, capture screenshots at every test step, and emailable reports
  • It can be easily integrated with frameworks like JUnit, NUnit, & TestNG
  • It displays the time taken for test case execution

Generating Extent Reports In Selenium WebDriver

The main scope of this article is to show the Extent Reports. This is the most popular and widely used Selenium Reporting tool in the current market. Let’s see how to generate extent reports in Selenium WebDriver.

Extent Reports Version 4 – Latest Version [2020]

Pre-requisites to Generate Extent Reports:

  1. Java should be installed –  Install and setup Java
  2. TestNG should be installed – Install TestNG
  3. Extent Report Jars (Version 2.41.2) – Download
  4. extent-config.xml – It allows to configure HTML Report

Steps To Generate Extent Reports:

  1. Firstly, create a TestNG project in eclipse
  2. Now download extent library files from the following link: http://extentreports.relevantcodes.com/
  3. Add the downloaded library files to your project
  4. Create a java class say ‘ExtentReportsClass’ and add the following code to it

Selenium Continuous Integration With Jenkins [Maven – Git – Jenkins]

Check the below video to see “Extent Reports Complete Guide”

Please be patient. The video will load in some time.

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

Code Explanation:

i. Imported two classes ExtentReports and ExtentTest.

ExtentReportsBy using this class we set the path where our reports need to generate.

ExtentTestBy using this class we could generate the logs in the report.

ii. Took three methods with @Test annotation such as passTest, failTest and skipTest and a method startTest with @BeforeTest annotation and another method endReport with @AfterMethod annotation

Here my intention is to generate a report with all the three types of results such as Pass, Fail and Skip.

Add Screenshots of a failed Test Cases in Extent Reports

iii. Used object of ExtentReports class (i.e., extent) in the startReport method which was assigned to @BeforeTest annotation to generate the HTML report in the required path

iv. Used object of ExtentTest class (i.e., logger) in the remaining methods to write logs in the report.

v. Used ITestResult class in the @AfterMethod to describes the result of a test.

Given clear explanation in the comments section with in the program itself. Please go through it to understand the flow.

Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package softwareTestingMaterial;
 
import java.io.File;
 
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.Test;
 
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;
 
public class ExtentReportsClass{
ExtentReports extent;
ExtentTest logger;
@BeforeTest
public void startReport(){
//ExtentReports(String filePath,Boolean replaceExisting)
//filepath – path of the file, in .htm or .html format – path where your report needs to generate.
//replaceExisting – Setting to overwrite (TRUE) the existing file or append to it
//True (default): the file will be replaced with brand new markup, and all existing data will be lost. Use this option to create a brand new report
//False: existing data will remain, new tests will be appended to the existing report. If the the supplied path does not exist, a new file will be created.
extent = new ExtentReports (System.getProperty(“user.dir”) +“/test-output/STMExtentReport.html”, true);
//extent.addSystemInfo(“Environment”,”Environment Name”)
extent
                .addSystemInfo(“Host Name”, “SoftwareTestingMaterial”)
                .addSystemInfo(“Environment”, “Automation Testing”)
                .addSystemInfo(“User Name”, “Rajkumar SM”);
                //loading the external xml file (i.e., extent-config.xml) which was placed under the base directory
                //You could find the xml file below. Create xml file in your project and copy past the code mentioned below
                extent.loadConfig(new File(System.getProperty(“user.dir”)+“\\extent-config.xml”));
}
@Test
public void passTest(){
//extent.startTest(“TestCaseName”, “Description”)
//TestCaseName – Name of the test
//Description – Description of the test
//Starting test
logger = extent.startTest(“passTest”);
Assert.assertTrue(true);
//To generate the log when the test case is passed
logger.log(LogStatus.PASS, “Test Case Passed is passTest”);
}
@Test
public void failTest(){
logger = extent.startTest(“failTest”);
Assert.assertTrue(false);
logger.log(LogStatus.PASS, “Test Case (failTest) Status is passed”);
}
@Test
public void skipTest(){
logger = extent.startTest(“skipTest”);
throw new SkipException(“Skipping – This is not ready for testing “);
}
@AfterMethod
public void getResult(ITestResult result){
if(result.getStatus() == ITestResult.FAILURE){
logger.log(LogStatus.FAIL, “Test Case Failed is “+result.getName());
logger.log(LogStatus.FAIL, “Test Case Failed is “+result.getThrowable());
}else if(result.getStatus() == ITestResult.SKIP){
logger.log(LogStatus.SKIP, “Test Case Skipped is “+result.getName());
}
// ending test
//endTest(logger) : It ends the current test and prepares to create HTML report
extent.endTest(logger);
}
@AfterTest
public void endReport(){
// writing everything to document
//flush() – to write or update test information to your report.
                extent.flush();
                //Call close() at the very end of your session to clear all resources.
                //If any of your test ended abruptly causing any side-affects (not all logs sent to ExtentReports, information missing), this method will ensure that the test is still appended to the report with a warning message.
                //You should call close() only once, at the very end (in @AfterSuite for example) as it closes the underlying stream.
                //Once this method is called, calling any Extent method will throw an error.
                //close() – To close all the operation
                extent.close();
    }
}

extent-config.xml: 

By using this external XML file (extent-config.xml), we could change the details such as Report Theme (either standard or dark), Report Title, Document Title etc.,

We use extent object and use loadConfig() method to load this XML file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?xml version=“1.0” encoding=“UTF-8”?>
<extentreports>
  <configuration>
    <! report theme >
    <! standard, dark >
    <theme>standard</theme>
  
    <! document encoding >
    <! defaults to UTF8 >
    <encoding>UTF8</encoding>
    
    <! protocol for script and stylesheets >
    <! defaults to https >
    <protocol>https</protocol>
    
    <! title of the document >
    <documentTitle>ExtentReports 2.0</documentTitle>
    
    <! report name displayed at topnav >
    <reportName></reportName>
    
    <! report headline displayed at topnav, after reportHeadline >
    <reportHeadline>Automation Report</reportHeadline>
    
    <! global date format override >
    <! defaults to yyyyMMdd >
    <dateFormat>yyyyMMdd</dateFormat>
    
    <! global time format override >
    <! defaults to HH:mm:ss >
    <timeFormat>HH:mm:ss</timeFormat>
    
    <! custom javascript >
    <scripts>
      <![CDATA[
        $(document).ready(function() {
          
        });
      ]]>
    </scripts>
    
    <! custom styles >
    <styles>
      <![CDATA[
        
      ]]>
    </styles>
  </configuration>
</extentreports>

Console Output:

1
2
3
4
===============================================
Default suite
Total tests run: 3, Failures: 1, Skips: 1
===============================================

Refresh the project after execution of above ExtentReportsClass.java file. You could find an HTML file named “STMExtentReport.html” in your test-output folder. Copy the location of the STMExtentReport.html file and open it by using any browser. Once you open the report, you will see beautiful high rich HTML test results as shown below.

Test Summary Report:

Graphical Report with PIE Charts:

This way we could generate extent reports.

Extent reports is an open-source library that generates customizable HTML reports. So it becomes the best choice of Selenium Automation Testers.

Test Automation Framework Interview Questions

If you are not a regular reader of SoftwareTestingMaterial.com then I highly recommend you to signup for the free email newsletter using the below form.

Can we customize extent reports?

Yes, we can customize extent reports like change the title, change to dark theme, add screenshots, include logo and others

How to change title in the extent reports?

Open your file “extent-config.xml” and add the below code to it.

Modify the browser title between ‘documentTitle’ open and closed tags.

Modify the report title between ‘reportName’ open and closed tags.

How to change the theme to dark theme?

Open your XML file and add the below code under the configuration node.

Modify the theme as ‘dark’ between ‘theme’ open and closed tags.

How to resolve HTTPS protocol error in extent reports?

Sometimes HTTPS protocol of style sheets and script files are blocked due to network restrictions in organizations.

In this case, we can add the below code under the configuration node of your XML file to ensure all the scripts and style sheets follow the HTTP protocol

Modify the text as ‘http‘ between ‘protocol’ open and closed tags.