网络应用程序测试
- 作者仓库星标 0
- 许可证 Complete terms in LICENSE.txt
- 作者仓库 FarmFriend-Terminal-React
- 领域
- 工程开发
- 作者声明的智能体
- 未找到明确声明;不据此推断已兼容或已测试
- 静态检查
- 94 / 100 · 启发式扫描,不代表运行安全
- 运行环境证据
- 未执行;当前没有隔离运行测试证据
- 作者 / 版本 / 许可证
- @0-CYBERDYNE-SYSTEMS-0 · 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=
内容校验值 -> d291b6049d8b
当前原文的自动摘译,尚未人工审校。
示例:使用 withserver.py
要启动服务器,请先运行
--help,然后使用帮助程序: 单个服务器: 多个服务器(例如后端 + 前端): 要创建自动化脚本,仅包含 Playwright 逻辑(服务器自动管理):先侦察后行动模式
检查渲染的 DOM:从检查结果中识别选择器 使用发现的选择器执行操作
常见陷阱
❌ 在动态应用程序上等待
networkidle之前不要检查 DOM ✅ 在检查之前一定要等待page.wait_for_load_state('networkidle')最佳实践
使用捆绑脚本作为黑匣子 - 要完成任务,请考虑
scripts/中可用的脚本之一是否可以提供帮助。这些脚本可以可靠地处理常见、复杂的工作流程,而不会扰乱上下文窗口。使用--help查看用法,然后直接调用。使用sync_playwright()进行同步脚本 完成后始终关闭浏览器 使用描述性选择器:text=、role=、CSS 选择器或 ID参考文件
example/ - 显示常见模式的示例:
element_discovery.py- 发现页面上的按钮、链接和输入static_html_automation.py- 使用本地 HTML 的 file:// URLconsole_logging.py- 在自动化过程中捕获控制台日志