Extent Report

Share on facebook
Share on twitter
Share on linkedin
Extent Report is a library that can be used to build a customized detailed report. It can be integrated with TestNG, JUNIT etc.

Introduction: 

Extent Report is a library that can be used to build a customized detailed report. It can be integrated with TestNG, JUNIT etc. This report can be built in JAVA, .NET and it provides detailed summary about each testcases and its each test steps too in graphical manner. The Extent Report library is paid library from version 4; However, it is a free source till the version Extent Report 3. Let us see how to build that report in JAVA. 

Installation: 

1.Visit https://jar-download.com/artifacts/com.aventstack/extentreports/3.1.5/source-code 

2.Click the ‘Download’ link and download the Zip. And then extract the zip and store in your local file system  

3.Import the downloaded jar file into your project using below steps: 

  • I.Right click the project 
  • II.Click on ‘Build  Path’ > ‘Configure Build Path’  

  • III.Click on ‘Libraries’ > ‘Add External Jars’  

  • IV.Select the below Jar files stored in your local file system and click on ‘Apply and Close’.  

If you are using a Maven project, we can add the dependency as below: 

  1. <!– pom.xml –> 
  2. <dependency> 
  3.     <groupId>com.aventstack</groupId> 
  4.     <artifactId>extentreports</artifactId> 
  5.     <version>3.1.5</version> 
  6. </dependency>  

Initialize the report: 

Initializing the report is the first step in this journey. Here, first we need to specify the file location where we want our report to be stored. Then, we need to create the HtmlReporter in that path and need to attach that htmlReporter with the extentReport. The syntax is given below:  

Creating the Test case and test steps: 

Assume we have two testcases (A, and B) and each testcase have 2 steps as A1, A2, B1, B2. We need to build the report for the test cases and steps by using the ‘createTest’ and ‘createNode’ methods as below:  

We can also achieve this by shortening the code as below:  

Logging the status as pass/fail: 

We can update the status of the execution in below two ways:  

Assigning author name to the testcases: 

If more than one resource is working for same project, it would be helpful if we can see the author name in the report to identify each resource contribution. We can assign the author name for the testcase as below:  

Assigning category to the testcases: 

If we want to see the report by category, then we need to assign the categories to each test case as below:  

Adding system level details: 

If we want to add few information around the environment or system, we can do that as below:  

Adding screenshot: 

When the testcase is failed, we need the screenshot to be captured to identify the exact bug/defect exists in the functionality. We can achieve the same in Extent Report also by using the below code:  

The above method takes the screen print of the webdriver and stores that in the ‘file’ object. Then, this ‘file’ is copied into new file called ‘sample.jpg’. After that new ExtentTest object is created and the file ‘sample.jpg’ is added to that ExtentTest object along with the test status as ‘fail’ and description as ‘Attached screen shot’. 

Flush the report: 

After we create the report and the required test cases and test steps details, it is important to write everything into the file. Without writing into the report, we cannot view the report after the execution. To write into the report, we need to use the flush method as below:  

Extent Html report configurations: 

The look and feel of the report and few other attributes can be customized through the custom config. This can be achieved in two ways: 

  1. Normal coding 
  2. Using xml config file 

1.Using normal coding: 

We can customize the report’s title, protocol, encoding, theme, and few other attributes by using below code: 

Couple of other available configurations are listed below:  

2.Using xml config file: 

