0
0
Selenium Javatesting~5 mins

Screenshot attachment on failure in Selenium Java

Choose your learning style9 modes available
Introduction

Taking a screenshot when a test fails helps you see what went wrong. It shows the exact screen state at failure.

When a test case does not pass and you want to understand the problem visually.
When debugging UI issues that are hard to catch by just reading error messages.
When sharing test results with teammates or developers to show the failure context.
When running automated tests on remote machines or CI servers where you cannot watch the test run.
When you want to keep a record of failures for future reference.
Syntax
Selenium Java
try {
    // test steps
} catch (Exception e) {
    File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(screenshot, new File("path/to/save/screenshot.png"));
    throw e; // rethrow to mark test as failed
}

Use TakesScreenshot interface from Selenium to capture the screen.

Save the screenshot file to a known folder for easy access.

Examples
Capture and save a screenshot named failure.png in the screenshots folder.
Selenium Java
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshot, new File("screenshots/failure.png"));
Save screenshots with a timestamp to avoid overwriting files.
Selenium Java
try {
    // some test code
} catch (Exception e) {
    File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(screenshot, new File("screenshots/error_" + System.currentTimeMillis() + ".png"));
    throw e;
}
Sample Program

This test opens a website, tries to find a missing element to cause failure, then takes a screenshot and saves it. It prints messages about the screenshot and failure.

Selenium Java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.apache.commons.io.FileUtils;
import java.io.File;

public class ScreenshotOnFailureTest {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        try {
            driver.get("https://example.com");
            // Intentionally fail: find element that does not exist
            driver.findElement(By.id("nonexistent-element"));
        } catch (Exception e) {
            try {
                File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
                FileUtils.copyFile(screenshot, new File("screenshots/failure.png"));
                System.out.println("Screenshot saved on failure.");
            } catch (Exception ioException) {
                System.out.println("Failed to save screenshot: " + ioException.getMessage());
            }
            System.out.println("Test failed: " + e.getMessage());
        } finally {
            driver.quit();
        }
    }
}
OutputSuccess
Important Notes

Make sure the screenshots folder exists or create it before saving files.

Use meaningful file names or timestamps to avoid overwriting screenshots.

Integrate screenshot capture in test frameworks like JUnit or TestNG listeners for automatic capture on failure.

Summary

Taking screenshots on failure helps you see what went wrong visually.

Use Selenium's TakesScreenshot interface to capture and save images.

Save screenshots with clear names and handle exceptions during saving.