Bird
Raised Fist0
Selenium Pythontesting~20 mins

Conftest for shared fixtures in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Conftest Fixture Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this pytest fixture usage?
Given the following conftest.py fixture and test code, what will be printed when running test_example?
Selenium Python
import pytest

@pytest.fixture(scope="module")
def browser():
    print("Setup browser")
    yield "ChromeDriver"
    print("Teardown browser")

def test_example(browser):
    print(f"Using {browser}")
    assert browser == "ChromeDriver"
ASetup browser\nUsing ChromeDriver\nTeardown browser
BSetup browser\nTeardown browser\nUsing ChromeDriver
CUsing ChromeDriver\nSetup browser\nTeardown browser
DUsing ChromeDriver only
Attempts:
2 left
💡 Hint
Remember that fixtures with yield run setup code before the test and teardown code after.
❓ assertion
intermediate
1:30remaining
Which assertion correctly verifies the fixture value?
Assuming a fixture driver returns a Selenium WebDriver instance, which assertion correctly checks the page title is 'Home'?
Selenium Python
def test_title(driver):
    driver.get('http://example.com')
    # Which assertion is correct here?
Aassert driver.page_title == 'Home'
Bassert driver.get_title() == 'Home'
Cassert driver.title == 'Home'
Dassert driver.title() == 'Home'
Attempts:
2 left
💡 Hint
Check the Selenium WebDriver API for the page title property.
❓ locator
advanced
2:00remaining
Which locator is best for a shared fixture to find a login button?
In a shared fixture, you want to locate the login button reliably across tests. Which locator is best practice?
Adriver.find_element('class name', 'btn')
Bdriver.find_element('xpath', '//button[text()="Login"]')
Cdriver.find_element('css selector', 'button.btn-primary')
Ddriver.find_element('id', 'login-btn')
Attempts:
2 left
💡 Hint
IDs are unique and stable locators.
🔧 Debug
advanced
2:30remaining
Why does this fixture cause tests to fail intermittently?
Analyze the following fixture and explain why tests using it sometimes fail with a stale element error.
Selenium Python
import pytest

@pytest.fixture(scope='function')
def login(driver):
    driver.get('http://example.com/login')
    login_button = driver.find_element('id', 'login-btn')
    login_button.click()
    yield
    driver.quit()
AThe fixture quits the driver after each test, causing stale element errors if reused.
BThe fixture does not wait for page load before clicking, causing timing issues.
CThe fixture uses function scope but should use module scope for driver reuse.
DThe fixture yields before clicking the login button, so click never happens.
Attempts:
2 left
💡 Hint
Consider what happens to the driver after the test finishes.
❓ framework
expert
3:00remaining
How to share a Selenium WebDriver instance across multiple test modules using conftest.py?
You want to create a shared Selenium WebDriver fixture in conftest.py that opens the browser once per test session and closes it after all tests finish. Which fixture definition achieves this?
A
import pytest

@pytest.fixture(scope='module')
def driver():
    from selenium import webdriver
    driver = webdriver.Chrome()
    yield driver
    driver.quit()
B
import pytest

@pytest.fixture(scope='session')
def driver():
    from selenium import webdriver
    driver = webdriver.Chrome()
    yield driver
    driver.quit()
C
import pytest

@pytest.fixture(scope='function')
def driver():
    from selenium import webdriver
    driver = webdriver.Chrome()
    yield driver
    driver.quit()
D
import pytest

def driver():
    from selenium import webdriver
    driver = webdriver.Chrome()
    yield driver
    driver.quit()
Attempts:
2 left
💡 Hint
Consider fixture scope to control browser lifetime.

Practice

(1/5)
1. What is the main purpose of a conftest.py file in Selenium Python testing?
easy
A. To hold shared fixtures for setup and cleanup across multiple test files
B. To store test data in JSON format
C. To define Selenium WebDriver locators
D. To write test cases directly

Solution

  1. Step 1: Understand the role of conftest.py

    The conftest.py file is used to define fixtures that can be shared across multiple test files for setup and cleanup tasks.
  2. Step 2: Differentiate from other files

    It does not store test data, locators, or test cases directly but provides reusable setup code like browser initialization.
  3. Final Answer:

    To hold shared fixtures for setup and cleanup across multiple test files -> Option A
  4. Quick Check:

    conftest.py = shared fixtures [OK]
Hint: Remember: conftest.py shares setup code across tests [OK]
Common Mistakes:
  • Confusing conftest.py with test case files
  • Thinking conftest.py stores test data
  • Using conftest.py to write test assertions
