hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

How to generate Extent Reports in selenium

In this topic we can learn how to generate Extent reports in selenium. firstly what is extent reports?

Extent Reports is an open-source reporting library used in selenium test automation. Extent reports become the first choice of Selenium Automation Testers, even though Selenium comes with inbuilt reports using frameworks like JUnit and TestNG. With extent reports, you can offer a more extensive and insightful perspective on the execution of your automation scripts. So let’s see why Automation testers prefer Extent reports to others.

Use of Extent Reports

  • Extent reports are more customizable than others

  • Extent API can produce more interactive reports, a dashboard view, graphical view, capture screenshots at every test step, and emailable reports

  • It can be easily integrated with frameworks like JUnit, NUnit, & TestNG

  • It displays the time taken for test case execution.

Steps To Generate Extent Reports:

  1. Firstly, create a TestNG project in eclipse

  2. Add dependency to your project POM.XML file

<dependency>

<groupId>com.relevantcodes</groupId>

<artifactId>extentreports</artifactId>

<version>2.41.2</version>

</dependency>

  1. Create a java class say ‘ExtentreportsListener’ and add the following code to it.

package extentreports;

import java.io.File; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Map;

import org.testng.IReporter; import org.testng.IResultMap; import org.testng.ISuite; import org.testng.ISuiteResult; import org.testng.ITestContext; import org.testng.ITestResult; import org.testng.xml.XmlSuite;

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

public class ExtentreportListener implements IReporter{ private ExtentReports extent; public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) { extent = new ExtentReports(outputDirectory + File.separator + “ExtentReportTestNG.html”, true); for (ISuite suite : suites) { Map<String, ISuiteResult> result = suite.getResults(); for (ISuiteResult r : result.values()) { ITestContext context = r.getTestContext(); buildTestNodes(context.getPassedTests(), LogStatus.PASS); buildTestNodes(context.getFailedTests(), LogStatus.FAIL); buildTestNodes(context.getSkippedTests(), LogStatus.SKIP); } } extent.flush(); extent.close(); } private void buildTestNodes(IResultMap tests, LogStatus status) { ExtentTest test; if (tests.size() > 0) { for (ITestResult result : tests.getAllResults()) { test = extent.startTest(result.getMethod().getMethodName()); test.setStartedTime(getTime(result.getStartMillis())); test.setEndedTime( getTime(result.getEndMillis())); for (String group : result.getMethod().getGroups()) test.assignCategory(group); String message = “Test “ + status.toString().toLowerCase() + “ed”; if (result.getThrowable() != null) message = result.getThrowable().getMessage(); test.log(status, message); extent.endTest(test); } } } private Date getTime(long millis) { Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(millis); return calendar.getTime(); }

}

2. Next In testng.xml file add listener class tag of ExtentreportListener

<listeners>

<listener class-name=”extentreports.ExtentreportListener” />

</listeners>

3.After running your project extent report automatically generated and it stored in test-output folder with name of ExtentReportTestNG.html.you can find in below screenshot.




4.if you click on ExtentReportTestNG.html file it will open the report in browser, you will see beautiful high rich HTML test results as shown below.

Test Summary Report:




I hope this blog will you help you to generate Extent Reports for your project .

Please follow me for next blog ,Thank you.

161 views0 comments

Recent Posts

See All