Playwrightを使って、ローカル環境(自分のパソコン)で動いているウェブアプリケーションを操作したり、テストしたりするためのツールです。 フロントエンド(ユーザーが見える画面部分)の機能が正しく動いているかの確認、UI(画面上のボタンやメニューなど)の動作のデバッグ、ブラウザのスクリーンショット撮影、ブラウザのログ(動作の記録)確認などに対応しています。
Toolkit for interacting with and testing local web applications using Playwright. Supports verifying frontend functionality, debugging UI behavior, capturing browser screenshots, and viewing browser logs.
ローカルのWebアプリケーションをテストするには、ネイティブのPython Playwrightスクリプトを記述してください。
利用可能なヘルパースクリプト:
scripts/with_server.py - サーバーのライフサイクルを管理(複数サーバーに対応)スクリプトは必ず最初に --help オプションを付けて実行し、使用方法を確認してください。
カスタマイズされた解決策が絶対に必要だと判断するまで、ソースコードを読まないでください。
これらのスクリプトは非常に大きくなる場合があり、コンテキストウィンドウを圧迫します。
コンテキストに取り込むのではなく、ブラックボックスのスクリプトとして直接呼び出すことを前提に設計されています。
ユーザーのタスク → 静的HTMLか?
├─ はい → HTMLファイルを直接読んでセレクターを特定
│ ├─ 成功 → 特定したセレクターでPlaywrightスクリプトを記述
│ └─ 失敗/不完全 → 動的コンテンツとして扱う(下記参照)
│
└─ いいえ(動的Webアプリ) → サーバーはすでに起動中か?
├─ いいえ → 実行: python scripts/with_server.py --help
│ その後、ヘルパーを使用して簡略化したPlaywrightスクリプトを記述
│
└─ はい → 事前調査してから操作:
1. ページに遷移し、networkidle まで待機
2. スクリーンショット撮影またはDOMの検査
3. レンダリングされた状態からセレクターを特定
4. 特定したセレクターを使ってアクションを実行
サーバーを起動するには、まず --help を実行してから、ヘルパーを使用してください。
単一サーバーの場合:
python scripts/with_server.py --server "npm run dev" --port 5173 -- python your_automation.py
複数サーバーの場合(例: バックエンド + フロントエンド):
python scripts/with_server.py \
--server "cd backend && python server.py" --port 3000 \
--server "cd frontend && npm run dev" --port 5173 \
-- python your_automation.py
自動化スクリプトを作成する際は、Playwrightのロジックのみを記述してください(サーバーの管理は自動で行われます):
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch(headless=True) # 常にヘッドレスモードでChromiumを起動
page = browser.new_page()
page.goto('http://localhost:5173') # サーバーはすでに起動・準備完了
page.wait_for_load_state('networkidle') # 重要: JavaScriptの実行完了まで待機
# ... 自動化ロジックをここに記述
browser.close()
レンダリングされたDOMを検査する:
page.screenshot(path='/tmp/inspect.png', full_page=True)
content = page.content()
page.locator('button').all()
検査結果からセレクターを特定する
特定したセレクターを使ってアクションを実行する
❌ NG: 動的アプリで networkidle を待たずにDOMを検査する
✅ OK: 検査の前に page.wait_for_load_state('networkidle') で待機する
scripts/ に用意されているスクリプトが活用できないか検討してください。
これらのスクリプトは、一般的かつ複雑なワークフローをコンテキストウィンドウを汚染することなく確実に処理します。
--help で使用方法を確認してから、直接呼び出してください。sync_playwright() を使用するtext=、role=、CSSセレクター、またはIDpage.wait_for_selector() または page.wait_for_timeout()element_discovery.py — ページ上のボタン・リンク・入力フィールドの検出static_html_automation.py — ローカルHTMLに対して file:// URLを使用した自動化console_logging.py — 自動化実行中のコンソールログのキャプチャTo test local web applications, write native Python Playwright scripts.
Helper Scripts Available:
scripts/with_server.py - Manages server lifecycle (supports multiple servers)Always run scripts with --help first to see usage. DO NOT read the source until you try running the script first and find that a customized solution is abslutely necessary. These scripts can be very large and thus pollute your context window. They exist to be called directly as black-box scripts rather than ingested into your context window.
User task → Is it static HTML?
├─ Yes → Read HTML file directly to identify selectors
│ ├─ Success → Write Playwright script using selectors
│ └─ Fails/Incomplete → Treat as dynamic (below)
│
└─ No (dynamic webapp) → Is the server already running?
├─ No → Run: python scripts/with_server.py --help
│ Then use the helper + write simplified Playwright script
│
└─ Yes → Reconnaissance-then-action:
1. Navigate and wait for networkidle
2. Take screenshot or inspect DOM
3. Identify selectors from rendered state
4. Execute actions with discovered selectors
To start a server, run --help first, then use the helper:
Single server:
python scripts/with_server.py --server "npm run dev" --port 5173 -- python your_automation.py
Multiple servers (e.g., backend + frontend):
python scripts/with_server.py \
--server "cd backend && python server.py" --port 3000 \
--server "cd frontend && npm run dev" --port 5173 \
-- python your_automation.py
To create an automation script, include only Playwright logic (servers are managed automatically):
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch(headless=True) # Always launch chromium in headless mode
page = browser.new_page()
page.goto('http://localhost:5173') # Server already running and ready
page.wait_for_load_state('networkidle') # CRITICAL: Wait for JS to execute
# ... your automation logic
browser.close()
Inspect rendered DOM:
page.screenshot(path='/tmp/inspect.png', full_page=True)
content = page.content()
page.locator('button').all()
Identify selectors from inspection results
Execute actions using discovered selectors
❌ Don't inspect the DOM before waiting for networkidle on dynamic apps
✅ Do wait for page.wait_for_load_state('networkidle') before inspection
scripts/ can help. These scripts handle common, complex workflows reliably without cluttering the context window. Use --help to see usage, then invoke directly.sync_playwright() for synchronous scriptstext=, role=, CSS selectors, or IDspage.wait_for_selector() or page.wait_for_timeout()element_discovery.py - Discovering buttons, links, and inputs on a pagestatic_html_automation.py - Using file:// URLs for local HTMLconsole_logging.py - Capturing console logs during automation原文・著作権は Anthropic および各プラグイン作者に帰属します。日本語訳は Claude API による自動翻訳です。