Complete the code to take a screenshot when the test fails.
try: assert driver.title == "Home Page" except AssertionError: driver.[1]("failure.png")
The get_screenshot_as_file method saves a screenshot to a file.
Complete the code to catch any exception and save a screenshot.
try: element = driver.find_element(By.ID, "submit") element.click() except [1]: driver.get_screenshot_as_file("error.png")
Using Exception catches all exceptions, ensuring the screenshot is taken on any failure.
Fix the error in the code to correctly save a screenshot on failure.
try: assert "Welcome" in driver.page_source except AssertionError: driver.[1]("fail.png")
The correct method to save a screenshot file is get_screenshot_as_file.
Fill both blanks to save a screenshot with a dynamic filename including the test name.
def test_login(driver): try: assert driver.current_url == "https://example.com/dashboard" except AssertionError: filename = f"[1]_failure.png" driver.[2](filename)
The filename uses the test function name test_login. The method to save the screenshot is get_screenshot_as_file.
Fill all three blanks to implement a pytest fixture that takes a screenshot on test failure.
import pytest @pytest.fixture(autouse=True) def screenshot_on_failure(request, driver): yield if request.node.rep_call.[1]: driver.[2](f"[3].png")
The fixture checks if the test failed. It saves a screenshot using get_screenshot_as_file with the test name from request.node.name.