Skip to main content

A zero-boilerplate Selenium WebDriver fixture for pytest

Project description

pytest-selenium-driver

PyPI version Python versions License: MIT

A zero-boilerplate Selenium WebDriver fixture for pytest with support for multiple browsers, headless mode, and both local and remote execution.

✨ Features

  • 🚀 Zero setup - Works out of the box with Selenium Manager
  • 🌐 Multi-browser - Chrome and Firefox support
  • 👻 Headless mode - Perfect for CI/CD pipelines
  • 🔗 Remote execution - Selenium Grid compatibility
  • Parallel testing - pytest-xdist integration
  • 🧵 Thread-safe - Isolated WebDriver instances
  • 🎯 Type hints - Full typing support
  • 📝 Comprehensive logging - Debug-friendly output

🚀 Quick Start

Installation

pip install pytest-selenium-driver

Basic Usage

The plugin automatically registers with pytest and provides a driver fixture:

def test_example(driver):
    driver.get("https://example.com")
    assert "Example" in driver.title

def test_form_submission(driver):
    driver.get("https://httpbin.org/forms/post")
    driver.find_element("name", "custname").send_keys("John Doe")
    driver.find_element("css selector", "input[type='submit']").click()
    assert "John Doe" in driver.page_source

🎛️ Command Line Options

Option Description Default
--browser Browser to use (chrome, firefox) chrome
--headless Run browser in headless mode False
--remote Use remote WebDriver execution False
--grid-url Selenium Grid URL for remote execution None

📚 Usage Examples

Local Testing

# Run tests with Chrome (default)
pytest

# Run tests with Firefox
pytest --browser=firefox

# Run in headless mode (great for CI)
pytest --browser=chrome --headless

Remote Testing (Selenium Grid)

# Run tests on remote Selenium Grid
pytest --remote --grid-url=http://selenium-grid:4444/wd/hub

# Remote with specific browser and headless mode
pytest --remote --grid-url=http://selenium-grid:4444/wd/hub --browser=firefox --headless

Parallel Testing

# Run tests in parallel (requires pytest-xdist)
pip install pytest-xdist
pytest -n 4 --browser=chrome --headless

Integration with Other Plugins

# Generate HTML reports with pytest-html
pytest --browser=firefox --html=report.html

# Run with coverage
pytest --browser=chrome --headless --cov=myapp

🔧 Advanced Configuration

Custom Fixture Scopes

# Function-scoped (default) - fresh driver for each test
def test_with_function_driver(driver):
    pass

# Class-scoped - shared driver across test class
def test_with_class_driver(class_scoped_driver):
    pass

# Session-scoped - shared driver across entire test session
def test_with_session_driver(session_scoped_driver):
    pass

Extending with Custom Arguments

Create a conftest.py to add custom CLI arguments:

def pytest_addoption(parser):
    group = parser.getgroup("selenium-driver")
    group.addoption("--timeout", type=int, default=10, 
                   help="Default timeout for WebDriver waits")

@pytest.fixture
def wait(request, driver):
    from selenium.webdriver.support.ui import WebDriverWait
    timeout = request.config.getoption("--timeout")
    return WebDriverWait(driver, timeout)

🏗️ Requirements

  • Python 3.8+
  • pytest 7.0+
  • selenium 4.6+

Note: Browser drivers are automatically managed by Selenium Manager (no manual setup required!)

🤝 Contributing

Contributions are welcome! Please see CONTRIBUTING.md for development setup and guidelines.

CI PyPI version

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

👨‍💻 Author

Shubham Singh - shubham.fps@gmail.com

🙏 Acknowledgments

  • Built on top of the excellent Selenium project
  • Inspired by the pytest testing framework
  • Thanks to the Python testing community

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_selenium_driver-0.1.1.tar.gz (17.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_selenium_driver-0.1.1-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

Details for the file pytest_selenium_driver-0.1.1.tar.gz.

File metadata

  • Download URL: pytest_selenium_driver-0.1.1.tar.gz
  • Upload date:
  • Size: 17.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for pytest_selenium_driver-0.1.1.tar.gz
Algorithm Hash digest
SHA256 adfbd1b916587a0efe461be7b4e9b99ef961fca484d0015cfe667fec79affb2e
MD5 82a56abc31603f81935d87dce475dc53
BLAKE2b-256 c1a1bc3e1624d0d347960d2ed17fd93965eb64c544092da60f49b4cf03c01a4c

See more details on using hashes here.

File details

Details for the file pytest_selenium_driver-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_selenium_driver-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 176afc07299d710c2373c83cf7120700743d7a71d68de40bb956c05ae4b48e71
MD5 b67b73af7a37b8e30070907a0e22ad83
BLAKE2b-256 e116e9edc76764d38d190e9c742ed712090af8552f954ccf735a42f63044ddc4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page