A comprehensive demonstration of Playwright with Python, showcasing both UI automation and API automation with best practices, testing frameworks, and production-ready patterns.
- β UI Automation: Browser automation with Playwright (Chrome, Firefox, Safari)
- β API Automation: REST API testing with requests library
- β Page Object Model (POM): Scalable and maintainable test structure
- β Pytest Integration: Comprehensive testing framework with fixtures
- β Test Parallelization: Run tests in parallel with pytest-xdist
- β HTML Reports: Generate beautiful test reports with pytest-html
- β CI/CD Ready: GitHub Actions workflow included
- β Best Practices: Environment configuration, logging, and error handling
.
βββ tests/
β βββ ui/ # UI automation tests
β β βββ test_html_page.py # HTML page interaction tests
β β βββ pages/ # Page Object Models
β β βββ base_page.py # Base page with common methods
β β βββ html_page.py # HTML page object model
β βββ api/ # API automation tests
β β βββ test_rest_api.py # REST API test suite
β β βββ clients/ # API client classes
β β βββ api_client.py # Base API client
β βββ fixtures/
β βββ conftest.py # Pytest configuration and fixtures
βββ utils/
β βββ __init__.py
β βββ logger.py # Logging utility
β βββ helpers.py # Common helper functions
βββ config/
β βββ settings.py # Configuration management
β βββ .env.example # Environment variables template
βββ main.py # Quick start example
βββ pyproject.toml # Project configuration (uv/pip)
βββ uv.lock # Locked dependencies (uv)
βββ pytest.ini # Pytest configuration
βββ .gitignore # Git ignore file
βββ README.md # This file
- Python 3.11+ (Python 3.13 recommended for best compatibility)
- uv (fast Python package manager) - Recommended
- OR pip (standard package manager)
- Clone the repository
git clone <repository-url>
cd plywright-demo-python-python- Install dependencies with UV
uv syncThis automatically creates a virtual environment at .venv/ with all dependencies.
- Install Playwright browsers
uv run playwright installOr if you prefer:
python -m playwright install- Configure environment variables
cp config/.env.example config/.env
# Edit config/.env with your settings- Clone the repository
git clone <repository-url>
cd plywright-demo-python-python- Create a virtual environment
# If the above fails, try with --copies flag:
python3 -m venv venv --copies
# Then activate it:
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies
pip install -e .- Install Playwright browsers
python -m playwright install- Configure environment variables
cp config/.env.example config/.env
# Edit config/.env with your settingsuv run pytest
# Or: uv run pytest -vsource .venv/bin/activate
pytestuv run pytest tests/ui/ -vuv run pytest tests/api/ -v# Smoke tests
uv run pytest -m smoke -v
# UI tests
uv run pytest -m ui -v
# API tests
uv run pytest -m api -v
# Slow tests
uv run pytest -m slow -vuv run pytest -n autouv run pytest --html=report.html --self-contained-htmluv run pytest -v -suv run pytest tests/ui/test_html_page.py::TestExampleUI::test_navigate_to_page -v
uv run pytest tests/api/test_rest_api.py::TestExampleAPI::test_get_single_post -v# Run the demo script to verify setup
python main.pyTests demonstrate:
- Browser navigation
- Element interaction (click, type, select)
- Waiting for elements
- Taking screenshots
- Form submissions
- Table data extraction
Tests demonstrate:
- GET requests
- POST requests with JSON payloads
- PUT/DELETE operations
- Response validation
- Status code assertions
- JSON schema validation
- Error handling
Create a config/.env file based on the template:
BASE_UI_URL=https://httpbin.org/html
API_BASE_URL=https://jsonplaceholder.typicode.com
BROWSER=chromium
HEADLESS=true
TIMEOUT=30000
Note: The default URLs point to public test APIs:
httpbin.org: For UI automation testingjsonplaceholder.typicode.com: For API testing (fake JSON API)
Pytest settings are in pytest.ini:
[pytest]
testpaths = tests
python_files = test_*.py
python_classes = Test*
python_functions = test_*| Package | Version | Purpose |
|---|---|---|
| playwright | 1.48.0+ | Browser automation |
| pytest | 8.3.4+ | Testing framework |
| pytest-playwright | 0.6.2+ | Pytest integration |
| requests | 2.32.3+ | HTTP API testing |
| python-dotenv | 1.0.1+ | Environment configuration |
| pytest-html | 4.1.1+ | HTML report generation |
| pytest-xdist | 3.6.1+ | Test parallelization |
- Playwright Python Documentation
- Pytest Documentation
- Requests Library Documentation
- Page Object Model Pattern
- Never commit
.envfiles with sensitive data - Use
config/.env.exampleas a template - Store credentials in CI/CD secrets
This project is ready for GitHub Actions. See .github/workflows/ for automated test execution.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
For issues, questions, or suggestions, please open an issue on GitHub.
Happy Testing! π