Skip to main content

A pytest wrapper with fixtures for Playwright to automate web browsers

Project description

Pytest plugin for Playwright PyPI

Write end-to-end tests for your web apps with Playwright and pytest.

  • Support for all modern browsers including Chromium, WebKit and Firefox.
  • Support for headless and headful execution.
  • Built-in fixtures that provide browser primitives to test functions.

Usage

pip install pytest-playwright

Use the page fixture to write a basic test. See more examples.

def test_example_is_working(page):
    page.goto("https://example.com")
    assert page.innerText('h1') == 'Example Domain'
    page.click("text=More information")

To run your tests, use pytest CLI.

# Run tests (Chromium and headless by default)
pytest

# Run tests in headful mode
pytest --headful

# Run tests in a different browser (chromium, firefox, webkit)
pytest --browser firefox

# Run tests in multiple browsers
pytest --browser chromium --browser webkit

Fixtures

This plugin configures Playwright-specific fixtures for pytest. To use these fixtures, use the fixture name as an argument to the test function.

def test_my_app_is_working(fixture_name):
    # Test using fixture_name
    # ...

Function scope: These fixtures are created when requested in a test function and destroyed when the test ends.

Session scope: These fixtures are created when requested in a test function and destroyed when all tests end.

  • browser: Browser instance launched by Playwright.
  • browser_name: Browser name as string.
  • is_chromium, is_webkit, is_firefox: Booleans for the respective browser types.

Customizing fixture options: For browser and context fixtures, use the the following fixtures to define custom launch options.

Examples

Configure Mypy typings for auto-completion

from playwright.sync_api import Page

def test_visit_admin_dashboard(page: Page):
    page.goto("/admin")
    # ...

Skip test by browser

import pytest

@pytest.mark.skip_browser("firefox")
def test_visit_example(page):
    page.goto("https://example.com")
    # ...

Run on a specific browser

import pytest

@pytest.mark.only_browser("chromium")
def test_visit_example(page):
    page.goto("https://example.com")
    # ...

Configure base-url

Start Pytest with the base-url argument.

pytest --base-url http://localhost:8080
def test_visit_example(page):
    page.goto("/admin")
    # -> Will result in http://localhost:8080/admin

Debugging

Use with pdb

Use the breakpoint() statement in your test code to pause execution and get a pdb REPL.

def test_bing_is_working(page):
    page.goto("https://bing.com")
    breakpoint()
    # ...

Screenshot on test failure

You can capture screenshots for failed tests with a pytest runtest hook. Add this to your conftest.py file.

Note that this snippet uses slugify to convert test names to file paths, which can be installed with pip install python-slugify.

# In conftest.py
from slugify import slugify
from pathlib import Path

def pytest_runtest_makereport(item, call) -> None:
    if call.when == "call":
        if call.excinfo is not None:
            page = item.funcargs["page"]
            screenshot_dir = Path(".playwright-screenshots")
            screenshot_dir.mkdir(exist_ok=True)
            page.screenshot(path=str(screenshot_dir / f"{slugify(item.nodeid)}.png"))

Deploy to CI

Use the Playwright GitHub Action or guides for other CI providers to deploy your tests to CI/CD

Special thanks

Thanks to Max Schmitt for creating and maintaining this project.

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-playwright-0.0.7.tar.gz (16.8 kB view details)

Uploaded Source

Built Distribution

pytest_playwright-0.0.7-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file pytest-playwright-0.0.7.tar.gz.

File metadata

  • Download URL: pytest-playwright-0.0.7.tar.gz
  • Upload date:
  • Size: 16.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.8.5

File hashes

Hashes for pytest-playwright-0.0.7.tar.gz
Algorithm Hash digest
SHA256 906f259b5f14cf481ae9ab3279ba30a4eb4d236004ffdf4b70bcd40e05fb43a4
MD5 ef7832dc5a8aa9db9a47a3d5f610bb36
BLAKE2b-256 84f8b041108b40924a23c55bc672185f3091609a56ff71c0aaf0d7d363d06857

See more details on using hashes here.

File details

Details for the file pytest_playwright-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: pytest_playwright-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.8.5

File hashes

Hashes for pytest_playwright-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 4feb5ad109a99f5e235d24dc5e31484b602d6ffb18053949c608893bea057740
MD5 36c323dde0f6f3e1572194edb39b4cc5
BLAKE2b-256 bd011377dbdbd9d6dd8c19ef33ea3c3513a0519e7b43e97a22d77a796b32c551

See more details on using hashes here.

Supported by

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