This repository demonstrates a simple Page Object Model (POM) based end-to-end
automation framework using the latest Python Playwright packages and
pytest/pytest-playwright plugin.
pytest-playwright-example/
├── pages/ # page object definitions
│ ├── base_page.py
│ └── home_page.py
├── tests/ # pytest test modules
│ └── test_example.py
├── conftest.py # fixtures (base_url, etc.)
├── pytest.ini # pytest configuration and markers
├── requirements.txt # dependencies
└── README.md # this file
-
Create a virtual environment and install dependencies (a Python 3.10+ interpreter is recommended, older versions will still work with the pinned requirements):
python -m venv .venv source .venv/bin/activate pip install -r requirements.txt -
Install browser binaries used by Playwright:
playwright install
-
Run tests:
# default single/browser run: python -m pytest -q # specify one or more browsers explicitly: python -m pytest -q --browser chromium python -m pytest -q --browser chromium,firefox,webkit
You can also instrument individual tests to accept a
browser_nameargument, which pytest-playwright will automatically populate based on the value(s) passed on the command line or configuration.Output will appear in the console. A simple HTML report can be generated by adding
--html=reports/report.html(create thereportsdirectory first) or specify a browser-specific path such as--html=reports/report-firefox.html.
class HomePage(BasePage):
def navigate(self):
self.goto("/")
def click_get_started(self):
self.page.get_by_role("link", name="Get started").click()- Add new page classes under
pages/with methods encapsulating user interactions. - Put business-level tests in
tests/that consume the page objects. - Use fixtures in
conftest.pyto share common data (URLs, credentials, etc.).
- This example uses sync API from Playwright. For async tests, adjust imports and fixtures accordingly.
pytest-playwrightprovidespage,browser, and other fixtures out of the box.
A workflow is provided at .github/workflows/ci.yml that demonstrates a
matrix build over the three Playwright engines. It:
- Checks out the repo and sets up Python 3.10.
- Caches
pippackages for speed. - Creates a virtualenv and installs requirements.
- Runs
playwright installto fetch browsers. - Executes
pytestwith--browserset from the matrix; an HTML report is archived as an artifact on every run.
You can adapt the job to publish results, post comments, or run only a subset of browsers based on the path/branch.
Feel free to adapt and expand this template for your own end-to-end test suites.