If you use ExtentReports and do not want to use it like a normal logger, it is also possible to use it as a listener for TestNG to still be able to generate a beautiful report with dashboards. This post shows a quick sample of how to do so. Since this is a basic sample, you can easily modify the listener to suit your needs by adding more features to it – Cedric has already shown us how to do so here.
Below is a sample testng.xml with the listener added:
<?xml version="1.0" encoding="UTF-8"?> <suite name="SuiteName"> <listeners> <listener class-name="package.ExtentReporterNG" /> </listeners> <test name="Test" allow-return-values="true"> <classes> <class name="package.class" /> </classes> </test> </suite> |
This is the listener class that the above testng.xml uses:
import java.io.File; 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 ExtentReporterNG implements IReporter { private ExtentReports extent; @Override public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) { extent = new ExtentReports(outputDirectory + File.separator + "Extent.html", true); for (ISuite suite : suites) { Map<String, ISuiteResult> result = suite.getResults(); for (ISuiteResult r : result.values()) { ITestContext context = r.getTestContext(); buildTestNodes(context.getFailedTests(), LogStatus.FAIL); buildTestNodes(context.getSkippedTests(), LogStatus.SKIP); buildTestNodes(context.getPassedTests(), LogStatus.PASS); } } extent.flush(); } private void buildTestNodes(IResultMap tests, LogStatus status) { ExtentTest test; if (tests.size() > 0) { for (ITestResult result : tests.getAllResults()) { test = extent.startTest(result.getMethod().getMethodName()); String message = "Test " + status.toString().toLowerCase() + "ed"; if (result.getThrowable() != null) message = result.getThrowable().getMessage(); test.log(status, message); extent.endTest(test); } } } } |
Let’s test the listener using the following 2 tests against Google (one passes, other fails):
// Notice in this example, there are no calls made to ExtentReports // but because ExtentReporterNG implements IReporter, a report will still get created // with passed and failed tests // Output: http://relevantcodes.com/Articles/ExtentReporterNG/Extent.html public class GoogleTest { private WebDriver driver; @Test public void googleAssertPass() { driver = new FirefoxDriver(); driver.get("http://google.com"); Boolean isFound = IsElementPresent(driver, By.cssSelector("input[value*='Google Search']")); Assert.assertTrue(isFound); } @Test public void googleAssertFail() { driver = new FirefoxDriver(); driver.get("http://google.com"); Boolean isFound = IsElementPresent(driver, By.cssSelector("input[value*='Googl Search']")); Assert.assertTrue(isFound); } private Boolean IsElementPresent(WebDriver driver, By by) { try { driver.findElement(by); return true; } catch (NoSuchElementException e) { return false; } } @AfterMethod public void afterMethod() { driver.quit(); } } |
If you would like to download the project, Charan has created and uploaded it to Dropbox.
Running the above example with the listener will produce the following output in the test-output folder created by testng.