Fixtures help open and close the browser automatically for each test. This keeps tests clean and avoids repeating code.
Fixtures for browser setup/teardown in Selenium Python
Introduction
Syntax
Selenium Python
import pytest from selenium import webdriver @pytest.fixture def browser(): driver = webdriver.Chrome() yield driver driver.quit()
The @pytest.fixture decorator marks the setup function.
The yield keyword splits setup (before yield) and teardown (after yield).
Examples
Selenium Python
import pytest from selenium import webdriver @pytest.fixture def browser(): driver = webdriver.Firefox() yield driver driver.quit()
Selenium Python
import pytest from selenium import webdriver @pytest.fixture(scope="session") def browser(): driver = webdriver.Chrome() yield driver driver.quit()
Selenium Python
import pytest from selenium import webdriver @pytest.fixture def browser(): driver = webdriver.Chrome() driver.maximize_window() yield driver driver.quit()
Sample Program
This test script uses a fixture to open and close Chrome for two tests. The first test checks the page title. The second test checks if the search box is visible.
Selenium Python
import pytest from selenium import webdriver from selenium.webdriver.common.by import By @pytest.fixture def browser(): driver = webdriver.Chrome() yield driver driver.quit() def test_google_title(browser): browser.get("https://www.google.com") assert "Google" in browser.title def test_google_search_box(browser): browser.get("https://www.google.com") search_box = browser.find_element(By.NAME, "q") assert search_box.is_displayed()
Important Notes
Always quit the browser in teardown to free resources.
Use fixtures to avoid repeating browser setup code in every test.
You can change the fixture scope to control how often the browser opens.
Summary
Fixtures automate browser setup and cleanup for tests.
Use yield to separate setup and teardown steps.
Fixtures keep tests simple and avoid code duplication.
Practice
1. What is the main purpose of using a fixture for browser setup and teardown in Selenium tests?
easy
Solution
Step 1: Understand fixture role
Fixtures help prepare the environment before tests and clean up after tests.Step 2: Apply to browser context
In Selenium, this means opening the browser before tests and closing it after.Final Answer:
To automate opening and closing the browser before and after tests -> Option CQuick Check:
Fixture = setup + teardown automation [OK]
Hint: Fixtures handle setup and cleanup automatically [OK]
Common Mistakes:
- Thinking fixtures replace test cases
- Believing fixtures remove need for assertions
- Assuming fixtures limit browser types
2. Which of the following is the correct way to define a pytest fixture for browser setup and teardown using
yield?easy
Solution
Step 1: Understand yield usage in fixtures
Yield separates setup (before yield) and teardown (after yield).Step 2: Check correct order
Setup: create driver; yield it; teardown: quit driver.Final Answer:
def browser(): driver = webdriver.Chrome() yield driver driver.quit() -> Option AQuick Check:
Setup before yield, teardown after yield [OK]
Hint: Setup before yield, teardown after yield in fixture [OK]
Common Mistakes:
- Calling quit before yield
- Using driver.close() instead of driver.quit() for teardown
- Yielding before creating driver
3. Given this fixture and test code, what will be printed when the test runs?
import pytest
from selenium import webdriver
@pytest.fixture
def browser():
driver = webdriver.Chrome()
print('Setup browser')
yield driver
print('Teardown browser')
driver.quit()
def test_example(browser):
print('Running test')medium
Solution
Step 1: Trace fixture execution order
Fixture runs setup code before yield, then test runs, then fixture runs teardown after yield.Step 2: Match print statements order
Prints 'Setup browser', then 'Running test', then 'Teardown browser'.Final Answer:
Setup browser\nRunning test\nTeardown browser -> Option DQuick Check:
Setup -> Test -> Teardown order [OK]
Hint: Fixture setup prints before test, teardown prints after [OK]
Common Mistakes:
- Assuming teardown runs before test
- Ignoring yield separates setup and teardown
- Thinking prints run all before test
4. What is wrong with this fixture code for browser setup and teardown?
@pytest.fixture
def browser():
driver = webdriver.Chrome()
driver.quit()
yield drivermedium
Solution
Step 1: Analyze order of operations
driver.quit() is called before yield, so browser closes before test uses it.Step 2: Understand yield role
Yield should come before teardown to keep browser open during test.Final Answer:
Calling driver.quit() before yield closes browser too early -> Option BQuick Check:
Teardown must be after yield [OK]
Hint: Teardown code must be after yield in fixture [OK]
Common Mistakes:
- Calling quit before yield
- Confusing yield with return
- Ignoring fixture import requirements
5. You want to run multiple tests using the same browser instance to save time. How should you modify the fixture to achieve this?
hard
Solution
Step 1: Understand fixture scopes
Default scope is 'function' which runs fixture per test; 'module' runs once per file.Step 2: Apply scope to reuse browser
Setting scope='module' opens browser once for all tests in module, saving time.Final Answer:
Change fixture scope to 'module' so browser opens once per module -> Option AQuick Check:
Fixture scope controls reuse [OK]
Hint: Use scope='module' to share browser across tests [OK]
Common Mistakes:
- Closing browser before tests run
- Removing yield breaks setup/teardown
- Creating new fixture per test wastes time
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
