What if you could see exactly what broke in your test without guessing?
Why Screenshot attachment on failure in Selenium Java? - Purpose & Use Cases
Imagine you run a test on a website manually and it fails. You try to remember what went wrong, but you have no picture or record of the error. You have to repeat the steps again and again to catch the problem.
Manually checking errors is slow and easy to forget details. Without a screenshot, you rely on memory or notes, which can miss important clues. This makes fixing bugs harder and wastes time.
Automatically taking a screenshot when a test fails captures the exact moment of error. This visual proof helps quickly understand what went wrong without repeating steps. It saves time and reduces frustration.
if(testFails) { System.out.println("Test failed, no screenshot available"); }
if(testFails) { takeScreenshot("failure.png"); attachScreenshotToReport("failure.png"); }
It enables fast, clear debugging by showing exactly what the user saw when the test failed.
When testing a login page, if the password field breaks, a screenshot shows the error message and page state instantly, helping developers fix it faster.
Manual error checks miss details and waste time.
Automatic screenshots capture exact failure moments.
This speeds up debugging and improves test reports.
Practice
Solution
Step 1: Understand the role of screenshots in testing
Screenshots help testers see what the application looked like when a test failed.Step 2: Identify the benefit of visual evidence
This visual evidence helps diagnose issues faster and more clearly than logs alone.Final Answer:
To visually capture the state of the application at failure -> Option CQuick Check:
Screenshot = Visual failure evidence [OK]
- Thinking screenshots fix errors automatically
- Believing screenshots speed up tests
- Assuming screenshots reduce report size
Solution
Step 1: Recall Selenium interfaces for screenshots
TheTakesScreenshotinterface provides the method to capture screenshots.Step 2: Differentiate from other interfaces
Other interfaces likeWebDrivercontrol browser,Alerthandles popups, andJavascriptExecutorruns JS scripts.Final Answer:
TakesScreenshot -> Option DQuick Check:
TakesScreenshot = Capture screenshots [OK]
- Using WebDriver directly for screenshots
- Confusing Alert or JavascriptExecutor with screenshot capture
- Not casting driver to TakesScreenshot
File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(srcFile, new File("screenshots/fail.png"));Solution
Step 1: Analyze the screenshot capture code
The driver is cast toTakesScreenshotand screenshot saved as a file named "fail.png" in the "screenshots" folder.Step 2: Check for errors or randomness
FileUtils.copyFile copies the screenshot correctly; file name is fixed as "fail.png" so no randomness.Final Answer:
Screenshot saved as fail.png in screenshots folder -> Option BQuick Check:
Code saves fixed-name screenshot file [OK]
- Forgetting to cast driver to TakesScreenshot
- Not importing or defining FileUtils
- Expecting random file names without code for it
File srcFile = driver.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(srcFile, new File("screenshots/error.png"));Solution
Step 1: Check method call on driver
ThegetScreenshotAsmethod belongs toTakesScreenshot, so driver must be cast before calling it.Step 2: Verify other parts
FileUtils.copyFile exists and OutputType.FILE is correct. Relative paths are allowed if folder exists.Final Answer:
driver must be cast to TakesScreenshot before calling getScreenshotAs -> Option AQuick Check:
Cast driver to TakesScreenshot before screenshot [OK]
- Calling getScreenshotAs directly on WebDriver
- Assuming OutputType.FILE is wrong
- Thinking relative paths cause errors
@AfterMethod
public void tearDown(ITestResult result) {
if (result.getStatus() == ITestResult.FAILURE) {
File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
File dest = new File("screenshots/" + result.getName() + ".png");
FileUtils.copyFile(src, dest);
// Attach dest to report here
}
driver.quit();
}Solution
Step 1: Check condition for failure
The code checks if test status is FAILURE before taking screenshot, so it runs only on failures.Step 2: Verify screenshot saving and naming
Screenshot saved with test method name, which is valid. Attaching to report is indicated as a comment.Step 3: Confirm no errors in logic
FileUtils.copyFile may throw IOException but typically handled or declared; this snippet focuses on logic correctness.Final Answer:
This code saves screenshot only on failure and attaches it correctly -> Option AQuick Check:
Screenshot on failure only with correct naming [OK]
- Taking screenshots regardless of test result
- Not handling exceptions from file operations
- Assuming getName() returns null
Ready to prove it?
Pass the test and this pattern moves unknown
