Generating Extent Reports – Java
Extent reports are very rich HTML reports for the selenium webdriver. This report will give so much of information about the execution status. The extra information can be provided from the external XML file. Once you create the XML file need to load in the program to read the configuration.
We can provide below information to the report using XML file:
- Report Title
- Report Name
- Report Headline
- Date Format
- Time Format
Apart from the above we can provide HostName, Environment Information and User Name etc… from the program.
Below is the sample program:
import java.io.File; import org.testng.Assert; import org.testng.ITestResult; 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 GenerateExtentReport { ExtentReports extent; ExtentTest test; @BeforeTest public void startReport() { extent = new ExtentReports(System.getProperty("user.dir") +"/test-output/MyOwnReport.html", true); extent .addSystemInfo("Host Name", "Krishna") .addSystemInfo("Environment", "QA") .addSystemInfo("User Name", "Krishna Sakinala"); extent.loadConfig(new File(System.getProperty("user.dir")+"\\extent-config.xml")); } @Test public void demoReportPass() { test=extent.startTest("demoReportPass"); Assert.assertTrue(true); test.log(LogStatus.PASS, "Assert Pass as condition is True"); } @Test public void demoReportFail() { test=extent.startTest("demoReportFail"); Assert.assertTrue(false); test.log(LogStatus.FAIL, "Assert Fail as condition is False"); } @AfterMethod public void getResult(ITestResult result) { if(result.getStatus()==ITestResult.FAILURE) { test.log(LogStatus.FAIL, result.getThrowable()); } extent.endTest(test); } @AfterTest public void endreport() { extent.flush(); extent.close(); } }
In the above program, we have used two classes called ExtentReports, ExtentTest. These two classes objects will help us to create a rich HTML reports with lot of useful information. ExtentReports will take the HTML report file path as an argument and it will create the report file with the mentioned name in the mentioned location. And ExtentTest will be used to log the so much information to the report like PASS, FAIL and SKIP status of the executed tests.
Important points to be remember:
- ExtentReports will create the report file.
- ExtentTest will log the information in the report.
- startTest() method of ExtentReports class is the starting point of the test and it will return the ExtentTest object.
- We need to capture that object into ExtentTest object.
- Used this reference to log the information into the report.
- ExtentReports object will be used to add the report information like Title, Header and Theme etc..
- And the above configuration need to passed from the external XML file using loadConfig() method of ExtentReports class. It will take the XML file path as argument.
- endTest() method of ExtentReports will stop capturing information about the test log.
- flush() method of ExtentReports wil push/write everything to the document.
- close() method of ExtentReports will clear/close all resource of the ExtentReports object.
Below is the sample configuration file:
<!--?xml version="1.0" encoding="UTF-8"?--> <!-- report theme --> <!-- standard, dark --> standard <!-- document encoding --> <!-- defaults to UTF-8 --> UTF-8 <!-- protocol for script and stylesheets --> <!-- defaults to https --> https <!-- title of the document --> ExtentReports 2.0 <!-- report name - displayed at top-nav --> Automation Report <!-- report headline - displayed at top-nav, after reportHeadline --> <!-- global date format override --> <!-- defaults to yyyy-MM-dd --> yyyy-MM-dd <!-- global time format override --> <!-- defaults to HH:mm:ss --> HH:mm:ss <!-- custom javascript --> <scripts> <![CDATA[ $(document).ready(function() { }); ]]> </scripts> <!-- custom styles --> <styles> <![CDATA[ ]]> </styles> </configuration> </extentreports>
This ways we can configure the report information and can generate the extent reports.
Output of the above program is:
Please watch the YouTube video of this blog for better understanding.