0
0
Selenium Pythontesting~5 mins

Fixtures for browser setup/teardown in Selenium Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a fixture in the context of Selenium testing with Python?
A fixture is a setup function that prepares the browser environment before tests run and cleans up after tests finish, ensuring tests start with a fresh browser state.
Click to reveal answer
beginner
Why use fixtures for browser setup and teardown in Selenium tests?
Fixtures help avoid repeating browser setup code in every test, make tests cleaner, and ensure the browser closes properly after tests to free resources.
Click to reveal answer
intermediate
How do you define a fixture in pytest for Selenium browser setup?
Use the @pytest.fixture decorator on a function that creates a browser instance, yields it for tests, and then closes the browser after tests complete.
Click to reveal answer
intermediate
What does the 'yield' keyword do in a pytest fixture for Selenium?
The 'yield' pauses the fixture to run the test using the browser, then resumes to perform cleanup like closing the browser after the test finishes.
Click to reveal answer
beginner
Show a simple pytest fixture example for opening and closing a Chrome browser with Selenium.
```python
import pytest
from selenium import webdriver

@pytest.fixture
 def browser():
  driver = webdriver.Chrome()
  yield driver
  driver.quit()
```
Click to reveal answer
What is the main purpose of a fixture in Selenium testing?
ATo generate test data
BTo set up and tear down the browser environment for tests
CTo write assertions in tests
DTo log test results
In pytest, which keyword is used inside a fixture to separate setup and teardown?
Ayield
Breturn
Cbreak
Dcontinue
What method is used to close the browser in Selenium Python after tests?
Adriver.quit()
Bdriver.close()
Cdriver.stop()
Ddriver.exit()
Why should browser setup code be placed in a fixture rather than inside each test?
ATo skip browser setup
BTo make tests run slower
CTo avoid using Selenium
DTo reduce code duplication and improve test clarity
Which decorator is used to define a fixture in pytest?
A@pytest.setup
B@pytest.test
C@pytest.fixture
D@pytest.browser
Explain how to create a pytest fixture for Selenium browser setup and teardown.
Think about setup before yield and cleanup after yield.
You got /4 concepts.
    Why is it important to close the browser after tests in Selenium?
    Consider what happens if browsers stay open after tests.
    You got /4 concepts.