Usage of TestNG Listeners-Types of Listeners

Refer to the below Links for Practise
Collections in Java
Complete List of Selenium Basics
50 Real Time Interview Questions
Real Time Scenario’s
Java Platform Standard Edition 8 Documentation
Complete List of Java Basics
Java Quiz
Java OOPS Quiz
Selenium Advanced Quiz
Selenium Quiz
Selenium WebDriver Resume

TestNG Listeners

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

ISuiteListener

IInvokedMethodListener

IExecutionListener

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.

ListenerClass

TestClass for Automation

TestNg.XML

Leave a Reply

Your email address will not be published. Required fields are marked *