Saturday, October 27, 2018

How to implement IExecutionListener in TestNG?

IExecutionListener is an interface or TestNG Listener provided by TestNG. This listener monitors the beginning and ending of the TestNG execution.

IExecutionListener interface has two methods onExecutionStart() and onExecutionEnd(). onExecutionStart() will be invoked before TestNG execute any test suite and onExecutionEnd() will be invoked after TestNG executes all test suites.

Let us understand this with an example:

Step 1: Create a executionListener() java class and implement IExecutionListener to it as shown below.
Step 2: Create test class like executionListenerTestClass() and some test methods. Also add configuration methods which will be executed before and after test suite execution as shown below

Step 3: Create a testng.xml file (executeListener.xml here) and associate the listener class with test class by adding <Listener> tag as shown below.
Step 4: Execute executeListener.xml as TestNG suite and see the results in the below screenshot

From the above results it is clear that onExecutionStart() method is executed before any suite execution and onExecutionEnd() method is executed after test suite.

This listener can be used for opening and closing Database connection, deleting cookies and third party tool integrations in real time Selenium automation projects.

Please comment your queries or suggestions on this post. Thanks for reading. #HappyLearning #HappyTesting #QABABU

No comments:

Post a Comment

DevOps 01: What is DevOps and How it benefits organizations?

DevOps is a culture in an organization, where the development team and operations team help each other by sharing information, process and t...