TestNG Listeners in Selenium Java – Complete Guide
By Bhau Automation • Master TestNG Listeners & Screenshot Capture
🎯 What You Will Learn in This Video
- What are TestNG Listeners and how they work
- Different types of listeners in TestNG (ITestListener, ISuiteListener, etc.)
- How to capture screenshots on test failure using listeners
- Practical implementation in Selenium WebDriver
- Best practices and interview questions for TestNG listeners
💡 Pro Tip: Using listeners can greatly improve your test reporting and debugging experience in Selenium automation.
📌 What is a TestNG Listener?
TestNG Listeners are interfaces that allow you to modify the default behavior of TestNG tests. By implementing listeners, you can listen to test events such as start, success, failure, skip, and take custom actions.
Common listener types include:
- ITestListener: Listen to individual test case events
- ISuiteListener: Listen to suite-level events
- IInvokedMethodListener: Listen before and after method invocation
🔧 How to Implement TestNG Listeners in Selenium
Example of using ITestListener to capture screenshots on test failure:
import org.testng.ITestListener;
import org.testng.ITestResult;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import java.io.File;
import org.apache.commons.io.FileUtils;
public class ListenerExample implements ITestListener {
private WebDriver driver;
@Override
public void onTestFailure(ITestResult result) {
System.out.println("Test Failed: " + result.getName());
try {
File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(src, new File("./screenshots/" + result.getName() + ".png"));
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onTestSuccess(ITestResult result) {
System.out.println("Test Passed: " + result.getName());
}
@Override
public void onTestStart(ITestResult result) {
System.out.println("Test Started: " + result.getName());
}
@Override
public void onTestSkipped(ITestResult result) {
System.out.println("Test Skipped: " + result.getName());
}
}
🎯 Real-World Use Cases
- Capture screenshots automatically on test failures
- Generate custom HTML or Extent reports
- Send notifications or emails on test events
- Log test execution details for debugging
❓ Common Interview Questions
Q: What are TestNG listeners?
A: Interfaces that allow you to listen and respond to events during test execution.
Q: How do you capture screenshots on failure in TestNG?
A: Implement ITestListener and override the onTestFailure method to take screenshots.
🎥 Watch the Complete Video Tutorial
👉 Watch on YouTube: TestNG Listeners in Selenium Java
🎓 Key Takeaways
- Listeners allow you to track test execution events
- ITestListener is commonly used for screenshots and logging
- ISuiteListener helps manage suite-level events
- Proper use of listeners improves reporting and debugging
- Listeners are essential for scalable and maintainable test frameworks
⚡ Next Steps: Implement listeners in your Selenium project and observe automatic screenshot capture and custom logging for each test case.
🚀 Created with ❤️ by Bhau Automation