网络应用程序测试
- 作者仓库星标 0
- 许可证 Complete terms in LICENSE.txt
- 作者更新于 2026年8月21日 17:10
- 作者仓库 skills
- 领域
- 通用
- 作者声明的智能体
- 未找到明确声明;不据此推断已兼容或已测试
- 静态检查
- 94 / 100 · 启发式扫描,不代表运行安全
- 运行环境证据
- 未执行;当前没有隔离运行测试证据
- 作者 / 版本 / 许可证
- @anthropics · Complete terms in LICENSE.txt
- 上下文用量评级
- 低消耗
- 接入复杂程度
- 需简单配置
- 是否需要外部接口密钥
- 不需要
- 兼容的系统
- macOS · Linux · Windows
- 运行环境要求
- Python
- 检测到的文件与系统行为
-
- 只读
- 允许写入 / 修改
- 命令执行
- 网络行为
- 仅限本地
- 安装命令数
- 无(仅作为资料)
需要注意: 未声明工具权限范围;实际权限由运行环境决定。
python scripts/with_server.py --server "npm run dev" --port 5173 -- python your_automation.py
# 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:**
```bash
python scripts/with_server.py --server "npm run dev" --port 5173 -- python your_automation.py
```
**Multiple servers (e.g., backend + frontend):**
```bash
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):
```python
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
1. **Inspect rendered DOM**:
```python
page.screenshot(path='/tmp/inspect.png', full_page=True)
content = page.content()
page.locator('button').all()
```
2. **Identify selectors** from inspection results
3. **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 `--help` to 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()` or `page.wait_for_timeout()`
## Reference Files
- **examples/** - Examples showing common patterns:
- `element_discovery.py` - Discovering buttons, links, and inputs on a page
- `static_html_automation.py` - Using file:// URLs for local HTML
- `console_logging.py` - Capturing console logs during automation 章节与文件索引
按说明文件中的顺序排列。
示例:使用 withserver.py
先侦察后行动模式
常见陷阱
最佳实践
参考文件
文件/命令 -> scripts/with_server.py · --help · networkidle · page.wait_for_load_state('networkidle') · scripts/ · sync_playwright() · text= · role=
内容校验值 -> 1eca2df498f2
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
python scripts/with_server.py --help然后用 helper + 写简化 Playwright 脚本wait_for_load_state('networkidle');② screenshot 或检 DOM;③ 从渲染后的状态找 selector;④ 用发现的 selector 执行动作多 server 场景
作者给的最实用例子是同时起 backend + frontend:
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 永远chromiumheadless,方便 CI;写脚本前screenshot('/tmp/inspect.png', full_page=True)看真实渲染状态再决定 selector,比凭脑想准多了。适合的场景
不适合
qa/qa-only更对口requests/ 同等 HTTP 客户端跑断言配套
qa/qa-only(gstack 的 browser QA 入口,与本技能互补)、scrape(同基于浏览器自动化的另一面)、web-artifacts-builder(artifact 自身的 Playwright 验证)、setup-browser-cookies(要登录态时配套)。