The same customization can be achieved using the xml file. For this, first we need to build the xml file as below:? 

  1. <?xml version=“1.0” encoding=“UTF-8”?> <extentreports> 
  2. <configuration>
  3. <!– title ofthe document –> 
  4. <documentTitle>MST Automation Report</documentTitle>
  5. <!– protocol for script and stylesheets–>
  6. <!- defaults to https–>
  7. <protocol>https</protocol> 
  8.  <!–document encoding –> 
  9. <!–defaults to UTF-8 –> 
  10. <encoding>UTF-8</encoding> 
  11. <!–report theme –> 
  12. <!–standard,dark–>
  13. <theme>standard</theme> 
  14.  <!–report name = displayed at top-nav with logo–> 
  15. <reportName?align=“center”> 
  16. <![CDATA[
  17. <img src=”../logo.png” style=”position:absolute;left:75px;”/> 
  18. <message>MST Automation Demo result summary</message>
  19.  </reportName> 
  20. <!–locationofchartsinthetestview–> 
  21. <!–top,bottom–>
  22. <testViewChartLocation>bottom</testViewChartLocation>
  23. </configuration> 
  24. </extentreports> 

Then, we need to load this xml file into the html Extent Report by using the code as below:  

Sample report: 

Dashboard view:

Category view:

Author view:

Exception view:

Testcase view:

Sample code 

Report Factory:

This code snippet will create a html Extent report in the specified location and customize the report as mentioned in the xml configuration file. 

  1. public class ExtentReportFactory{
  2.  private static ExtentHtmlReporter htmlReporter;
  3.  private static ExtentReports reporter; 
  4. public static synchronized ExtentReports getReporter(){ 
  5. String fileName= “src/test/resources/extent_config.xml”;
  6. File file =new File(fileName); 
  7.  if (reporter==null){ 
  8. htmlReporter=newExtentHtmlReporter(“Report/report.html”); 
  9. htmlReporter.loadXMLConfig(file);
  10. reporter = new ExtentReports(); 
  11. reporter.attachReporter(htmlReporter); 
  12. reporter.setSystemInfo(“Author”, “MST QA Automation”); 
  13. reporter.setSystemInfo(“User Name”,“Automation”); 
  14. reporter.setSystemInfo(“Environment”,“QA”); 
  15. reporter.setSystemInfo(“Selenium Version”,“3.4.0”);
  16. returnreporter;

Report Generator: 

  1. public class ReportGenerator{ 
  2. ExtentTest childTest; 
  3. ExtentTest parentTest; 
  4.  private static ExtentReports reporter =ExtentReportFactory.getReporter(); 
  5.  public void parentReport(String methodName,String author){ 
  6. /creates a test case with the value of received parameter ‘methodName’ 
  7. parentTest = reporter.createTest(methodName); 
  8. //assigns the author 
  9. parentTest.assignAuthor(author); 
  10. //assignsn the category 
  11. parentTest.assignCategory(“Demo Test cases”); 
  12.  public void childReport(String methodName){
  13. //creates a test step for the test case with the value ofreceived ‘methodName’ parameter 
  14. childTest = parentTest.createNode(methodName); 
  15. //logs the test step status as pass 
  16. childTest.log(Status.PASS, methodName); 
  17. public void flush(){ 
  18. //flushes the report
  19. reporter.flush(); 
  20. public void logScreenshot(WebDriver screenDriver, String testCaseName, String res, Exception e) throws IOException{
  21. try{ 
  22. //take the screen print of the screenDriver 
  23. File file=((TakesScreenshot)screenDriver).getScreenshotAs(OutputType.FILE); 
  24. File dir = new File(“Report/screenshot/”+testCaseName); 
  25. //make the directory with the name mentioned above  
  26. dir.mkdirs(); 
  27. StringfileName=“Report/screenshot/”+testCaseName+“/”+testCaseName+“.jpg”;
  28. //copy the screen print into the path mentioned above
  29. FileUtils.copyFile(file, new File(fileName)); 
  30. //creates a new test step 
  31. ExtentTest logger= this.childTest; 
  32. //logs the test step status as fail with the exception as the description
  33. logger.fail(e); 
  34. //logs the screen print taken 
  35. logger.info(“Attachedscreenshot”).addScreenCaptureFromPath(“screenshot/”+testCaseName+“/”+testCaseName+“.jpg”);
  36. catch(Exception ex){ 
  37. throw new CustomException(“Exceptionwhiletakingscreenshot: “+ex);
  38. publicvoidlogSkipTest(WebDriverscreenDriver,StringtestCaseName,Stringres)throws IOException{ 
  39. Filefile=((TakesScreenshot)screenDriver).getScreenshotAs(OutputType.FILE); 
  40. File dir =new File(“Report/screenshot/”+testCaseName); 
  41. dir.mkdirs(); 
  42. StringfileName=“Report/screenshot/”+testCaseName+“/”+testCaseName+“.jpg”;
  43. FileUtils.copyFile(file, new File(fileName)); 
  44. ExtentTest logger = this.childTest;
  45. //logs the test step status as skip and attaches the screenshottaken
  46. logger.skip(“Attachedscreenshot”).addScreenCaptureFromPath(“screenshot/”+testCaseName+“/”+testCaseName+“.jpg”); 

Conclusion: 

The Extent Report has lot of other features too , but we can create a detailed report by using the above features itself. This report can be incorporated and customized to many frameworks like pom, data driven, hybrid and BDD. 

About MST

At MST Solutions our cornerstone is to adapt, engage and create solutions which guarantee the success of our clients. The talent of our team and experiences in varied business verticals gives us an advantage over other competitors.

Recent Articles

Mobile Responsive Testing

Mobile Responsive Testing is simply a Testing Process performed to make sure that your website or an app is working appropriately on all types of devices.
Whether it an app or website, they need to ensure that they are accessible from any device, anytime, anywhere.

Read Article »

Work with us.

Our people aren’t just employees, they are key to the success of our business. We recognize the strengths of each individual and allow them time and resources to further develop those skills, crafting a culture of leaders who are passionate about where they are going within our organization.