Bird
0
0

In conftest.py, how do you create a Selenium WebDriver fixture that launches Chrome once before all tests run and closes it after all tests complete?

hard📝 framework Q8 of 15
Selenium Python - Test Framework Integration (pytest)
In conftest.py, how do you create a Selenium WebDriver fixture that launches Chrome once before all tests run and closes it after all tests complete?
A<pre>@pytest.fixture(scope='module') def browser(): driver = webdriver.Chrome() return driver driver.quit()</pre>
B<pre>@pytest.fixture(scope='function') def browser(): driver = webdriver.Chrome() yield driver driver.quit()</pre>
C<pre>@pytest.fixture(scope='session') def browser(): driver = webdriver.Chrome() yield driver driver.quit()</pre>
D<pre>@pytest.fixture(scope='class') def browser(): driver = webdriver.Chrome() yield driver driver.quit()</pre>
Step-by-Step Solution
Solution:
  1. Step 1: Use session scope

    Setting scope='session' ensures the fixture runs once per test session.
  2. Step 2: Use yield to manage setup and teardown

    Yield allows setup before tests and teardown after all tests finish.
  3. Step 3: Properly quit the driver after tests

    Calling driver.quit() after yield closes the browser cleanly.
  4. Final Answer:

    Fixture with session scope and yield -> Option C
  5. Quick Check:

    Session scope + yield for single browser instance [OK]
Quick Trick: Use session scope and yield for one browser per session [OK]
Common Mistakes:
  • Using function scope causing browser to open per test
  • Returning driver without yield prevents proper teardown
  • Using module or class scope opens browser multiple times

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Python Quizzes