0
0
Selenium Pythontesting~20 mins

Screenshot on failure in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Screenshot Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output when the test fails and screenshot is taken?
Consider the following Selenium Python test snippet that tries to find a non-existing element and takes a screenshot on failure. What will be the output or result of running this code?
Selenium Python
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException

try:
    driver = webdriver.Chrome()
    driver.get('https://example.com')
    driver.find_element('id', 'nonexistent')
except NoSuchElementException:
    driver.save_screenshot('failure.png')
    print('Screenshot saved on failure')
finally:
    driver.quit()
ARaises TimeoutException
BScreenshot saved on failure
CNo output, test passes silently
DSyntaxError due to wrong method call
Attempts:
2 left
💡 Hint
Look at the exception caught and what happens inside the except block.
assertion
intermediate
1:30remaining
Which assertion correctly verifies screenshot file creation after failure?
After a test failure and screenshot capture, which assertion correctly checks that the screenshot file 'failure.png' exists?
Selenium Python
import os

# Assume screenshot is saved as 'failure.png' in current directory

# Which assertion below is correct?
Aassert os.path.isfile('failure.png')
Bassert os.path.exists('failure.png') == False
Cassert os.path.isdir('failure.png')
Dassert os.path.isfile('failure.png') == False
Attempts:
2 left
💡 Hint
Check which function verifies a file exists.
🔧 Debug
advanced
2:30remaining
Why does this screenshot code fail to save the image?
This Selenium Python code is supposed to save a screenshot on failure but does not create any file. What is the likely cause?
Selenium Python
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException

try:
    driver = webdriver.Chrome()
    driver.get('https://example.com')
    driver.find_element('id', 'nonexistent')
except NoSuchElementException:
    driver.save_screenshot()
    print('Screenshot saved')
finally:
    driver.quit()
Asave_screenshot requires a filename argument, missing here
BNoSuchElementException is not caught properly
Cdriver.quit() is called before save_screenshot
Ddriver.get() URL is invalid
Attempts:
2 left
💡 Hint
Check the method call for saving screenshots.
framework
advanced
3:00remaining
How to integrate screenshot on failure in pytest with Selenium?
Which pytest fixture code snippet correctly captures a screenshot on test failure using Selenium WebDriver?
A
import pytest

def pytest_runtest_setup(item):
    driver.save_screenshot('failure.png')
B
import pytest

@pytest.fixture(autouse=True)
def screenshot_on_failure(request, driver):
    yield
    if request.node.rep_call.failed:
        driver.save_screenshot('failure.png')
C
import pytest

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    rep = outcome.get_result()
    if rep.when == 'call' and rep.failed:
        driver = item.funcargs['driver']
        driver.save_screenshot('failure.png')
D
import pytest

@pytest.fixture
def driver():
    driver = webdriver.Chrome()
    yield driver
    driver.save_screenshot('failure.png')
    driver.quit()
Attempts:
2 left
💡 Hint
Look for pytest hook that runs after test call and can access test result.
🧠 Conceptual
expert
1:30remaining
Why is taking screenshots on failure important in automated UI testing?
Select the best reason why automated UI tests should capture screenshots when they fail.
AScreenshots replace the need for logs and error messages
BScreenshots reduce test execution time significantly
CScreenshots prevent tests from failing by fixing bugs automatically
DScreenshots provide visual evidence of the UI state at failure, aiding debugging
Attempts:
2 left
💡 Hint
Think about how visual context helps when tests fail.