A beginner's guide and examples for browser automation testing using Playwright with Python.
This project demonstrates basic to intermediate Playwright concepts for end-to-end web testing. It includes:
- Installation setup
- Basic test examples
- Browser interaction demonstrations
- Configuration and best practices
- Python 3.8 or higher
- pip (Python package installer)
- Git (optional, for version control)
-
Clone or download this repository
-
Create a virtual environment (recommended):
python3 -m venv venv
-
Activate the virtual environment:
source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install pytest-playwright
-
Install browser binaries:
playwright install
Optional: Install system dependencies (Linux/Mac):
playwright install --with-deps
playwright/
├── venv/ # Virtual environment
├── playwright-beginner-guide.md # Comprehensive beginner guide
├── test_example.py # Basic test examples
├── test_interactions.py # Browser interaction demonstrations
├── example_screenshot.png # Screenshot from tests
└── README.md # This file
source venv/bin/activatepytestpytest test_example.pypytest test_example.py::test_has_titlepytest --headedpytest --browser chromium # or firefox, webkitpytest --browser allpytest --uitest_has_title: Basic navigation and title assertiontest_get_started_link: Clicking links and verifying navigation
Demonstrates core Playwright interactions:
- Navigation (
page.goto()) - Clicking elements (
page.click(), role-based locators) - Text input (
page.fill()) - Waiting for elements (
page.wait_for_selector()) - Screenshots (
page.screenshot()) - Assertions (
expect()statements)
Each test gets an isolated browser context (incognito-like session).
Playwright automatically waits for elements to be ready before interactions.
Multiple ways to find elements:
- CSS selectors:
page.click("button#submit") - Role-based:
page.get_by_role("button", name="Submit") - Text content:
page.get_by_text("Welcome")
Web-first assertions using expect():
expect(page).to_have_title("My App")
expect(page.get_by_text("Success")).to_be_visible()Create pytest.ini for custom settings:
[tool:pytest]
testpaths = .
addopts = --browser chromium --headedBrowser not found:
playwright installTests failing:
- Check internet connection
- Verify target websites are accessible
- Use
--headedto debug visually
Virtual environment issues:
- Ensure venv is activated:
source venv/bin/activate - Reinstall if corrupted:
rm -rf venv && python3 -m venv venv
If you see library warnings on Linux, install system dependencies:
# Ubuntu/Debian
sudo apt-get install libnss3 libatk-bridge2.0-0 libdrm2 libxkbcommon0 libxcomposite1 libxdamage1 libxrandr2 libgbm1 libxss1 libasound2
# Or use Playwright's installer
playwright install --with-deps- Explore the
playwright-beginner-guide.mdfor detailed explanations - Modify existing tests to learn different scenarios
- Add new tests for your own applications
- Set up CI/CD with GitHub Actions
- Learn advanced features: API testing, mobile emulation, visual comparisons
Feel free to add more test examples or improve the documentation.
This project is for educational purposes. Playwright is licensed under Apache 2.0.