07 - 02 - TestNG Listeners in Selenium
07 - 02 - TestNG Listeners in Selenium
Selenium: ITestListener
& ITestResult
May-2019
RBVH/ENG1
Content
What is Listeners in TestNG?
Types of Listeners in TestNG
Test Scenario:
Steps to create a TestNG Listener
Use of Listener for multiple classes.
2
What is Listeners in TestNG?
3
Types of Listeners in TestNG
There are many types of listeners which allows you to change the TestNG's behavior.
Below are the few TestNG listeners:
1.IAnnotationTransformer ,
2.IAnnotationTransformer2 ,
3.IConfigurable ,
4.IConfigurationListener ,
5.IExecutionListener,
6.IHookable ,
7.IInvokedMethodListener ,
8.IInvokedMethodListener2 ,
9.IMethodInterceptor ,
10.IReporter,
11.ISuiteListener,
12.ITestListener .
4
ITestListener
5
Test Scenario:
In this test scenario, we will automate Login process and implement
the 'ItestListener'.
6
Steps to create a TestNG Listener
7
Steps to create a TestNG Listener
Just click on "Add unimplemented methods". Multiple
unimplemented methods (without a body) is added to the code.
Check below-
8
Steps to create a TestNG Listener
9
Steps to create a TestNG Listener
Step 2) Create another class "TestCases" for the login process automation.
Selenium will execute this 'TestCases' to login automatically.
10
Steps to create a TestNG Listener
Step 3) Next, implement this listener in our regular project class i.e. "TestCases".
There are two different ways to connect to the class and interface.
The first way is to use Listeners annotation (@Listeners) as shown below:
11
Steps to create a TestNG Listener
So Finally the class " TestCases " looks like after using Listener annotation
12
Steps to create a TestNG Listener
The project structure looks like:
13
Steps to create a TestNG Listener
Step 4): Execute the "TestCases " class. Methods in class "ListenerTest
" are called automatically according to the behavior of methods
annotated asthe
Step 5): Verify @Test.
Output that logs displays at the console.
14
Steps to create a TestNG Listener
Use of Listener for multiple classes.
If project has multiple classes adding Listeners to each one of them could be
cumbersome and error prone.
In such cases, we can create a testng.xml and add listeners tag in XML.
This listener is implemented throughout the test suite irrespective of the number of
classes you have. When you run this XML file, listeners will work on all classes mentioned.
You can also declare any number of listener class.
15
RBVH/ENG1
Conclusion
Listeners are required to generate logs or customize TestNG reports
in Selenium Webdriver.
There are many types of listeners and can be used as per
requirements.
Listeners are interfaces used in selenium web driver script
Demonstrated the use of Listener in Selenium
Implemented the Listeners for multiple classes
16
RBVH/ENG1
Reference
https://www.guru99.com/listeners-selenium-webdriver.html
17