Bird
0
0

Given this fixture and test code, what will be the output when the test runs?

medium📝 Predict Output Q4 of 15
Selenium Python - Test Framework Integration (pytest)
Given this fixture and test code, what will be the output when the test runs?
import pytest
from selenium import webdriver

@pytest.fixture
def browser():
    driver = webdriver.Chrome()
    yield driver
    driver.quit()


def test_title(browser):
    browser.get('https://example.com')
    print(browser.title)
APrints the title of the page 'Example Domain' and closes the browser.
BRaises an error because driver.quit() is called before print.
CPrints nothing because print is outside the fixture.
DPrints the URL instead of the title.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze fixture and test interaction

    The fixture yields a WebDriver instance to the test, which uses it to open the page and print the title.
  2. Step 2: Understand when driver.quit() runs

    driver.quit() runs after the test finishes, so print(browser.title) executes successfully.
  3. Final Answer:

    Prints the title of the page 'Example Domain' and closes the browser. -> Option A
  4. Quick Check:

    Yield allows test to run before teardown [OK]
Quick Trick: Teardown runs after test, so print works [OK]
Common Mistakes:
  • Thinking driver.quit() runs before print
  • Assuming print outside fixture means no output
  • Confusing URL with page title

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Python Quizzes