Challenge - 5 Problems
Browser Fixture Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of pytest fixture with browser setup
What will be the output of the following pytest test when run?
Note: The fixture opens a browser, navigates to example.com, then closes the browser after the test.
Note: The fixture opens a browser, navigates to example.com, then closes the browser after the test.
Selenium Python
import pytest from selenium import webdriver @pytest.fixture def browser(): driver = webdriver.Chrome() driver.get('https://example.com') yield driver driver.quit() def test_title(browser): assert 'Example Domain' in browser.title print('Test Passed')
Attempts:
2 left
💡 Hint
The fixture yields the driver after opening the page, so the test can access the page title.
✗ Incorrect
The fixture sets up the browser and navigates to example.com. The test asserts the title contains 'Example Domain', which is true, so the assertion passes and 'Test Passed' is printed. The fixture then quits the browser after the test.
❓ assertion
intermediate2:00remaining
Correct assertion to verify browser is closed after test
Given a pytest fixture that opens and closes a browser, which assertion correctly verifies the browser is closed after the test?
Selenium Python
import pytest from selenium import webdriver @pytest.fixture def browser(): driver = webdriver.Chrome() yield driver driver.quit() def test_browser_closed(browser): pass # Which assertion below is correct here?
Attempts:
2 left
💡 Hint
When the browser is closed, the session_id becomes None.
✗ Incorrect
After driver.quit(), the session_id attribute becomes None indicating the browser session is closed. Other attributes like current_url or title raise exceptions or remain unchanged until accessed.
🔧 Debug
advanced2:00remaining
Identify the bug in fixture teardown
What is the bug in this pytest fixture for browser setup and teardown?
Selenium Python
import pytest from selenium import webdriver @pytest.fixture def browser(): driver = webdriver.Chrome() driver.get('https://example.com') yield driver driver.close() # Bug here def test_example(browser): assert 'Example Domain' in browser.title
Attempts:
2 left
💡 Hint
driver.close() and driver.quit() behave differently in Selenium.
✗ Incorrect
driver.close() closes the current browser window but does not end the WebDriver session, which can leave the browser process running. driver.quit() properly ends the session and closes all windows.
❓ framework
advanced2:00remaining
Best practice for browser fixture scope in pytest
Which pytest fixture scope is best to use for browser setup/teardown to speed up multiple UI tests without side effects?
Attempts:
2 left
💡 Hint
Consider test isolation and speed trade-offs.
✗ Incorrect
Using scope='module' opens the browser once per module, balancing speed and isolation. 'function' is slow, 'session' risks side effects across unrelated tests, 'class' is less common and depends on test organization.
🧠 Conceptual
expert2:00remaining
Why use yield in pytest fixtures for browser setup?
Why is the yield statement preferred over return in pytest fixtures that setup and teardown a browser?
Attempts:
2 left
💡 Hint
Think about how pytest manages setup and teardown with fixtures.
✗ Incorrect
Yield pauses the fixture to run the test, then resumes to run teardown code after the test. Return ends the fixture immediately, so no teardown code runs after the test.