Extent Reports: 5 Lines of code to set up reporting in Selenium


Top post on IndiBlogger, the biggest community of Indian Bloggers

Test reporting is one the important activity in quality assurance. It doesn’t matter whether you are a manual tester or automation tester, you must implement logging as well as reporting activities in your Automation framework. Extent reports give the seamless environment to implement test report generation in the Selenium project.

Extent reports efficiently support HTML reporting. It gives a broader view of the end results. Let’s discuss some of the advantages of using Extent Reports for test reporting in Selenium WebDriver.

Don’t Miss: How to set up Jenkins connection in Maven project for Selenium?

Advantages of reporting through Extent Reports:

  • It shows coverage in percentage
  • Pass – Failed report comes in pie chart format
  • A tabular format to display Pass, Fail and Skipped test step
  • Test environment details appear automatically in the Test report
  • Total tests and Total steps appear in the report
  • Total time taken for the current run as well as total time taken for the overall run appears in the report along with Start and end time count
  • Tests are configured and customized as per the choice of the test engineer/developer
  • Easy to implement this reporting in the Automation framework

Detailed Extent Reports

How to set up Extent Reports in IDE?

Firstly you need to add the library files of the Extent Reports. You can do it by uploading the external JARs to your project. Click here to download the JAR file.

If you are using Maven then add its dependencies to the pom.xml file.

<dependency>
    <groupId>com.relevantcodes</groupId>
    <artifactId>extentreports</artifactId>
    <version>2.40.2</version>
</dependency>

How to implement Extent Reports in Selenium Automation framework?

As I claim above (in the title) that you only need 5 lines of code to implement the Extent reports, which is true as Sun rises in the east.

Here are the codes which you need to implement:

Starts with-

ExtentReports report = new ExtentReports(reportPath+".html", false);
ExtentTest test = report.startTest("Extent Report Example Project");

Ends with-

report.endTest(test);
 report.flush();
report.close();

As you see above, you just need these five lines of code to implement Extent reports in your test automation framework. Let me explain the above code.

Step# 1: We create the instance of ExtentReports class and we pass arguments as path and file name with extension .html. As you see above, I have given the link to the path followed by file name as the current class name with .html as the extension.

Step# 2: Once the instance of ExtentReports created and path passed as the argument then we need to create the instance of ExtentTest. It holds the method which initiates and end the tests Along with the Log status as PASS, FAIL, SKIP, ERROR, FAIL, FATAL and WARNING.

Step# 3:  You need to implement the log status with the help of the instance of ExtentTest. You can implement it wherever you are asserting any condition as pass or fail. We will see it further in this tutorial.

Step# 4: At the end, you need to end the test along with flushing and closing of the report. It will help you to write the reporting to the designated file name.

Let’s discuss the implementation of Extent Reports in the execution of the test.

Scenario:  We are taking a scenario in which the driver will open the URL and it will capture the title at the home page. Now our task is to assert the title through the if-else statement. If the title contains the expected keyword, then Extent Reports should print this as pass. Similarly, if the title does not contain the keyword then Extent reports should print fail in the report generated.

Let’s have a look at the sample code:

Sample Code:

package com.inviul.selenium.project;

import org.testng.annotations.Test;

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

import org.testng.annotations.BeforeMethod;

import java.io.File;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;

public class ExtentReportExample {
	WebDriver driver = null;
	public ExtentReports report;
	public ExtentTest test;
	String reportPath = new File("").getAbsolutePath().toString().trim()+"/Reports/";
	
	@BeforeMethod
	  public void beforeMethod() {
		 System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");
		  driver = new ChromeDriver();
		  
		  
		  
		  //***********Extent Report Implementation***********
		  report = new ExtentReports(reportPath+this.getClass().getSimpleName()+".html", false);
		  
		  //Starting the test case for reporting
		  test = report.startTest("Extent Report Example Project");
		  
		  //***********Extent Report Implementation***********
	  }
	
  @Test
  public void extentReport() {
	  driver.get("http://www.inviul.com/");
	  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
	  driver.manage().window().maximize();
	  String title = driver.getTitle();

	  //Setting Pass Failure Log
	  if(title.contains("Avinash")){
		 System.out.println(title);
		  test.log(LogStatus.PASS, "Validate title matched for the web page", "Title matched");
	  }else{
		  
		  test.log(LogStatus.FAIL, "Validate title matched for the web page", "Title didn't match");
	  }
	  
	  
  }
  

  @AfterMethod
  public void afterMethod() {
	  driver.close();
	  driver.quit();
	  
	  //Closing the report
	  report.endTest(test);
	  report.flush();
	  report.close();
  }

}

In the above code, you see the creation of the instance of both ExtentReports class and ExtentTest class are done inside @BeforeMethod annotation. Further, logging is done inside the @Test annotation. Ultimately, report closing and flushing are handled inside the @AfterMethod annotation.

Tip:

One thing you should have noticed that there is a Boolean as another argument in ExtentReports() class. Right?

So, you may ask its significance here.

If we keep false then the single report will hold all the logs, otherwise on setting it to true will lead to the generation of multiple report file for every pass-fail status.

Extent Reports Dashboard

This is all about implementation of Extent Reports in your Selenium project. If you have any queries, please share in the comment below and join our Facebook group for the latest updates.

Join Inviul fb group

10 Comments

Leave a Reply