What is Listener?
Listener is defined as interface that modifies the default behavior. Listeners listen to particular events of WebDriver.
What is Event?
Event refers to any action that we perform using WebDriver like click, type, navigate etc. this all are termed as Events.
Why to use Listener?
We can make the use of listeners to perform set of actions before and after a WebDriver event.
There are two main listeners.
- WebDriver Listeners
- TestNG Listeners
- WebDriver Listeners : WebDriver Event Listeners in Selenium WebDriver. Listeners “listen” to the event defined in the selenium script and behave accordingly. Listeners are mainly used for generating reports and logs. Below are the class and interface listeners in Selenium:
- WebDriverEventListener: This listener allows to implement the methods and selenium WebDriver perform the activities like Type, Navigate, Click, etc. For tracking these activities, this listener should be implemented in the WebDriver Event Listener interface is used.
- EventFiringWebDriver: This is an event firing WebDriver class used for firing WebDriver Event.
2. TestNG Listeners : These Listeners “listen” to the event defined in the selenium script and behave as per the defined events. These listeners are used for generating logs and customize TestNG Reports.
There are many types of TestNG listeners available. Below are the few TestNG listeners:
- IAnnotationTransformer
- IAnnotationTransformer2
- IConfigurable
- IConfigurationListener
- IExecutionListener
- IHookable
- IInvokedMethodListener
- IInvokedMethodListener2
- IMethodInterceptor
- IReporter
- ISuiteListener
- ITestListener
Test automation services adopt very high standards for script development & Reports. Software testing company write Selenium test cases in TestNG, and calling the TestNG suite from selenium.Above Interface are called TestNG Listeners. These interfaces are used in selenium to generate logs or customize the automation testing reports. Below is one example
ITestListener has following methods
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.
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.