跳到主要内容

网页应用测试

收藏0

用 Python Playwright 脚本测试本地网页应用:起服务、等 networkidle、先侦察 DOM 再操作,附元素发现、静态页面与控制台日志三个示例。

让 AI 安装

星光尘缘LV2最新版本 0.1.0更新于 2026-11-11下载 0 次

SKILL.md

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:

python scripts/with_server.py --server "npm run dev" --port 5173 -- python your_automation.py

Multiple servers (e.g., backend + frontend):

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):

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:

    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

用途与依赖

作者没写,这些是平台读文件后整理的,以文件为准。

使用示例

请求:帮我测一下本地 5173 端口的应用,点开 Dashboard 看看控制台有没有报错,并截个图。

预期

先运行 python scripts/with_server.py --help 查看用法,然后用 with_server.py 启动 npm run dev 并等待 5173 端口就绪,再运行一个 Playwright 脚本:以 headless 模式启动 chromium,打开 http://localhost:5173,等待 networkidle,点击 text=Dashboard,监听并打印 console 消息,截取整页截图,最后关闭浏览器;控制台日志写入输出文件,并汇报捕获到的消息条数与截图路径。

适用场景
  • 验证本地前端页面功能是否正常
  • 调试 UI 交互行为与元素定位
  • 自动截取页面截图做视觉检查
  • 收集浏览器控制台日志排查报错
  • 对静态 HTML 文件做表单填写与点击测试
  • 同时启动前后端多个服务后跑端到端脚本
输入
  • 本地 Web 应用的访问地址或端口
  • 启动服务的命令(如 npm run dev)
  • 要测试或验证的具体操作与页面元素
  • 静态 HTML 文件的路径(如适用)
  • 截图或日志的输出位置
输出
  • Playwright 自动化脚本及其运行结果
  • 页面截图(整页或指定状态)
  • 浏览器控制台日志
  • 元素发现结果(按钮、链接、输入框列表)
  • 服务启动与关闭的过程信息
运行环境
Python 3
依赖
  • playwright
  • Playwright 浏览器(chromium)
  • Node.js/npm(仅当被测应用需要 npm 启动时)