In this post, we will see how we can integrate extent report with testng listeners, in our previous post we have seen how to setup and integrate extent report with testNG classes.
TestngListeners helps us to execute set of code blocks in each event like before and after executing the test cases which reduces lines of code and dynamically generates logs based on what event it’s executing currently.
Watch demo for detail steps –
Folder structure –
extentReport
— — ExtentManager.java
— — ExtentTestManager.java
— — TestListeners.java
— — DriverContext.java
— — TestSampleExtentListeners.java
Code samples –
ExtentManager.java
Already described in our previous post
ExtentTestManager.java
package extentReport; import com.aventstack.extentreports.ExtentReports; import com.aventstack.extentreports.ExtentTest; public class ExtentTestManager { private static ExtentReports extent = ExtentManager.getInstance(); static ExtentTest test; public static ExtentTest getTest() { return test; } public static void createTest(String testName, String Desc) { test = extent.createTest(testName, Desc); } }
TestListeners.java
package extentReport; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.IInvokedMethod; import org.testng.IInvokedMethodListener; import org.testng.ITestContext; import org.testng.ITestListener; import org.testng.ITestResult; import com.aventstack.extentreports.ExtentReports; import com.aventstack.extentreports.Status; public class TestListeners implements ITestListener, IInvokedMethodListener{ private static ExtentReports extent = ExtentManager.getInstance(); @Override public void beforeInvocation(IInvokedMethod method, ITestResult testResult) { if(method.isTestMethod()) { WebDriver driver; System.setProperty("webdriver.chrome.driver", "/Users/sunilkumarpatro/sel/chromedriver"); driver = new ChromeDriver(); DriverContext.setDriver(driver); driver.get("http://qavalidation.com/"); } } @Override public void afterInvocation(IInvokedMethod method, ITestResult testResult) { if(DriverContext.getDriver() != null) { DriverContext.getDriver().quit(); } } @Override public void onTestStart(ITestResult result) { ExtentTestManager.createTest(result.getMethod().getMethodName(), result.getMethod().getDescription()); ExtentTestManager.getTest().info(result.getMethod().getMethodName() + " Started..."); } @Override public void onTestSuccess(ITestResult result) { ExtentTestManager.getTest().log(Status.PASS, "Test case Passed"); } @Override public void onTestFailure(ITestResult result) { ExtentTestManager.getTest().log(Status.FAIL, "Test failed because of "+ result.getThrowable()); } @Override public void onTestSkipped(ITestResult result) { } @Override public void onTestFailedButWithinSuccessPercentage(ITestResult result) { } @Override public void onStart(ITestContext context) { } @Override public void onFinish(ITestContext context) { extent.flush(); } }
TestSampleExtentListeners.java
package extentReport; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Assert; import org.testng.annotations.*; import com.aventstack.extentreports.ExtentReports; import com.aventstack.extentreports.ExtentTest; import com.aventstack.extentreports.Status; public class TestSampleExtentListeners { @Test public void checkSignupPage() { //System.out.println("Site Title - " + driver.getTitle()); ExtentTestManager.getTest().info("Site Title - " + DriverContext.getDriver().getTitle()); DriverContext.getDriver().findElement(By.linkText("DemoForm")).click(); ExtentTestManager.getTest().info("Site Title after clicking link - " + DriverContext.getDriver().getTitle()); Assert.assertTrue(DriverContext.getDriver().getTitle().contains("Practice")); } }
Run below testNG.xml file to generate the html report
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="Suite1" verbose="1" > <listeners> <listener class-name = "extentReport.TestListeners"/> </listeners> <test name="testNGListenerTest"> <classes> <class name="extentReport.TestSampleExtentListeners" /> </classes> </test> </suite>
Hope this helps!
Share with friends…