0
0
Selenium Pythontesting~20 mins

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

Choose your learning style9 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.