Extent Report in Selenium, TestNG, Java

How to generate automation HTML report using extent report in selenium web driver and TestNG. Let’s see an example for the extent report in selenium. In the below example we are going to use extent report 4 with TestNG 7. 

Extent report in Selenium, TestNG

Extent reports, it’s a third-party framework that is used to generate meaning full and color full test results. It’s really important. when you run your automation test suite the results should be well defined and readable for everyone.

Steps to Generate extent report in Selenium

  • Install TestNG into your IDE (Eclipse TestNG integration)
  • Find maven dependency for extent report.
  • Create extend report configuration file (for good coding practice).
  • Integrate extent report with each of the testcase(@BeforeTest Annotation) 
  • Run the test suite and view the results.

for the below example, pre-requirements are  Eclipse TestNG integration and maven dependency for extent report.

Maven dependency for extent report 4

copy and paste below maven dependency for extent report into your pom.xml

<dependency>
  <groupId>com.aventstack</groupId>
  <artifactId>extentreports</artifactId>
  <version>4.0.9</version>
</dependency>

Create extend report configuration class

ReportConfiguration.java is a custom class that contains a static method ExtendReportConfiguraiton() 

package com.Extentreport;

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;

public class ReportConfiguration {
 
 public static ExtentHtmlReporter htmlReporter;
 public static ExtentReports extent;
 public static ExtentTest logger;
 
 public  static void ExtendReportConfiguraiton() {
  
  htmlReporter = new ExtentHtmlReporter(System.getProperty("user.dir") + "/test-output/ExtentReport.html");
  extent = new ExtentReports();
  extent.attachReporter(htmlReporter);
  extent.setSystemInfo("Host Name", "Bhupi");
  extent.setSystemInfo("Environment", "Automation Testing");
  extent.setSystemInfo("User Name", "gfgdfgfgd  SM");
  htmlReporter.config().setDocumentTitle("Auatomaiton Results");
 }

}

new ExtentHtmlReporter(System.getProperty("user.dir") + "/test-output/ExtentReport.html")Will get the path of the project directory and create a folder “test-output” and an HTML file ExtentReport.html

new ExtentReports();Create an object of ExtentReports class.

attachReporter(htmlReporter);will attach your ExtentReport.html file with the ExtentReports object.

setSystemInfo() Will allow making more configuration in the HTML file.

Create TestCases using Selenium and TestNG

DriverContext.java is also a custom Java class that contains Initialize() method to define Browser drivers and base URL and also we have set the browser driver in the same.

Basically, This is a Page object format where we manage all the configuration and other resources in separate classes and objects.

package com.Extentreport;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class DriverContext {
      static WebDriver driver;
      public static String baseUrl = "https://www.codebun.com/";
   static String driverPath = "D:\\geckodriver.exe";
      
   public static WebDriver Initialize()
      {
         System.setProperty("webdriver.gecko.driver", driverPath);
         driver = new FirefoxDriver();
         driver.get(baseUrl);    
         return driver;
       
      }

}

FirstTestNG.java, Let’s create a test file and write some simple testcases in TestNG for the demo.

package com.Extentreport;

import org.openqa.selenium.*;
import org.testng.Assert;
import org.testng.annotations.*;

public class FirstTestNG extends ReportConfiguration {

 public WebDriver driver;

 public FirstTestNG() {
  
  driver = DriverContext.Initialize();
 }

 @BeforeTest
 public void startReport() {
  ReportConfiguration.ExtendReportConfiguraiton();
 }

 @Test
 public void T1() {
  
  Assert.assertTrue(true);
  
 }

 @Test
 public void T2() {

  System.out.println("TestCase 2");
 }
 
 @Test
 public void T3() {

  System.out.println("TestCase 3");
 }
}

Run the TestNG class

Right-click on the class –> Run As –> TestNG Test

Open Extent report in the browser

In your project directory open the “test-output” folder and run the file index.html on any real-time browser(Chrome or Firefox etc).

Output:

Extent report with multiple test classes and testcases

Yes, Most of the time will face issues to run the extent report with multiple classes and testcases. to achieve this we need to create a testng.xml that will run our test suite. (All Test Classes in a single run).

As we already have one test class FirstTestNG.java,  So I am going to continue the same example, and let’s create another test class SecondTestNG.java

SecondTestNG.java

package com.Extentreport;

import org.openqa.selenium.WebDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class SecondTestNG {
 
 public WebDriver driver;
 
  public SecondTestNG() {
  driver = DriverContext.Initialize();
   }

 
  @Test
   public void Test1() {
          
         System.out.println("Test casesssssssss");
         driver.close();
   }
  

  @Test
   public void Test3() {
   Assert.assertTrue(true);
   }
  
  @Test
   public void Test4() {
   Assert.assertTrue(true);
   }
}

Create testng.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">	

<suite thread-count="1" verbose="1" name="Auto Suite" annotations="JDK" parallel="tests">
         
  <test name="flipkart">
  <classes>
    <class name="com.Extentreport.FirstTestNG"/>
    <class name="com.Extentreport.SecondTestNG"/>
  </classes>
   </test>

</suite>

The remaining configuration will be the same but to run the automation suite, we have to run the testng.xml

Right-click on the testng.xml–> Run As –> TestNG Test

Selenium Practice Task

https://codebun.com/how-to-handle-html-elements-in-selenium-web-driver/

https://codebun.com/how-to-handle-drop-down-in-selenium-web-driver/

https://codebun.com/browser-and-navigation-commands-in-selenium-web-driver/

https://codebun.com/how-to-find-element-in-selenium-web-driver/

https://codebun.com/what-is-web-driver-and-web-element-in-selenium/