Web应用测试
这是一个用于使用Playwright与本地Web应用程序交互和测试的工具包。支持验证前端功能、调试UI行为、捕获浏览器截图和查看浏览器日志。
使用方法
---
name: webapp-testing
description: 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.
license: Complete terms in LICENSE.txt
---
# 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核心功能
Playwright自动化:支持编写原生Python Playwright脚本来测试Web应用程序
服务器生命周期管理:通过
scripts/with_server.py脚本管理服务器启动和停止多服务器支持:可以同时管理多个服务器(如后端+前端)
DOM检查和元素发现:支持在渲染后的页面上识别选择器和元素
屏幕截图功能:可以捕获浏览器页面的截图用于分析
控制台日志捕获:支持在自动化过程中捕获浏览器控制台日志
使用场景
验证前端应用程序的功能
调试用户界面行为
捕获浏览器屏幕截图
查看和分析浏览器日志
自动化Web应用程序测试
测试本地开发服务器上的应用程序
测试需要JavaScript执行才能正确渲染的动态应用程序
使用方法
静态HTML vs 动态应用决策:
如果是静态HTML:直接读取HTML文件以识别选择器
如果是动态Web应用且服务器未运行:使用
scripts/with_server.py启动服务器如果服务器已运行:使用侦察然后行动模式
使用with_server.py工具:
# 单个服务器 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_playwrightwith 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
从渲染状态中识别选择器
使用发现的选择器执行操作
许可证类型
Apache许可证 2.0版本 (Apache License Version 2.0),允许自由使用、复制、修改和分发,但需保留版权声明和许可证声明。该许可证提供了专利许可,并对责任进行了限制。