0
0
Selenium Pythontesting~20 mins

Fixtures for browser setup/teardown in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Browser Fixture Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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.
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')
ATest Passed printed and test passes
BTest Passed printed but test fails with AssertionError
CNo output printed, test passes silently
DTest fails with WebDriverException due to missing driver
Attempts:
2 left
💡 Hint
The fixture yields the driver after opening the page, so the test can access the page title.
assertion
intermediate
2: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?
Aassert browser.current_url == ''
Bassert browser.session_id is None
Cassert browser.title == ''
Dassert browser is None
Attempts:
2 left
💡 Hint
When the browser is closed, the session_id becomes None.
🔧 Debug
advanced
2: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
Adriver.close() closes only the current window, not the browser session, causing resource leaks
Bdriver.close() raises an exception because it is called after yield
Cdriver.close() should be replaced with driver.quit() to avoid syntax error
DNo bug, the fixture works correctly
Attempts:
2 left
💡 Hint
driver.close() and driver.quit() behave differently in Selenium.
framework
advanced
2: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?
Ascope='class' to open browser once per test class
Bscope='function' to open and close browser for each test function
Cscope='module' to open browser once per test module
Dscope='session' to open browser once per test session
Attempts:
2 left
💡 Hint
Consider test isolation and speed trade-offs.
🧠 Conceptual
expert
2: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?
AYield returns the browser object faster than return
BReturn cannot be used in fixtures, only yield is allowed
CReturn causes the fixture to run multiple times, yield runs once
DYield allows running teardown code after the test finishes, return does not
Attempts:
2 left
💡 Hint
Think about how pytest manages setup and teardown with fixtures.