TakesScreenshot and TestNG Listener

How to Capture Screenshots on Test Failures, attach to Reports

UI Test Automation Framewok Interview Question Series

When automated UI tests fail, a screenshot can provide invaluable context for troubleshooting. Instead of relying solely on stack traces or log files, a captured image shows the exact state of the application at the moment of failure, making it much easier to identify the root cause. Here we answer the beloved interview question: “How to Capture Screenshots on Test Failures, attach to Reports?

In Selenium WebDriver with Java, you can automate screenshot capture using the TakesScreenshot interface and integrate it with a TestNG Listener. This approach ensures that every failed test automatically generates a screenshot and attaches it to your test report, such as Extent Reports, Allure Reports

Create a Screenshot Utility

The first step is to create a reusable utility method that captures a screenshot and returns the file path.

Java
public class ScreenshotUtil {
public static String captureScreenshot(WebDriver driver, String testName) {
TakesScreenshot ts = (TakesScreenshot) driver;
File src = ts.getScreenshotAs(OutputType.FILE);
String path = "./Screenshots/" + testName + "_" +
System.currentTimeMillis() + ".png";
FileHandler.copy(src, new File(path));
return path;
}
}

This utility:

  • Uses Selenium’s TakesScreenshot interface.
  • Saves the image with a unique timestamp.
  • Returns the screenshot path for reporting purposes.

Capture Screenshots Using a TestNG Listener

Rather than adding screenshot logic to every test case, implement the ITestListener interface and use the onTestFailure() callback.

Java
public class ListenerTest implements ITestListener {
@Override
public void onTestFailure(ITestResult result) {
WebDriver driver = DriverManager.getDriver();
String screenshotPath = ScreenshotUtil.captureScreenshot(driver, result.getName());
// Attach screenshot to Extent Reports
ExtentReportTestsTracker.logFail("Test Failed");
ExtentReportTestsTracker.getCurrentTest()
.addScreenCaptureFromPath(screenshotPath);
}
}

Whenever a test fails, the listener automatically:

  • Retrieves the current WebDriver instance.
  • Captures a screenshot.
  • Saves it to the designated folder.
  • Attaches it to the Extent Report.

Best Practices

To get the most from your screenshot implementation:

  • Capture screenshots only when a test fails to avoid unnecessary storage usage.
  • Include timestamps in filenames to prevent overwriting previous screenshots.
  • Store screenshots in a dedicated directory within your project or CI workspace.
  • Integrate screenshots with reporting frameworks like Extent Reports or Allure Reports.
  • Consider uploading screenshots as CI artifacts for easy access after pipeline execution.

Final Thoughts

Capturing screenshots on test failures is a simple enhancement that significantly improves the effectiveness of Selenium test automation. By combining a reusable screenshot utility with a TestNG listener, you can automatically collect visual evidence for every failed test and include it directly in your reports.

Related Articles

TestNG Guide Page

Selenium Framework Design

Interview Preparation Page

SUBSCRIBE to Rotebit for more such quick interview questions on Test Automation!


Discover more from Rotebit

Subscribe to get the latest posts sent to your email.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply