pytest-selenium is a pytest plugin that provides seamless integration for running Selenium-based tests. It offers a function-scoped `selenium` fixture, allowing easy browser automation within pytest tests. The library is currently at version 4.1.0 and is actively maintained with regular updates and improvements, building upon the `pytest` and `selenium` ecosystems.
Warnings
breaking Cloud testing provider credentials were moved from generic configuration files (e.g., `pytest.ini`) to dedicated provider-specific files for improved security in versions 4.0.0/4.0.1.
Fix: Migrate cloud provider credentials to new, separate configuration files (e.g., `.saucelabs`, `.browserstack`). Refer to the official documentation for specific file names and formats. Environment variables remain an unaffected method for credential management.
gotcha By default, `pytest-selenium` assumes all tests are 'destructive' and all environments are 'sensitive' (matching any URL), causing tests to be skipped.
Fix: To prevent skipping, explicitly mark nondestructive tests with `@pytest.mark.nondestructive` or configure the `sensitive_url` regular expression in `pytest.ini` to exclude your non-sensitive test environments.
gotcha Selenium WebDriver requires browser driver executables (e.g., `chromedriver`, `geckodriver`) to be available in the system's PATH or explicitly configured, otherwise browser launch will fail.
Fix: Download the appropriate WebDriver executable for your browser and operating system, then add its directory to your system's PATH. Alternatively, you can use tools like `webdriver-manager` with direct Selenium WebDriver initialization or configure specific driver paths if `pytest-selenium` allows for it (often handled by `pytest-selenium`'s underlying Selenium setup).
deprecated With Selenium 4, the `DesiredCapabilities` class is deprecated in favor of browser-specific `Options` classes (e.g., `ChromeOptions`, `FirefoxOptions`) for configuring browser capabilities. While `pytest-selenium` abstracts some of this, direct interaction with capabilities may be affected.
Fix: When directly specifying browser capabilities, use browser-specific `Options` classes (e.g., `selenium.webdriver.ChromeOptions`, `selenium.webdriver.FirefoxOptions`) instead of `DesiredCapabilities`.
Install
pip install pytest-seleniumInstall stable version
Imports
selenium (fixture)
def test_example(selenium): ...
The 'selenium' fixture is automatically discovered and provided by the plugin; no explicit import statement from 'pytest_selenium' is typically needed in test files.
Used to mark tests that do not make destructive changes, avoiding accidental skipping in sensitive environments.
Quickstart
This quickstart demonstrates a basic pytest test using the `selenium` fixture. The fixture automatically provides a WebDriver instance. To run, specify your desired browser (e.g., Firefox or Chrome) via the `--driver` command-line option. Ensure the corresponding WebDriver executable (e.g., `geckodriver`, `chromedriver`) is in your system's PATH.
import pytest
def test_example_opens_website(selenium):
"""A simple test that opens a website using the selenium fixture."""
selenium.get('http://www.example.com')
assert "Example Domain" in selenium.title
# To run: pytest --driver Firefox -v