使用说明
Playwright Python MCP
This repository demonstrates a Python test client driving browser automation through the official Playwright MCP server over stdio.
GitHub Actions -> Python MCP Client --stdio--> Playwright MCP server (@playwright/mcp) -> Browser automation
What Runs
playwright_mcp/mcp_client.pystarts@playwright/mcpwithnpxand keeps the stdio session alive.playwright_mcp/mcp_adapter.pyperforms a smoke flow against Playwright's public TodoMVC demo, then evaluatesdocument.title.tests/test_example.pyverifies the Python client is wired to the Playwright MCP stdio command and tool names.tests/test_mcp_integration.pyis an opt-in live smoke test that launches the real MCP server and browser..github/workflows/ci.ymlinstalls Python dependencies, Node 20,@playwright/mcp, Chromium, then runs the full test suite.
Project Structure
playwright-python-mcp/
│
├── .github/
│ └── workflows/
│ └── ci.yml # GitHub Actions CI workflow
│
├── playwright_mcp/ # Python MCP client implementation
│ ├── __init__.py
│ ├── browser.py # Playwright browser helper utilities
│ ├── config.py # Default smoke-test URL/text and config
│ ├── mcp_adapter.py # MCP smoke flow and evaluation logic
│ └── mcp_client.py # Starts @playwright/mcp over stdio
│
├── tests/ # Pytest test cases
│ ├── test_example.py # Unit/test wiring for MCP stdio client
│ └── test_mcp_integration.py # Opt-in live Playwright MCP smoke test
│
├── requirements.txt # Python dependency manifest
├── pytest.ini # Pytest configuration and default options
├── README.md # Project overview, setup, and CI usage
├── setup_playwright_repo.ps1 # Local Windows setup helper script
├── main.py # Optional manual runner/demo entry point
└── report.html # Generated HTML test report artifact (CI output, not tracked)
Prerequisites
- Python 3.9+
- Node.js 20+
- Git
- PowerShell (Windows)
Local Setup
python -m venv venv
.\venv\Scripts\Activate.ps1
python -m pip install --upgrade pip
python -m pip install -r requirements.txt
npm install -g @playwright/mcp@latest
npx -y @playwright/mcp@latest install-browser chrome-for-testing
Run unit tests only:
python -m pytest -v
Run the live MCP smoke locally:
$env:RUN_PLAYWRIGHT_MCP_INTEGRATION = "1"
python -m pytest -v tests/test_mcp_integration.py
Run the manual flow:
python main.py
Configuration
The client defaults to the official stdio command:
npx -y @playwright/mcp@latest --headless --browser chrome --isolated
You can override the executable with:
$env:PLAYWRIGHT_MCP_COMMAND = "npx"
You can override the public smoke-test URL with:
$env:PLAYWRIGHT_MCP_SMOKE_URL = "https://playwright.dev/"
$env:PLAYWRIGHT_MCP_SMOKE_TITLE_TEXT = "Playwright"
The previous MCP_SERVER_URL WebSocket flow is no longer required for this repo. Browser automation now happens through the local stdio MCP server process.
@playwright/mcp@latest currently requires Node.js 20+ for the live smoke. The unit tests still run without launching the MCP server.
The default smoke URL is https://demo.playwright.dev/todomvc/, a free public demo app maintained for Playwright automation examples.
GitHub Actions
The CI workflow:
- Checks out the repository.
- Sets up Python 3.11.
- Installs
requirements.txt. - Sets up Node 20.
- Installs
@playwright/mcp@latest. - Installs Chromium for Playwright.
- Runs pytest and uploads
report.html.