TestNG listeners are the piece of code that listens to the events occurring in the TestNG.
TestNG Listeners are applied as interfaces in the code because “Listeners” is a “class” in TestNG
Types Of Listeners In TestNG
TestNG provides a bunch of listeners as a part of its testing environment. These listeners are as follows:
ITestListener
IReporter
ISuiteListener
IInvokedMethod
IHookable
IConfigurationListener
IConfigurableListener
IAnnotationTransformer
IExecution
IMethodInterceptor
TestNG Listeners are applied as interfaces in the code because “Listeners” is a “class” in TestNG
Types Of Listeners In TestNG
TestNG provides a bunch of listeners as a part of its testing environment. These listeners are as follows:
ITestListener
IReporter
ISuiteListener
IInvokedMethod
IHookable
IConfigurationListener
IConfigurableListener
IAnnotationTransformer
IExecution
IMethodInterceptor
ITestListener In TestNG
1.ITestListener implements since it is an interface
2. ITestListener listens to specific events (depending on its methods) and executes the code written inside the method
MoreInfo
Please use this URL
Steps:
1.Create a new class with Listeners
//ITestListener interface which implements TestNG listeners
2. Listeners implements(Keyword) ITestListener
//Implement all the methods present in this interface
3. Add unimplemented methods
4.Based on Test fail/Pass
5. Explore on other methods
6. To invoke Listeners class
a.create testng.xml file
b. Click finish
c. Add below Listeners
<listeners>
<listener class-name = "ExtentReport.ExtentReports.Listeners"></listener>
</listeners>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests">
<listeners>
<listener class-name = "ExtentReport.ExtentReports.Listeners"></listener>
</listeners>
<test thread-count="5" name="Test">
<classes>
<class name="ExtentReport.ExtentReports.DemoReport" />
<class name="ExtentReport.ExtentReports.TestFail" />
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
=====================
Listeners
package ExtentReport.ExtentReports;
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import ExtentReport.ExtentReports.Base;
//ITestListener interface which implements TestNG listeners
public class Listeners extends Base implements ITestListener {
ExtentTest test;
ExtentReports extent=ExtentReporterNG.getReportObject();
ThreadLocal<ExtentTest> extentTest = new ThreadLocal<ExtentTest>();
public void onTestStart(ITestResult result) {
// TODO Auto-generated method stub
test= extent.createTest(result.getMethod().getMethodName());
extentTest.set(test);
}
public void onTestSuccess(ITestResult result) {
// TODO Auto-generated method stub
extentTest.get().log(Status.PASS, "Test Passed");
}
public void onTestFailure(ITestResult result) {
// TODO Auto-generated method stub
extentTest.get().fail(result.getThrowable());
WebDriver driver = null;
String testMethodName = result.getMethod().getMethodName();
try {
driver =(WebDriver)result.getTestClass().getRealClass().getDeclaredField("driver").get(result.getInstance());
} catch(Exception e)
{
}
try {
extentTest.get().addScreenCaptureFromPath(getScreenShotPath(testMethodName,driver), result.getMethod().getMethodName());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void onTestSkipped(ITestResult result) {
// TODO Auto-generated method stub
}
public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
// TODO Auto-generated method stub
}
public void onStart(ITestContext context) {
// TODO Auto-generated method stub
}
public void onFinish(ITestContext context) {
// TODO Auto-generated method stub
extent.flush();
}
}
Listeners
package ExtentReport.ExtentReports;
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import ExtentReport.ExtentReports.Base;
//ITestListener interface which implements TestNG listeners
public class Listeners extends Base implements ITestListener {
ExtentTest test;
ExtentReports extent=ExtentReporterNG.getReportObject();
ThreadLocal<ExtentTest> extentTest = new ThreadLocal<ExtentTest>();
public void onTestStart(ITestResult result) {
// TODO Auto-generated method stub
test= extent.createTest(result.getMethod().getMethodName());
extentTest.set(test);
}
public void onTestSuccess(ITestResult result) {
// TODO Auto-generated method stub
extentTest.get().log(Status.PASS, "Test Passed");
}
public void onTestFailure(ITestResult result) {
// TODO Auto-generated method stub
extentTest.get().fail(result.getThrowable());
WebDriver driver = null;
String testMethodName = result.getMethod().getMethodName();
try {
driver =(WebDriver)result.getTestClass().getRealClass().getDeclaredField("driver").get(result.getInstance());
} catch(Exception e)
{
}
try {
extentTest.get().addScreenCaptureFromPath(getScreenShotPath(testMethodName,driver), result.getMethod().getMethodName());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void onTestSkipped(ITestResult result) {
// TODO Auto-generated method stub
}
public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
// TODO Auto-generated method stub
}
public void onStart(ITestContext context) {
// TODO Auto-generated method stub
}
public void onFinish(ITestContext context) {
// TODO Auto-generated method stub
extent.flush();
}
}
No comments:
Post a Comment