P
Playwright MCP Test Project
作者 @samudithn
MCP server by samudithn
创建于 1/1/2026
更新于 about 5 hours ago
README
Repository documentation and setup instructions
SauceDemo Test Automation Framework (Dev)
A comprehensive test automation framework using Python, Playwright, and pytest with Page Object Model (POM) design pattern for testing the SauceDemo e-commerce application (https://www.saucedemo.com).
Project Structure
playwright-mcp-test-project/
├── venv/ # Python virtual environment
├── pages/ # Page Object Model classes
│ ├── __init__.py
│ ├── base_page.py # Base page class with common methods
│ ├── login_page.py # Login page object
│ ├── inventory_page.py # Products inventory page object
│ ├── cart_page.py # Shopping cart page object
│ └── checkout_page.py # Checkout process page object
├── tests/ # Test cases
│ ├── __init__.py
│ ├── test_login.py # Login functionality tests
│ └── test_e2e.py # End-to-end purchase flow tests
├── config/ # Configuration files
│ └── config.py # Test configuration settings
├── utils/ # Utility functions
│ └── helpers.py # Helper functions and utilities
├── conftest.py # Pytest fixtures and configuration
├── requirements.txt # Python dependencies
└── README.md # This file
Prerequisites
- Python 3.7 or higher
- Git
- Virtual environment (created automatically via setup)
Setup Instructions
1. Clone the Repository
git clone <repository-url>
cd playwright-mcp-test-project
2. Activate Virtual Environment
# On Windows
venv\Scripts\activate
# On macOS/Linux
# source venv/bin/activate
3. Install Dependencies
pip install -r requirements.txt
4. Install Playwright Browsers
playwright install
Running Tests
Run All Tests
pytest
Run Specific Test File
pytest tests/test_login.py
pytest tests/test_e2e.py
Run Tests with Specific Browser
pytest --browser chromium
pytest --browser firefox
pytest --browser webkit
Run Tests in Headless Mode
pytest --headless
Generate HTML Report
pytest --html=reports/report.html
Note: HTML reports are automatically generated by default when running tests (configured in pytest.ini). Open reports/report.html in your browser to view detailed test results.
HTML Report Features
- Interactive Dashboard: Visual summary of test results
- Detailed Test Logs: Step-by-step execution details
- Failure Screenshots: Automatic screenshots on test failures
- Test Metadata: Browser, platform, and timing information
- Self-Contained: Includes CSS and assets for easy sharing
Run Tests with Screenshots on Failure
pytest --screenshot=on
Framework Features
Page Object Model (POM)
- BasePage: Contains common methods used across all pages
- Page Classes: Specific page objects extend BasePage with page-specific methods
- Locators: Centralized element selectors for easy maintenance
Configuration Management
- Centralized configuration in
config/config.py - Environment-specific settings
- Test data management
Utilities
- Screenshot capture on test failures
- Random data generation
- Element interaction helpers
- Directory management
Test Fixtures
- Browser configuration
- Page setup and teardown
- Custom fixtures for common test setup
Writing New Tests
1. Create a Page Object
# pages/new_page.py
from .base_page import BasePage
class NewPage(BasePage):
USERNAME_INPUT = "#username"
PASSWORD_INPUT = "#password"
LOGIN_BUTTON = "#login-btn"
def login(self, username: str, password: str):
self.fill_input(self.USERNAME_INPUT, username)
self.fill_input(self.PASSWORD_INPUT, password)
self.click_element(self.LOGIN_BUTTON)
2. Create Test Cases
# tests/test_login.py
import pytest
from pages.login_page import LoginPage
from config.config import VALID_USERNAME, VALID_PASSWORD
def test_successful_login(page):
login_page = LoginPage(page)
login_page.navigate_to_login()
login_page.login(VALID_USERNAME, VALID_PASSWORD)
assert login_page.is_login_successful()
3. Update Configuration
Modify config/config.py for any new settings or test data.
Best Practices
Page Objects
- Keep locators at the top of the class
- Use descriptive method names
- Return self for method chaining when appropriate
- Handle waits appropriately
Test Cases
- Use descriptive test names
- Keep tests independent
- Use fixtures for setup/teardown
- Assert meaningful outcomes
Selectors
- Prefer data-testid or unique IDs
- Use CSS selectors over XPath when possible
- Avoid brittle selectors
Troubleshooting
Common Issues
- Browser not found: Run
playwright installto install browsers - Import errors: Ensure virtual environment is activated
- Element not found: Check selectors and wait conditions
Debug Mode
# Run with debug logging
pytest --log-level=DEBUG
# Run with browser visible (not headless)
pytest --headed
Contributing
- Follow the existing code structure
- Add tests for new features
- Update documentation
- Ensure all tests pass before submitting
License
This project is licensed under the MIT License.
快速设置
此服务器的安装指南
安装包 (如果需要)
uvx playwright-mcp-test-project
Cursor 配置 (mcp.json)
{
"mcpServers": {
"samudithn-playwright-mcp-test-project": {
"command": "uvx",
"args": [
"playwright-mcp-test-project"
]
}
}
}