0
0
Selenium Pythontesting~10 mins

Screenshot on failure in Selenium Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to take a screenshot when the test fails.

Selenium Python
try:
    assert driver.title == "Home Page"
except AssertionError:
    driver.[1]("failure.png")
Drag options to blanks, or click blank then click option'
Acapture_screenshot
Btake_screenshot
Cget_screenshot_as_file
Dsave_screenshot
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that does not exist in Selenium WebDriver.
Forgetting to call the method on the driver object.
2fill in blank
medium

Complete the code to catch any exception and save a screenshot.

Selenium Python
try:
    element = driver.find_element(By.ID, "submit")
    element.click()
except [1]:
    driver.get_screenshot_as_file("error.png")
Drag options to blanks, or click blank then click option'
AException
BValueError
CKeyError
DTimeoutError
Attempts:
3 left
💡 Hint
Common Mistakes
Catching only specific exceptions and missing others.
Using exception classes unrelated to Selenium errors.
3fill in blank
hard

Fix the error in the code to correctly save a screenshot on failure.

Selenium Python
try:
    assert "Welcome" in driver.page_source
except AssertionError:
    driver.[1]("fail.png")
Drag options to blanks, or click blank then click option'
AsaveScreenshot
BcaptureScreenshot
Cscreenshot_save
Dget_screenshot_as_file
Attempts:
3 left
💡 Hint
Common Mistakes
Using camelCase method names which do not exist.
Misspelling the method name.
4fill in blank
hard

Fill both blanks to save a screenshot with a dynamic filename including the test name.

Selenium Python
def test_login(driver):
    try:
        assert driver.current_url == "https://example.com/dashboard"
    except AssertionError:
        filename = f"[1]_failure.png"
        driver.[2](filename)
Drag options to blanks, or click blank then click option'
Atest_login
Bsave_screenshot
Cget_screenshot_as_file
Dfailure
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect method names for saving screenshots.
Not using the test name in the filename.
5fill in blank
hard

Fill all three blanks to implement a pytest fixture that takes a screenshot on test failure.

Selenium Python
import pytest

@pytest.fixture(autouse=True)
def screenshot_on_failure(request, driver):
    yield
    if request.node.rep_call.[1]:
        driver.[2](f"[3].png")
Drag options to blanks, or click blank then click option'
Afailed
Bget_screenshot_as_file
Crequest.node.name
Dpassed
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for 'passed' instead of 'failed'.
Using wrong method names for screenshot saving.
Not using the test name for the screenshot filename.