How to Use Selenium with Pytest for Web Testing
To use
selenium with pytest, write test functions that use Selenium WebDriver to control browsers, then run them with the pytest command. Use fixtures to set up and tear down the browser for clean, reusable tests.Syntax
Use pytest test functions starting with test_. Use Selenium WebDriver to open and interact with web pages. Use pytest fixtures to initialize and quit the browser before and after tests.
Example parts:
@pytest.fixture: defines setup/teardown for browserdef test_example(browser):: test function using the browser fixturebrowser.get(url): opens a web pageassert: checks expected conditions
python
import pytest from selenium import webdriver @pytest.fixture def browser(): driver = webdriver.Chrome() yield driver driver.quit() def test_example(browser): browser.get('https://example.com') assert 'Example Domain' in browser.title
Example
This example shows a simple test that opens example.com and checks the page title using Selenium WebDriver and Pytest.
python
import pytest from selenium import webdriver @pytest.fixture def browser(): driver = webdriver.Chrome() yield driver driver.quit() def test_example_domain_title(browser): browser.get('https://example.com') assert 'Example Domain' in browser.title
Output
============================= test session starts ==============================
collected 1 item
test_sample.py . [100%]
============================== 1 passed in 5.12s ===============================
Common Pitfalls
Common mistakes when using Selenium with Pytest include:
- Not quitting the browser after tests, causing resource leaks.
- Using hardcoded waits instead of Selenium waits, leading to flaky tests.
- Not using fixtures for setup/teardown, causing repeated code.
- Failing to install the correct WebDriver or mismatched browser versions.
Always use yield in fixtures to ensure proper cleanup and prefer explicit waits like WebDriverWait.
python
import pytest from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC @pytest.fixture def browser(): driver = webdriver.Chrome() yield driver driver.quit() def test_wait_example(browser): browser.get('https://example.com') # Wrong: time.sleep(5) # Avoid fixed sleeps # Right: wait for element WebDriverWait(browser, 10).until( EC.presence_of_element_located((By.TAG_NAME, 'h1')) ) assert 'Example Domain' in browser.title
Quick Reference
| Concept | Description | Example |
|---|---|---|
| Fixture | Setup and teardown browser | @pytest.fixture def browser(): ... |
| Test Function | Write tests starting with test_ | def test_example(browser): ... |
| Open URL | Navigate to web page | browser.get('https://example.com') |
| Assertion | Check expected result | assert 'Example' in browser.title |
| Waits | Use explicit waits to avoid flakiness | WebDriverWait(browser, 10).until(...) |
Key Takeaways
Use pytest fixtures to manage Selenium WebDriver setup and cleanup.
Write test functions starting with test_ that use the browser fixture.
Use explicit waits instead of fixed sleeps to make tests reliable.
Always quit the browser after tests to free resources.
Run tests with the pytest command to see clear pass/fail results.