What is the purpose of taking a screenshot on test failure in Selenium?
Taking a screenshot on test failure helps to capture the exact state of the application when the test failed. It makes debugging easier by showing what was visible on the screen at the failure moment.
Click to reveal answer
beginner
Which Selenium interface is used to capture screenshots in Java?
The TakesScreenshot interface is used in Selenium Java to capture screenshots. It provides the method getScreenshotAs() to get the screenshot as a file or byte array.
Click to reveal answer
intermediate
How do you attach a screenshot to a test report after a failure?
After capturing the screenshot file, you save it to a known location and then link or embed it in the test report. Many test frameworks support attaching files or images to reports for easy viewing.
Click to reveal answer
intermediate
Show a simple Java code snippet to capture a screenshot on test failure using Selenium.
Why is it important to use descriptive file names or timestamps when saving screenshots on failure?
Using descriptive file names or timestamps prevents overwriting screenshots from multiple failures and helps quickly identify which test and when the failure happened.
Click to reveal answer
Which Selenium interface allows you to take screenshots in Java?
ATakesScreenshot
BWebElement
CJavascriptExecutor
DActions
✗ Incorrect
TakesScreenshot interface provides the getScreenshotAs() method to capture screenshots.
What is the best time to capture a screenshot during automated testing?
ARandomly during test
BAfter test success
CBefore starting the test
DOn test failure
✗ Incorrect
Capturing screenshots on test failure helps diagnose what went wrong.
Which output type is commonly used to save screenshots as files in Selenium Java?
AOutputType.FILE
BOutputType.STRING
COutputType.BYTES
DOutputType.JSON
✗ Incorrect
OutputType.FILE returns the screenshot as a file which can be saved to disk.
Why should screenshots have unique names when saved?
ATo improve screenshot quality
BTo reduce file size
CTo avoid overwriting previous screenshots
DTo make screenshots invisible
✗ Incorrect
Unique names prevent overwriting and help identify screenshots from different failures.
Which Java class is commonly used to copy screenshot files to a destination folder?
AFileReader
BFiles
CScanner
DBufferedWriter
✗ Incorrect
The java.nio.file.Files class provides methods like copy() to move files.
Explain how to capture and save a screenshot on test failure using Selenium in Java.
Think about catching exceptions and saving the screenshot file.
You got /4 concepts.
Why is attaching screenshots to test reports helpful for debugging?
Imagine you are a developer trying to fix a bug.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of taking a screenshot on test failure in Selenium Java?
easy
A. To automatically fix the test errors
B. To speed up the test execution
C. To visually capture the state of the application at failure
D. To reduce the size of the test report
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 C
Quick Check:
Screenshot = Visual failure evidence [OK]
Hint: Screenshots show what failed visually, not fix or speed tests [OK]
Common Mistakes:
Thinking screenshots fix errors automatically
Believing screenshots speed up tests
Assuming screenshots reduce report size
2. Which Selenium interface is used to capture screenshots in Java?
easy
A. WebDriver
B. JavascriptExecutor
C. Alert
D. TakesScreenshot
Solution
Step 1: Recall Selenium interfaces for screenshots
The TakesScreenshot interface provides the method to capture screenshots.
Step 2: Differentiate from other interfaces
Other interfaces like WebDriver control browser, Alert handles popups, and JavascriptExecutor runs JS scripts.
Final Answer:
TakesScreenshot -> Option D
Quick Check:
TakesScreenshot = Capture screenshots [OK]
Hint: Use TakesScreenshot interface to capture images [OK]
Common Mistakes:
Using WebDriver directly for screenshots
Confusing Alert or JavascriptExecutor with screenshot capture
Not casting driver to TakesScreenshot
3. What will happen if you run this code snippet after a test failure?
File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(srcFile, new File("screenshots/fail.png"));
medium
A. Compilation error due to missing cast
B. Screenshot saved as fail.png in screenshots folder
C. Runtime error because FileUtils is undefined
D. Screenshot saved but file name is random
Solution
Step 1: Analyze the screenshot capture code
The driver is cast to TakesScreenshot and 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 B
Quick Check:
Code saves fixed-name screenshot file [OK]
Hint: Casting driver and copyFile saves fixed screenshot file [OK]
Common Mistakes:
Forgetting to cast driver to TakesScreenshot
Not importing or defining FileUtils
Expecting random file names without code for it
4. Identify the error in this code snippet for saving a screenshot on failure:
File srcFile = driver.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(srcFile, new File("screenshots/error.png"));
medium
A. driver must be cast to TakesScreenshot before calling getScreenshotAs
B. FileUtils.copyFile method does not exist
C. OutputType.FILE is invalid for screenshots
D. File path must be absolute, not relative
Solution
Step 1: Check method call on driver
The getScreenshotAs method belongs to TakesScreenshot, 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 A
Quick Check:
Cast driver to TakesScreenshot before screenshot [OK]
Hint: Always cast driver to TakesScreenshot before getScreenshotAs [OK]
Common Mistakes:
Calling getScreenshotAs directly on WebDriver
Assuming OutputType.FILE is wrong
Thinking relative paths cause errors
5. You want to attach a screenshot to your test report only when a test fails. Which approach correctly implements this in Selenium Java with TestNG?
@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();
}
hard
A. This code saves screenshot only on failure and attaches it correctly
B. Screenshot is saved on every test, not just failures
C. FileUtils.copyFile will throw error without try-catch
D. result.getName() returns null, so file name is invalid
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 A
Quick Check:
Screenshot on failure only with correct naming [OK]
Hint: Use ITestResult.FAILURE check to save screenshots only on failure [OK]
Common Mistakes:
Taking screenshots regardless of test result
Not handling exceptions from file operations
Assuming getName() returns null
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.