Listeners help watch what happens during tests and report results automatically. They make it easy to see which tests passed or failed without checking manually.
Listeners and reporting in Selenium Java
public class TestListener implements ITestListener { @Override public void onTestStart(ITestResult result) { System.out.println("Test started: " + result.getName()); } @Override public void onTestSuccess(ITestResult result) { System.out.println("Test passed: " + result.getName()); } @Override public void onTestFailure(ITestResult result) { System.out.println("Test failed: " + result.getName()); // Add code to take screenshot or log error } @Override public void onTestSkipped(ITestResult result) { System.out.println("Test skipped: " + result.getName()); } // Other methods can be overridden as needed }
Listeners implement interfaces like ITestListener from TestNG.
Override only the methods you need for your reporting or actions.
public class SimpleListener implements ITestListener { @Override public void onTestFailure(ITestResult result) { System.out.println("Failed test: " + result.getName()); } }
public class ScreenshotListener implements ITestListener { @Override public void onTestFailure(ITestResult result) { System.out.println("Test failed: " + result.getName()); // Code to capture screenshot here } }
public class FullListener implements ITestListener { @Override public void onTestStart(ITestResult result) { System.out.println("Starting: " + result.getName()); } @Override public void onTestSuccess(ITestResult result) { System.out.println("Passed: " + result.getName()); } @Override public void onTestFailure(ITestResult result) { System.out.println("Failed: " + result.getName()); } @Override public void onTestSkipped(ITestResult result) { System.out.println("Skipped: " + result.getName()); } }
This example shows a TestNG test class with two tests: one passes and one fails. The TestListener prints messages when tests start, pass, or fail.
import org.testng.ITestListener; import org.testng.ITestResult; import org.testng.annotations.Listeners; import org.testng.annotations.Test; import org.testng.Assert; @Listeners(ListenersExample.TestListener.class) public class ListenersExample { public static class SampleTests { @Test public void testPass() { Assert.assertTrue(true); } @Test public void testFail() { Assert.fail("Failing test"); } } public static class TestListener implements ITestListener { @Override public void onTestStart(ITestResult result) { System.out.println("Test started: " + result.getName()); } @Override public void onTestSuccess(ITestResult result) { System.out.println("Test passed: " + result.getName()); } @Override public void onTestFailure(ITestResult result) { System.out.println("Test failed: " + result.getName()); } @Override public void onTestSkipped(ITestResult result) { System.out.println("Test skipped: " + result.getName()); } } }
Listeners run automatically when registered with @Listeners annotation or in testng.xml.
Using listeners helps avoid repeating reporting code in every test.
Common mistake: forgetting to register the listener, so it does not run.
Listeners watch test events and help create reports automatically.
They can run code on test start, success, failure, or skip.
Register listeners to your tests to get automatic reporting and actions.
Practice
Solution
Step 1: Understand what listeners do in Selenium
Listeners watch test events such as start, success, failure, or skip.Step 2: Identify the main purpose of listeners
They trigger actions automatically, like creating reports or logging results.Final Answer:
To monitor test events and trigger actions like reporting -> Option CQuick Check:
Listeners = Monitor events and report [OK]
- Confusing listeners with element locators
- Thinking listeners execute browser scripts
- Assuming listeners manage cookies
Solution
Step 1: Identify the listener interface for TestNG
TestNG uses the ITestListener interface to listen to test events like success or failure.Step 2: Confirm the interface for test success events
ITestListener has methods like onTestSuccess to handle success events.Final Answer:
ITestListener -> Option AQuick Check:
ITestListener = TestNG event listener [OK]
- Choosing unrelated interfaces like Runnable
- Confusing WebDriverListener with TestNG listener
- Selecting Serializable which is unrelated
public void onTestFailure(ITestResult result) {
System.out.println("Test failed: " + result.getName());
}What will be printed if a test named
loginTest fails?Solution
Step 1: Understand the onTestFailure method
This method prints "Test failed: " plus the test name from result.getName().Step 2: Identify the test name when loginTest fails
The test name is "loginTest", so the output is "Test failed: loginTest".Final Answer:
Test failed: loginTest -> Option DQuick Check:
Prints test failure with test name [OK]
- Assuming result.getName() returns null
- Confusing method name with event name
- Thinking no output is printed on failure
Solution
Step 1: Check listener registration
Listeners must be registered in TestNG XML or via @Listeners annotation to activate.Step 2: Understand why reports fail without registration
If not registered, listener methods never run, so no reports are generated.Final Answer:
Listener class is not registered in the test suite XML or annotation -> Option AQuick Check:
Unregistered listener = no reports [OK]
- Thinking listener must extend WebDriver
- Expecting main method in listener class
- Assuming private methods run automatically
Solution
Step 1: Identify when to capture screenshots
Screenshots are needed on test failure, so override onTestFailure method.Step 2: Attach screenshot to report in onTestFailure
Within onTestFailure, capture the screenshot and add it to the test report for visibility.Final Answer:
Override onTestFailure to capture screenshot and attach to report -> Option BQuick Check:
Screenshot on failure = onTestFailure override [OK]
- Capturing screenshots on test start or success
- Logging success in failure handler
- Clearing cookies unrelated to reporting
Ready to prove it?
Pass the test and this pattern moves unknown
