mcpField
W
webapp-testing

webapp-testing

github.com/full-stack-skills测试与质量254 次安装4 周前更新
代码仓库

关于 webapp-testing

webapp-testing 是一项使用 Python Playwright 脚本测试本地 Web 应用的技能,涵盖静态与动态页面,并提供管理一个或多个服务器的辅助脚本。

主要功能

  1. 01Python Playwright 测试指导
  2. 02单个及多个服务器管理脚本
  3. 03渲染后 DOM 检查流程
  4. 04元素发现与日志示例

适用场景

  1. 01自动检查本地 Web 应用
  2. 02测试依赖一个或多个服务器的页面
  3. 03查找渲染页面中的选择器

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/full-stack-skills/testing-skills --skill webapp-testing

安装后在 Claude Code 中输入/webapp-testing确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

__init__.py
LICENSE.txt
agents/
openai.yaml
examples/
__init__.py
console_logging.py
element_discovery.py
static_html_automation.py
scripts/
__init__.py
with_server.py

详细介绍

Web 应用测试

要测试本地 Web 应用,请编写原生 Python Playwright 脚本。

可用的辅助脚本:

  • scripts/with_server.py - 管理服务器生命周期(支持多个服务器)

务必先使用 --help 运行脚本以查看用法。请勿在尝试运行脚本并确认确实需要定制化方案之前阅读源代码。此类脚本可能非常庞大,会占用上下文窗口。它们的设计用途是作为黑盒脚本直接调用,而不是读入上下文窗口。

决策树:选择合适的方法

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

示例:使用 with_server.py

启动服务器时,请先运行 --help,然后使用辅助脚本:

单个服务器:

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_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()

先侦察再行动模式

  1. 检查渲染后的 DOM:

    page.screenshot(path='/tmp/inspect.png', full_page=True)
    content = page.content()
    page.locator('button').all()
    
  2. 根据检查结果确定选择器

  3. 使用找到的选择器执行操作

常见误区

❌ 不要在动态应用完成 networkidle 等待之前检查 DOM ✅ 要在检查之前等待 page.wait_for_load_state('networkidle')

最佳实践

  • 将内置脚本作为黑盒使用 - 为完成任务,请考虑 scripts/ 中是否有可用脚本能提供帮助。这些脚本可以可靠地处理常见的复杂工作流,且不会占用上下文窗口。使用 --help 查看用法,然后直接调用。
  • 同步脚本使用 sync_playwright()
  • 完成后务必关闭浏览器
  • 使用描述性选择器:text=、role=、CSS 选择器或 ID
  • 添加适当的等待:page.wait_for_selector() 或 page.wait_for_timeout()

参考文件

  • examples/ - 展示常见模式的示例:
    • element_discovery.py - 查找页面上的按钮、链接和输入框
    • static_html_automation.py - 使用 file:// URL 访问本地 HTML
    • console_logging.py - 在自动化期间捕获控制台日志

国内适配

  • 支持中文文档和中文注释
  • 示例代码兼容国内开发环境
  • 提供中文 FAQ 和常见问题解答

使用流程

第 1 步:环境准备

确保开发环境已安装必要的依赖和工具。

第 2 步:配置初始化

根据项目需求进行基础配置。

第 3 步:使用核心功能

按照示例代码实现核心功能。

第 4 步:测试验证

运行测试,确保功能正常。

第 5 步:部署上线

完成开发后进行部署和监控。

评论

支持 Markdown