Tuesday, September 11, 2018

How to use Extent Reporter in Selenium

Hello Techienthu! This post is all about, how to use extent reporter in Selenium


Extent reporter is one of the advanced reporting framework developed using Java. It is widely used in number of Java based Selenium projects. In this post, I have used Extent Reporter 2.40 version.

Benefits of using Extent Report in Selenium


  • Easy to maintain
  • Gives user friendly GUI
  • Provides charts of test pass percentage
  • Can include screenshots
  • Provides different log statuses like PASS, FAIL, ERROR, UNKNOWN, INFO, WARNING, FATAL
  • User can able to customize the final report
  • Also works with testng listeners
  • Can publish in Jenkins

API's required to integrate Extent Reports in Selenium test


  • Extent Reports V-2.40.2 Download || Contains all the required classes and methods of extent reports API
  • Freemaker V-2.3.23 Download || Used to generate graphs in extent report
  • Jsoup V-1.8.2 Download || Used to parse the data into extent report

Below is a sample code to generate a report with two suites, one testcase under each suite and all available log statuses in extent reports


import java.io.File;

import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;

public class ExtentDemo {
 static ExtentReports extent = new ExtentReports("d://report.html",true);
 static ExtentTest suiteName = null;
 static ExtentTest testName = null;
 public static void main(String[] args) {
  extent.loadConfig(new File("extent-config.xml"));
  suiteName = extent.startTest("SuiteName1");
  testName = extent.startTest("Testcase name1");
  suiteName.appendChild(testName);
  testName.log(LogStatus.PASS, "The first step with status PASS");
  testName.log(LogStatus.UNKNOWN, "Third Step with status as UNKNOWN");
  testName.log(LogStatus.INFO, "Fourth step with status INFO");
  testName.log(LogStatus.SKIP, "Fifth step with th status SKIP");
  testName.log(LogStatus.WARNING, "Sixth step with the status as WARNING");
  testName.log(LogStatus.SKIP, "Seventh step with the status as SKIP");
  testName.log(LogStatus.ERROR, "Eighth step with the status as ERROR");
  testName.log(LogStatus.FATAL, "Second step with status as FATAL");
  extent.endTest(suiteName);
  extent.flush();

  suiteName = extent.startTest("SuiteName2");
  testName = extent.startTest("Testcase name2");
  suiteName.appendChild(testName);
  testName.log(LogStatus.PASS, "The first step with status PASS");
  extent.endTest(suiteName);
  extent.flush();
  extent.close();

 }

}

Explanation for the above code

ExtentReports extent = new ExtentReports("d://report.html",true);
It creates an object for ExtentReports class. as per the above script, constructor takes two arguments
  • filepath along with filename of type String || Location where the report need to generate
  • boolean status || To override the existing report
extent.loadConfig(new File("extent-config.xml"));
It loads the extent-config.xml file during runtime. It accepts one argument.
  • Path of the extent-config.xml file.

Sample extent-config.xml


<?xml version="1.0" encoding="UTF-8"?>
<extentreports>
 <configuration>
  <!-- report theme -->
  <!-- standard, dark -->
  <theme>dark</theme>

  <!-- document encoding -->
  <!-- defaults to UTF-8 -->
  <encoding>UTF-8</encoding>

  <!-- protocol for script and stylesheets -->
  <!-- defaults to https -->
  <protocol>https</protocol>

  <!-- title of the document -->
  <documentTitle>Title here</documentTitle>

  <!-- report name - displayed at top-nav -->
  <reportName>
  </reportName>

  <!-- report headline - displayed at top-nav, after reportHeadline -->
  <reportHeadline>Version
   R20A
    <![CDATA[
    <img src="RL.PNG" />
    ]]>
  </reportHeadline>

  <!-- global date format override -->
  <!-- defaults to yyyy-MM-dd -->
  <dateFormat>yyyy-MM-dd</dateFormat>

  <!-- global time format override -->
  <!-- defaults to HH:mm:ss -->
  <timeFormat>HH:mm:ss</timeFormat>

  <!-- custom javascript -->
  <scripts>
      <![CDATA[$(document).ready(function(){$(".logo-content").hide();$(".logo-container").html("Company name here");});

      ]]>
  </scripts>

  <!-- custom styles -->
  <styles>
      <![CDATA[
        
      ]]>
  </styles>
 </configuration>
</extentreports>
ExtentTest suiteName = extent.startTest("SuiteName");
It creates an object for Suite in report file and it accepts one argument
  • Suite name as an argument || The given value will be considered as suite name
ExtentTest testName = extent.startTest("Testcase name");
It creates an object for Testcase in report file and it accepts one argument
  • Testcase name as an argument || The given value will be considered as testcase name under a suite
suiteName.appendChild(testName);
It appends the given test case under a suite in report file. It accepts one argument.
  • Testcase object || The given test case object will be appended under the suite

  testName.log(LogStatus.PASS, "The first step with status PASS");
  testName.log(LogStatus.FATAL, "Second step with status as FATAL");
  testName.log(LogStatus.UNKNOWN, "Third Step with status as UNKNOWN");
  testName.log(LogStatus.INFO, "Fourth step with status INFO");
  testName.log(LogStatus.SKIP, "Fifth step with th status SKIP");
  testName.log(LogStatus.WARNING, "Sixth step with the status as WARNING");
  testName.log(LogStatus.SKIP, "Seventh step with the status as SKIP");
  testName.log(LogStatus.ERROR, "Eighth step with the status as ERROR");
Extent Reports gives 8 types of statues in report file. It accepts two arguments.
  • Log status || Based on the status given, steps in report will get changed
  • Message || Log step
extent.flush();
extent.close();
Finally, saves the report and close the stream of report file after completion of the test execution.

Sample Extent Report which is generated by above code