0
0
Selenium-pythonHow-ToBeginner · 4 min read

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 browser
  • def test_example(browser):: test function using the browser fixture
  • browser.get(url): opens a web page
  • assert: 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

ConceptDescriptionExample
FixtureSetup and teardown browser@pytest.fixture def browser(): ...
Test FunctionWrite tests starting with test_def test_example(browser): ...
Open URLNavigate to web pagebrowser.get('https://example.com')
AssertionCheck expected resultassert 'Example' in browser.title
WaitsUse explicit waits to avoid flakinessWebDriverWait(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.