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.
0
0
Listeners and reporting in Selenium Java
Introduction
You want to know immediately when a test fails during automation.
You need to create a detailed report of all test results after running tests.
You want to take a screenshot automatically when a test fails.
You want to log extra information during test execution for debugging.
You want to perform actions before or after each test method runs.
Syntax
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.
Examples
This listener only prints a message when a test fails.
Selenium Java
public class SimpleListener implements ITestListener { @Override public void onTestFailure(ITestResult result) { System.out.println("Failed test: " + result.getName()); } }
This listener can be used to take screenshots automatically on failure.
Selenium Java
public class ScreenshotListener implements ITestListener { @Override public void onTestFailure(ITestResult result) { System.out.println("Test failed: " + result.getName()); // Code to capture screenshot here } }
This listener reports all test events: start, success, failure, and skipped.
Selenium Java
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()); } }
Sample Program
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.
Selenium Java
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()); } } }
OutputSuccess
Important Notes
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.
Summary
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.