webapp-testing
- 信任分
- 94/100
- 兼容 Agent
- 1
- 许可证
- Complete terms in LICENSE.txt
- 领域
- 通用
- 兼容 Agent
- Claude Code
- 信任分
- 94 / 100 · 已通过审计
- 作者 / 版本 / 许可
- @anthropics · Complete terms in LICENSE.txt
- 安装命令数
- 1 条
需要注意: 未限定 allowed-tools,默认拥有全部工具权限。
想读作者英文原文? ↓ 滚到正文区切换 · 在 GitHub 查看 ↗
webapp-testing 是给本地 webapp 写 Native Python Playwright 脚本 的指南。作者特意要求:永远先 --help 看 helper script 怎么用,不要直接读它的源——脚本可能很大,读了就污染你的 context。脚本是黑箱被调,不是被 ingest。
设计思路
把 webapp 测试拆成两条路径——静态 HTML 直接读 HTML 找 selector;动态 webapp 走 Reconnaissance-then-Action(先探后做)。生命周期问题用 scripts/with_server.py 这个 helper 自动起停 server,写自动化脚本时只管 Playwright 逻辑。
Decision Tree
- 静态 HTML?
- 是 → 直接读 HTML 找 selector;不全 / 失败 → 当作动态处理
- 否(动态)→ server 已经起着?
- 否 →
python scripts/with_server.py --help然后用 helper + 写简化 Playwright 脚本 - 是 → Reconnaissance-then-action:① navigate +
wait_for_load_state('networkidle');② screenshot 或检 DOM;③ 从渲染后的状态找 selector;④ 用发现的 selector 执行动作
- 否 →
多 server 场景
作者给的最实用例子是同时起 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
helper 管 server 启停,automation 脚本里只放 Playwright 逻辑:p.chromium.launch(headless=True) → page.goto(...) → page.wait_for_load_state('networkidle') → 操作 → browser.close()。
关键原则
page.wait_for_load_state('networkidle') 是必跑的——动态 webapp 的 JS 要时间执行,不等就 selector 找不到;headless 永远 chromium headless,方便 CI;写脚本前 screenshot('/tmp/inspect.png', full_page=True) 看真实渲染状态再决定 selector,比凭脑想准多了。
适合的场景
- 给本地 webapp 写端到端测试脚本
- 多 server(前后端)需要并行起停的项目
- 想把测试沉淀成可复用 Python 脚本,而不是依赖 GUI 工具
何时不要用
- 仅做手动验证、不打算保留脚本:用
qa/qa-only更对口 - 仅测 API(无 UI):直接
requests/ 同等 HTTP 客户端跑断言
配套
qa / qa-only(gstack 的 browser QA 入口,与本技能互补)、scrape(同基于浏览器自动化的另一面)、web-artifacts-builder(artifact 自身的 Playwright 验证)、setup-browser-cookies(要登录态时配套)。
Web Application Testing
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.
Decision Tree: Choosing Your Approach
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
Example: Using with_server.py
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()
Reconnaissance-Then-Action Pattern
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
Common Pitfall
❌ Don't inspect the DOM before waiting for networkidle on dynamic apps
✅ Do wait for page.wait_for_load_state('networkidle') before inspection
Best Practices
- Use bundled scripts as black boxes - To accomplish a task, consider whether one of the scripts available in
scripts/can help. These scripts handle common, complex workflows reliably without cluttering the context window. Use--helpto see usage, then invoke directly. - Use
sync_playwright()for synchronous scripts - Always close the browser when done
- Use descriptive selectors:
text=,role=, CSS selectors, or IDs - Add appropriate waits:
page.wait_for_selector()orpage.wait_for_timeout()
Reference Files
- examples/ - Examples showing common patterns:
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