2. Which of the following is the correct way to declare a fixture in conftest.py using pytest?
easy
A. def browser(): yield webdriver.Chrome()
B. @pytest.fixture def browser(): yield webdriver.Chrome()
C. fixture browser(): yield webdriver.Chrome()
D. @fixture def browser(): return webdriver.Chrome()

Solution

  1. Step 1: Identify correct fixture decorator usage

    pytest fixtures require the @pytest.fixture decorator above the function to mark it as a fixture.
  2. Step 2: Check syntax correctness

    @pytest.fixture def browser(): yield webdriver.Chrome() correctly uses @pytest.fixture and a generator with yield to provide the resource.
  3. Final Answer:

    @pytest.fixture\ndef browser(): yield webdriver.Chrome() -> Option B
  4. Quick Check:

    Use @pytest.fixture decorator [OK]
Hint: Always prefix fixture functions with @pytest.fixture [OK]
Common Mistakes:
  • Omitting @pytest.fixture decorator
  • Using wrong decorator name like @fixture
  • Not using yield for setup/teardown
3. Given this fixture in conftest.py:
@pytest.fixture
def browser():
    driver = webdriver.Chrome()
    yield driver
    driver.quit()
What happens when a test uses this fixture?
medium
A. The browser never opens because yield is missing
B. The browser opens and closes immediately before the test runs
C. The browser opens but never closes
D. The browser opens before the test and closes after the test finishes

Solution

  1. Step 1: Understand yield in fixture

    The yield statement pauses the fixture, providing the driver to the test, then resumes after the test to run cleanup.
  2. Step 2: Analyze setup and teardown

    Driver is created before yield (browser opens), and driver.quit() runs after test finishes (browser closes).
  3. Final Answer:

    The browser opens before the test and closes after the test finishes -> Option D
  4. Quick Check:

    yield = setup before, cleanup after [OK]
Hint: yield splits setup and teardown in fixtures [OK]
Common Mistakes:
  • Thinking yield closes browser immediately
  • Forgetting driver.quit() after yield
  • Assuming browser stays open forever
4. You wrote this fixture in conftest.py:
@pytest.fixture
def browser():
    driver = webdriver.Chrome()
    return driver
    driver.quit()
What is the problem with this fixture?
medium
A. driver.quit() is never called, so browser stays open after tests
B. Syntax error because return and yield cannot be used together
C. Fixture will not provide the driver to tests
D. driver.quit() runs before driver is created

Solution

  1. Step 1: Analyze return and driver.quit() placement

    Since return exits the function immediately, driver.quit() after it never runs.
  2. Step 2: Understand impact on browser lifecycle

    Without driver.quit(), the browser stays open after tests, causing resource leaks.
  3. Final Answer:

    driver.quit() is never called, so browser stays open after tests -> Option A
  4. Quick Check:

    return ends function before cleanup [OK]
Hint: Use yield, not return, to ensure cleanup runs [OK]
Common Mistakes:
  • Placing driver.quit() after return
  • Using return instead of yield for fixtures
  • Expecting cleanup code after return to run
5. You want to share a browser fixture in conftest.py that opens Chrome once per test session and closes it after all tests finish. Which fixture scope and code is correct?
hard
A. @pytest.fixture(scope='function') def browser(): driver = webdriver.Chrome() yield driver driver.quit()
B. @pytest.fixture(scope='module') def browser(): driver = webdriver.Chrome() driver.quit() yield driver
C. @pytest.fixture(scope='session') def browser(): driver = webdriver.Chrome() yield driver driver.quit()
D. @pytest.fixture(scope='session') def browser(): driver = webdriver.Chrome() driver.quit() yield driver

Solution

  1. Step 1: Choose correct fixture scope for session-wide browser

    Using scope='session' makes the fixture run once per test session.
  2. Step 2: Verify setup and teardown order with yield

    Driver is created before yield (browser opens), and driver.quit() runs after all tests finish.
  3. Final Answer:

    @pytest.fixture(scope='session')\ndef browser():\n driver = webdriver.Chrome()\n yield driver\n driver.quit() -> Option C
  4. Quick Check:

    scope='session' + yield = one browser per session [OK]
Hint: Use scope='session' and yield for one browser per session [OK]
Common Mistakes:
  • Using scope='function' for session-wide browser
  • Calling driver.quit() before yield
  • Yielding after driver.quit()
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard