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 web page when the test failed. This makes it easier to understand what went wrong and debug the issue.
Click to reveal answer
beginner
Which Selenium WebDriver method is used to capture a screenshot in Python?
The method driver.save_screenshot('filename.png') is used to capture and save a screenshot of the current browser window.
Click to reveal answer
intermediate
How can you automatically take a screenshot only when a test fails in Python unittest?
You can override the tearDown method to check if the test failed, and if so, call driver.save_screenshot() to save the screenshot.
Click to reveal answer
beginner
Why is it important to name screenshot files with timestamps or test names?
Naming screenshots with timestamps or test names prevents overwriting files and helps quickly identify which test and time the screenshot belongs to.
Click to reveal answer
intermediate
What is a simple way to integrate screenshot capture on failure in pytest?
In pytest, you can use the request.node.rep_call.failed hook in a fixture to detect failure and then call driver.save_screenshot() to save the screenshot.
Click to reveal answer
Which Selenium WebDriver method saves a screenshot in Python?
Aget_screenshot()
Btake_screenshot()
Ccapture_screen()
Dsave_screenshot()
✗ Incorrect
The correct method is save_screenshot() which saves the current browser window as an image file.
When should you take a screenshot during automated testing?
AOnly when the test fails
BBefore starting the test
COnly when the test passes
DAfter every test step
✗ Incorrect
Taking screenshots on failure helps capture the error state for debugging.
In unittest, which method is best to override to add screenshot capture on failure?
AtearDown()
BsetUp()
CrunTest()
DstartTest()
✗ Incorrect
The tearDown() method runs after each test and is ideal to check for failure and capture screenshots.
Why add timestamps to screenshot filenames?
ATo make files smaller
BTo prevent overwriting and identify when the screenshot was taken
CTo encrypt the file
DTo improve screenshot quality
✗ Incorrect
Timestamps help avoid overwriting files and make it easier to track when the screenshot was taken.
Which Python testing framework allows using fixtures to capture screenshots on failure?
Aunittest
Bnose
Cpytest
Ddoctest
✗ Incorrect
pytest supports fixtures that can detect test failure and capture screenshots automatically.
Explain how to implement automatic screenshot capture on test failure using Selenium with Python unittest.
Think about what happens after each test and how to detect failure.
You got /4 concepts.
Describe best practices for naming and storing screenshots taken on test failure.
Consider how to keep screenshots easy to find and identify.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of taking a screenshot on test failure in Selenium?
easy
A. To capture the current state of the web page for debugging
B. To speed up the test execution
C. To improve the browser's performance
D. To automatically fix the test errors
Solution
Step 1: Understand the role of screenshots in testing
Screenshots help testers see what the user saw when the test failed.
Step 2: Identify the purpose of capturing screenshots on failure
They provide visual evidence to debug UI problems quickly.
Final Answer:
To capture the current state of the web page for debugging -> Option A
Quick Check:
Screenshot on failure = capture page state [OK]
Hint: Screenshots show what failed looks like visually [OK]
Common Mistakes:
Thinking screenshots speed up tests
Believing screenshots fix errors automatically
Confusing screenshots with performance tools
2. Which of the following is the correct syntax to take a screenshot in Selenium Python when a test fails?
easy
A. driver.save_screenshot('error.png')
B. driver.takeScreenshot('error.png')
C. driver.capture('error.png')
D. driver.screenshotSave('error.png')
Solution
Step 1: Recall Selenium Python screenshot method
The correct method to save a screenshot is save_screenshot().
Step 2: Verify method names and syntax
Only driver.save_screenshot('error.png') matches Selenium's API.
Final Answer:
driver.save_screenshot('error.png') -> Option A
Quick Check:
Correct method name = save_screenshot [OK]
Hint: Remember method is save_screenshot with underscore [OK]
Common Mistakes:
Using camelCase instead of snake_case
Using non-existent methods like capture or screenshotSave
Missing parentheses or quotes
3. What will be the output if the following Selenium Python code runs and the element is not found?
try:
driver.find_element(By.ID, 'submit').click()
except Exception as e:
driver.save_screenshot('fail.png')
print('Screenshot saved on failure')
medium
A. No output, test passes silently
B. RuntimeError without screenshot
C. SyntaxError due to missing import
D. Screenshot saved on failure
Solution
Step 1: Analyze try-except behavior on element not found
If element is missing, exception triggers except block.
Step 2: Check actions in except block
Screenshot is saved and message printed: 'Screenshot saved on failure'.
C. Missing quotes around filename in save_screenshot
D. Wrong method name saveScreenshot instead of save_screenshot
Solution
Step 1: Check save_screenshot argument syntax
Filename must be a string in quotes, but here it is unquoted.
Step 2: Confirm exception handling and method name
Except syntax and method name are correct; only filename is wrong.
Final Answer:
Missing quotes around filename in save_screenshot -> Option C
Quick Check:
Filename must be a string in quotes [OK]
Hint: Always put filename in quotes for save_screenshot [OK]
Common Mistakes:
Forgetting quotes around filename string
Changing method name incorrectly
Misusing except syntax
5. You want to automatically take a screenshot only when a Selenium test fails and save it with a timestamp. Which approach below correctly implements this in Python?
hard
A. Use driver.save_screenshot('fail.png') outside try-except block
B. Use try-except around test steps, call driver.save_screenshot(f"fail_{time.time()}.png") in except
C. Call driver.save_screenshot() before every test step unconditionally
D. Use a loop to retry test steps and save screenshot on success
Solution
Step 1: Understand when to take screenshots
Screenshots should be taken only on failure, inside except block.
Step 2: Use timestamp for unique filenames
Using time.time() in filename ensures unique screenshot names.
Final Answer:
Use try-except around test steps, call driver.save_screenshot(f"fail_{time.time()}.png") in except -> Option B
Quick Check:
Screenshot on failure with timestamp = try-except + formatted filename [OK]
Hint: Take screenshots only in except block with timestamped names [OK]
Common Mistakes:
Taking screenshots before test steps always
Saving screenshots outside exception handling
Retrying tests without capturing failure screenshots
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.