|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +Guidance for Claude when working in this repository. |
| 4 | + |
| 5 | +## What this is |
| 6 | + |
| 7 | +Python bindings for [Playwright](https://playwright.dev). The Python client talks JSON over a pipe to the Node-based driver bundled in `playwright/driver/`. The pipe protocol is defined upstream in `packages/protocol/src/protocol.yml`. |
| 8 | + |
| 9 | +## Layout |
| 10 | + |
| 11 | +- `playwright/_impl/` — hand-written client implementation (one module per object: `_browser.py`, `_page.py`, `_locator.py`, `_network.py`, etc.). Edit these to add or change behavior. |
| 12 | +- `playwright/async_api/_generated.py`, `playwright/sync_api/_generated.py` — **auto-generated**. Never edit by hand; rerun `./scripts/update_api.sh` after changing `_impl/` or the driver. |
| 13 | +- `scripts/generate_api.py`, `scripts/generate_async_api.py`, `scripts/generate_sync_api.py`, `scripts/documentation_provider.py` — codegen and validation. They diff the Python implementation against the driver's `playwright/driver/package/api.json` and abort if either side is out of sync. |
| 14 | +- `scripts/expected_api_mismatch.txt` — explicit allowlist of "documented in JS, not in Python" or "named differently in Python" gaps. Lines that no longer apply must be removed. |
| 15 | +- `tests/async/`, `tests/sync/` — pytest suites. Most new tests are added to the async file with a sync mirror. |
| 16 | +- `setup.py` — `driver_version = "X.Y.Z"` is the source of truth for which driver build is downloaded from `cdn.playwright.dev`. |
| 17 | +- `ROLLING.md`, `CONTRIBUTING.md` — human-facing setup and roll docs. |
| 18 | + |
| 19 | +## Setup |
| 20 | + |
| 21 | +`CONTRIBUTING.md` has the full sequence. The short version: |
| 22 | + |
| 23 | +```sh |
| 24 | +python3 -m venv env && source env/bin/activate |
| 25 | +pip install --upgrade pip |
| 26 | +pip install -r local-requirements.txt |
| 27 | +pip install -e . |
| 28 | +python -m build --wheel # downloads the driver listed in setup.py |
| 29 | +pre-commit install |
| 30 | +``` |
| 31 | + |
| 32 | +If the system lacks `python3-venv`, `uv venv env` is an acceptable substitute (then `uv pip install --python env/bin/python --upgrade pip`). |
| 33 | + |
| 34 | +## Common commands |
| 35 | + |
| 36 | +- Regenerate `_generated.py`: `./scripts/update_api.sh` (runs codegen + pre-commit on the generated files). |
| 37 | +- Lint everything: `pre-commit run --all-files`. |
| 38 | +- Type-check: `mypy playwright`. |
| 39 | +- Run tests: `pytest --browser chromium [-k name]`. Browsers are installed via `playwright install chromium` (do **not** use `--with-deps`, which requires sudo). |
| 40 | + |
| 41 | +When changing public API, edit `_impl/`, then run `./scripts/update_api.sh`. The script regenerates `_generated.py` and validates against the driver's `api.json`. If validation fails, fix the mismatch in `_impl/`, in `expected_api_mismatch.txt`, or in `documentation_provider.py` — not by hand-editing `_generated.py`. |
| 42 | + |
| 43 | +## Rolling Playwright to a new version |
| 44 | + |
| 45 | +This is the recurring high-stakes task. Use the dedicated skill: |
| 46 | + |
| 47 | +→ **[`.claude/skills/playwright-roll/SKILL.md`](.claude/skills/playwright-roll/SKILL.md)** |
| 48 | + |
| 49 | +It documents the full process: the upstream commit-range diff over `docs/src/api/`, how to classify each commit (PORT / MISMATCH / N/A), how to handle the `langs:` filter, the recurring failure modes, and the tests/sync-mirroring conventions. |
| 50 | + |
| 51 | +## House style |
| 52 | + |
| 53 | +- Don't hand-edit generated files. |
| 54 | +- Don't add `# type: ignore` or modify `_generated.py` to silence pyright; fix the source of the mismatch. |
| 55 | +- New public methods on impl classes need a sync test mirror under `tests/sync/`. |
| 56 | +- Keep `expected_api_mismatch.txt` minimal — every entry needs a one-line rationale comment above it. |
| 57 | +- Prefer `locals_to_params(locals())` for forwarding optional kwargs to channel sends, matching the rest of the codebase. |
0 commit comments