TestNG Listeners in Selenium:
- The listener is defined as the interface which modifies the behavior of the default TestNG.
- As the name suggests, listeners “listen” for the event defined in the Selenium script and behave accordingly.
- It is used in selenium by applying listeners interface.
- TestNG listeners use to create logs in Selenium.
- TestNG can generate step by step logs with the help of Listeners.
- It allows customizing TestNG reports or logs with the help of listeners.
TestNG provides us below listeners:
- IExecutionListener
- IAnnotationTransformer
- ISuiteListener
- ITestListener
- IConfigurationListener
- IMethodInterceptor
- IInvokedMethodListener
- IHookable
- IReporter
Note: These interfaces are used in selenium to generate logs or customize the testing reports with the help of Step by Step Scenario’s.
ITestListener ( I ):
When we use ITestListener:
- After successfully completing each test.
- After each failed test.
- After each test skipped.
- After the completion of all tests.
Methods in ITestListener are following below:
1. OnStart( ):
onStart method is called when any Test starts.
1 | public void onTestStart(ITestResult result) |
2. onTestSuccess( ):
onTestSuccess method is called on the success of any Test.
1 | public void onTestSuccess(ITestResult result) |
3. onTestFailure( ):
onTestFailure method is called on the failure of any Test.
1 | public void onTestFailure(ITestResult result) |
4. onTestSkipped( ):
onTestSkipped method is called on skipped of any Test.
1 | public void onTestSkipped(ITestResult result) |
5. onTestFailedButWithinSuccessPercentage( ):
method is called each time Test fails but is within success percentage.
1 | public void onTestFailedButWithinSuccessPercentage(ITestResult result) |
6. onFinish( ):
onFinish method is called after all Tests are executed.
1 | public void onFinish(ITestContext context) |
Step 1: Create a simple class and implement ITestListener listener.
1 2 3 4 5 | public class Listeners implements ITestListener { // Declaration } |
Step 2: Add all unimplemented methods.
Note: Before execute program add Listener class into TestNG.xml file then Run as TestNG Suite to TestNG listeners in Selenium.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | package package_name; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.ITestContext; import org.testng.ITestListener; import org.testng.ITestResult; import org.testng.annotations.Test; public class Listeners implements ITestListener { @Test public void open_Website() { System.setProperty("webdriver.gecko.driver","D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); } @Override public void onFinish(ITestContext arg0) { // TODO Auto-generated method stub } @Override public void onStart(ITestContext arg0) { // TODO Auto-generated method stub } @Override public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) { // TODO Auto-generated method stub } @Override public void onTestFailure(ITestResult arg0) { // TODO Auto-generated method stub } @Override public void onTestSkipped(ITestResult arg0) { // TODO Auto-generated method stub } @Override public void onTestStart(ITestResult arg0) { // TODO Auto-generated method stub System.out.println("TestCase onTestStart====" +arg0.toString()); } @Override public void onTestSuccess(ITestResult arg0) { // TODO Auto-generated method stub System.out.println("TestCase onTestSuccess====" +arg0.toString()); } } |
Output:
1 2 3 | TestCase onTestStart====[TestResult name=open_Website status=STARTED method=Listeners.open_Website()[pri:0, instance:package_name.Listeners@754ba872] output={null}] TestCase onTestSuccess====[TestResult name=open_Website status=SUCCESS method=Listeners.open_Website()[pri:0, instance:package_name.Listeners@754ba872] output={null}] |
Step 3: Add listener class into TestNG.xml file.
1 2 3 4 5 | <listeners> <listener class-name="package_name.Listeners"/> </listeners> |
Example: Add listeners into existing TestNG.xml file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="stqatools" parallel="methods" thread-count="2"> <listeners> <listener class-name="package_name.Listeners"/> </listeners> <test name="testngTest"> <classes> <class name="package_name.Listeners" /> </classes> </test> </suite> |