Selenium WebDriver - TestNG @Listener Annotations

4:18 AM 4 Comments

@Listeners Annotations in TestNG


A simple ITestListener adapter that stores all the tests that were run. You can retrieve these results with the following methods: getPassedTests() getFailedTests() getSkippedTests().
In addition to that we can reuse the existing methods like onTestSuccess(ITestResult tr),onTestSkipped(ITestResult tr) and onTestFailure(ITestResult tr) of org.testng.TestListenerAdapter Class.
If you extend this class in order to override any of these methods, remember to call their super equivalent if you want this list of tests to be maintained.

Implementation of TestListener Adapter Class

Steps to use TestListener Adapter class

1. First extend the TestListenerAdapterClass.
Example:
public class useListeners extends TestListenerAdapter
2. After that over the methods of onTestSuccess(ITestResult tr),onTestSkipped(ITestResult tr) and onTestFailure(ITestResult tr) in the useListeners class.
3. Use @Listener annotation in the Test Class which contains @Test Annotations.
4. Execute the Test Class using testng.xml or run an Testng Test.
5. For every @Test methods available in the Test Class based on the status of pass/fail/skipped the Listener methods are called.

4 comments: