浏览器自动化MCP客户端

通过Python客户端驱动Playwright MCP服务器,实现浏览器自动化测试与网页交互。

  • by krishnaharshap
  • Python
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 krishnaharshap/playwright-python-mcp

使用说明

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.py starts @playwright/mcp with npx and keeps the stdio session alive.
  • playwright_mcp/mcp_adapter.py performs a smoke flow against Playwright's public TodoMVC demo, then evaluates document.title.
  • tests/test_example.py verifies the Python client is wired to the Playwright MCP stdio command and tool names.
  • tests/test_mcp_integration.py is an opt-in live smoke test that launches the real MCP server and browser.
  • .github/workflows/ci.yml installs 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:

  1. Checks out the repository.
  2. Sets up Python 3.11.
  3. Installs requirements.txt.
  4. Sets up Node 20.
  5. Installs @playwright/mcp@latest.
  6. Installs Chromium for Playwright.
  7. Runs pytest and uploads report.html.