webapp-testing
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseWeb Application Testing
Web应用测试
To test local web applications, write native Python Playwright scripts.
Helper Scripts Available:
- - Manages server lifecycle (supports multiple servers)
scripts/with_server.py
Always run scripts with first to see usage. DO NOT read the source until you try running the script first and find that a customized solution is absolutely necessary.
--help要测试本地Web应用,请编写原生Python Playwright脚本。
可用辅助脚本:
- - 管理服务器生命周期(支持多服务器)
scripts/with_server.py
请始终先使用运行脚本查看使用方法。除非在尝试运行脚本后发现确实需要定制化解决方案,否则不要直接阅读源代码。
--helpDecision 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用户任务 → 是否为静态HTML?
├─ 是 → 直接读取HTML文件以定位选择器
│ ├─ 成功 → 使用选择器编写Playwright脚本
│ └─ 失败/不完整 → 按动态应用处理(见下方)
│
└─ 否(动态Web应用) → 服务器是否已运行?
├─ 否 → 运行:python scripts/with_server.py --help
│ 然后使用辅助脚本 + 编写简化的Playwright脚本
│
└─ 是 → 先侦察再执行:
1. 导航至页面并等待networkidle状态
2. 截取截图或检查DOM
3. 从渲染状态中定位选择器
4. 使用找到的选择器执行操作Example: Using with_server.py
示例:使用with_server.py
To start a server, run first, then use the helper:
--helpSingle server:
bash
python scripts/with_server.py --server "npm run dev" --port 5173 -- python your_automation.pyMultiple 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.pyTo 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()要启动服务器,请先运行查看帮助,然后使用辅助脚本:
--help单服务器:
bash
python scripts/with_server.py --server "npm run dev" --port 5173 -- python your_automation.py多服务器(例如:后端 + 前端):
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要创建自动化脚本,只需包含Playwright逻辑(服务器将自动管理):
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
先侦察再执行模式
-
Inspect rendered DOM:python
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
-
检查渲染后的DOM:python
page.screenshot(path='/tmp/inspect.png', full_page=True) content = page.content() page.locator('button').all() -
从检查结果中定位选择器
-
使用找到的选择器执行操作
Common Pitfall
常见误区
❌ Don't inspect the DOM before waiting for on dynamic apps
✅ Do wait for before inspection
networkidlepage.wait_for_load_state('networkidle')❌ 不要在动态应用中未等待状态就检查DOM
✅ 务必在检查前等待
networkidlepage.wait_for_load_state('networkidle')Best Practices
最佳实践
- Use bundled scripts as black boxes - To accomplish a task, consider whether one of the scripts available in can help. These scripts handle common, complex workflows reliably without cluttering the context window. Use
scripts/to see usage, then invoke directly.--help - Use for synchronous scripts
sync_playwright() - Always close the browser when done
- Use descriptive selectors: ,
text=, CSS selectors, or IDsrole= - Add appropriate waits: or
page.wait_for_selector()page.wait_for_timeout()
- 将打包脚本视为黑盒 - 要完成任务时,先考虑目录下的可用脚本是否能提供帮助。这些脚本可可靠处理常见的复杂工作流,且不会占用上下文窗口空间。使用
scripts/查看用法后直接调用即可。--help - 同步脚本使用
sync_playwright() - 完成操作后务必关闭浏览器
- 使用描述性选择器:,
text=, CSS选择器或IDsrole= - 添加适当的等待:或
page.wait_for_selector()page.wait_for_timeout()
Reference Files
参考文件
- examples/ - Examples showing common patterns:
- - Discovering buttons, links, and inputs on a page
element_discovery.py - - Using file:// URLs for local HTML
static_html_automation.py - - Capturing console logs during automation
console_logging.py
- examples/ - 展示常见模式的示例:
- - 发现页面上的按钮、链接和输入框
element_discovery.py - - 对本地HTML使用file:// URLs
static_html_automation.py - - 自动化过程中捕获控制台日志
console_logging.py