0% found this document useful (0 votes)
14 views17 pages

07 - 02 - TestNG Listeners in Selenium

Uploaded by

minhtandragon29
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views17 pages

07 - 02 - TestNG Listeners in Selenium

Uploaded by

minhtandragon29
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

TestNG Listeners in

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?

 Listener is defined as interface that modifies the default TestNG's


behavior. As the name suggests Listeners "listen" to the event defined
in the selenium script and behave accordingly. It is used in selenium by
implementing Listeners Interface. It allows customizing TestNG reports
or logs. There are many types of TestNG listeners available.

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

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.

5
Test Scenario:
In this test scenario, we will automate Login process and implement
the 'ItestListener'.

1 .Launch the Firefox and open the site "http://demo.guru99.com/V4/ “


2. Login to the application.

6
Steps to create a TestNG Listener

Step 1) Create class "ListenerTest" that implements 'ITestListener'. Move the


mouse over redline text, and Eclipse will suggest you 2 quick fixes as shown in
below screen:

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

You might also like