Skip to content

captain-nimo/plywright-demo-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

10 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Playwright Python Demo Project

A comprehensive demonstration of Playwright with Python, showcasing both UI automation and API automation with best practices, testing frameworks, and production-ready patterns.

🎯 Features

  • βœ… 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

πŸ“‹ Project Structure

.
β”œβ”€β”€ 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

πŸš€ Getting Started

Prerequisites

  • Python 3.11+ (Python 3.13 recommended for best compatibility)
  • uv (fast Python package manager) - Recommended
  • OR pip (standard package manager)

Installation

Option 1: Using UV (Recommended) ⚑

  1. Clone the repository
git clone <repository-url>
cd plywright-demo-python-python
  1. Install dependencies with UV
uv sync

This automatically creates a virtual environment at .venv/ with all dependencies.

  1. Install Playwright browsers
uv run playwright install

Or if you prefer:

python -m playwright install
  1. Configure environment variables
cp config/.env.example config/.env
# Edit config/.env with your settings

Option 2: Using pip

  1. Clone the repository
git clone <repository-url>
cd plywright-demo-python-python
  1. 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
  1. Install dependencies
pip install -e .
  1. Install Playwright browsers
python -m playwright install
  1. Configure environment variables
cp config/.env.example config/.env
# Edit config/.env with your settings

πŸ“ Usage

Running All Tests (with UV)

uv run pytest
# Or: uv run pytest -v

Running All Tests (with activated venv)

source .venv/bin/activate
pytest

Running UI Tests Only

uv run pytest tests/ui/ -v

Running API Tests Only

uv run pytest tests/api/ -v

Running Specific Test Markers

# 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 -v

Running Tests in Parallel

uv run pytest -n auto

Generating HTML Report

uv run pytest --html=report.html --self-contained-html

Running with Debug Mode

uv run pytest -v -s

Running Specific Test

uv 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

Quick Demo

# Run the demo script to verify setup
python main.py

πŸ§ͺ Test Examples

UI Automation Example

Tests demonstrate:

  • Browser navigation
  • Element interaction (click, type, select)
  • Waiting for elements
  • Taking screenshots
  • Form submissions
  • Table data extraction

API Automation Example

Tests demonstrate:

  • GET requests
  • POST requests with JSON payloads
  • PUT/DELETE operations
  • Response validation
  • Status code assertions
  • JSON schema validation
  • Error handling

πŸ”§ Configuration

Environment Variables

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 testing
  • jsonplaceholder.typicode.com: For API testing (fake JSON API)

Pytest Configuration

Pytest settings are in pytest.ini:

[pytest]
testpaths = tests
python_files = test_*.py
python_classes = Test*
python_functions = test_*

πŸ“¦ Dependencies

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

πŸŽ“ Learning Resources

πŸ”’ Security

  • Never commit .env files with sensitive data
  • Use config/.env.example as a template
  • Store credentials in CI/CD secrets

πŸ“Š CI/CD Integration

This project is ready for GitHub Actions. See .github/workflows/ for automated test execution.

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

πŸ“„ License

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

πŸ“§ Support

For issues, questions, or suggestions, please open an issue on GitHub.


Happy Testing! 🎭

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages