1. What are Listeners?
• TestNG listeners are set of class exposed into TestNG framework which we can utilize to modify default TestNG’s behavious.
• As the name suggests Listeners “listen” to the event and behave accordingly.
• It allows customizing TestNG reports or logs or to take a screen shot.
• Listeners contains unimplemented methods(blank body and we can customize it)
2. Types of Listeners in TestNG
• IAnnotationTransformer
• IAnnotationTransformer2
• IConfigurable
• IConfigurationListener
• IExecutionListener
• IHookable
• IInvokedMethodListener
• IInvokedMethodListener2
• IMethodInterceptor
• IReporter
• ISuiteListener
• ITestListener
ITestListener
ITestListerner is an interface which implements with TestNG .
We can either extend ‘TestListenerAdapter’ or implement Interface ‘ITestListener’ for test running. For custom listener we extends TestListenerAdapter which implements ITestListener. Hence no need to override all methods of interface.
ITestListener contains below unimplemented methods
1 2 3 4 5 6 7 |
OnStart- OnStart method is called when any Test starts. onTestSuccess- onTestSuccess method is called on the success of any Test. onTestFailure- onTestFailure method is called on the failure of any Test. onTestStart-Invoked each time before a test will be invoked. onTestSkipped- onTestSkipped method is called on skipped of any Test. onTestFailedButWithinSuccessPercentage- method is called each time Test fails but is within success percentage. onFinish- onFinish method is called after all Tests are executed. |
ISuiteListener
1 2 |
onStart() & onFinish(). Whenever a class implements this listener, TestNG guarantees the end-user that it will invoke the methods onStart() and onFinish() before and after running a TestNG Suite. |
IInvokedMethodListener
1 2 |
afterInvocattion() & beforeInvocation() It makes the call before and after every Method. |
IExecutionListener
1 2 3 |
onExecutionStart() onExecutionFinish() Method onExecutionStart() run before the TestNG starts running the suites and onExecutionFinish() run after TestNG has completed running all the test suites. |
Steps to create a TestNG Listener
Step1
• Create a Test class for automation
Step 2
• Create a listener class that implements TestNG listeners
• import org.testng.ITestNGListener
• Add unimplemented methods
• Customize the methods
Step 3
Implement the listeners to the Test class
Method 1: class level
• use listeners annotation
@listeners (packageName.ListenerClassName)
• Execute the Test class. Methods in the Listener class are called automatically according to the behavior of methods annotated as @Test.
Method 2: Multiple classes
• Create a testng.xml and add listeners tag in XML.
1 2 3 |
<listeners> <listener class-name="packageName.ListenerClassName" /> </listeners> |
ListenerClass
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 |
package totalQA; import org.testng.ITestContext; import org.testng.ITestListener; import org.testng.ITestResult; public class Listeners implements ITestListener{ @Override public void onTestStart(ITestResult result) { System.out.println("The name of the testcase started is :"+result.getName()); } @Override public void onTestSuccess(ITestResult result) { System.out.println("The name of the testcase Pass is :"+result.getName()); } @Override public void onTestFailure(ITestResult result) { System.out.println("The name of the testcase failed is :"+result.getName()); } @Override public void onTestSkipped(ITestResult result) { System.out.println("The name of the testcase skipped is :"+result.getName()); } @Override public void onTestFailedButWithinSuccessPercentage(ITestResult result) { } @Override public void onStart(ITestContext context) { } @Override public void onFinish(ITestContext context) { // TODO Auto-generated method stub System.out.println("The name of the testcase finished is :"+context.getName()); } } |
TestClass for Automation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package totalQA; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.Parameters; import org.testng.annotations.Test; @Listeners(totalQA.Listeners.class) public class login{ @Parameters({"URL" }) @Test public void login(String url,String uname, String pword) { System.getProperty("webdriver.Chrome.driver","chromedriver.exe"); WebDriver driver; driver= new ChromeDriver(); driver.get(url); Assert.assertTrue(false); driver.close(); } } |
TestNg.XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite"> <listeners> <listener class-name="totalQA.Listeners" /> </listeners> <test name="Test Login2 "> <parameter name="URL" value="http://total-qa.com"></parameter> <classes> <class name="totalQA.login" /> </classes> </test> <!-- Test --> </suite> <!-- Suite --> |
Loved this page. Thank you.
http://total-qa.com/usage-testng-listeners-types-listeners/