home / skills / enoch-robinson / agent-skill-collection / webapp-testing

webapp-testing skill

/skills/development/webapp-testing

This skill helps you test local web apps with Playwright, automate UI, capture screenshots, and inspect browser logs for faster debugging.

npx playbooks add skill enoch-robinson/agent-skill-collection --skill webapp-testing

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
2.8 KB
---
name: webapp-testing
description: Web应用测试工具包。使用 Playwright 进行前端自动化测试、UI 调试、截图捕获、浏览器日志查看。当需要测试本地 Web 应用、验证前端功能、调试 UI 行为时使用此技能。
---

# Web Application Testing

使用 Playwright 测试本地 Web 应用。

## 决策树

```
任务 → 是静态 HTML?
├─ 是 → 直接读取 HTML 识别选择器 → 编写 Playwright 脚本
└─ 否(动态应用)→ 服务器已运行?
    ├─ 否 → 先启动服务器
    └─ 是 → 侦察后操作:
        1. 导航并等待 networkidle
        2. 截图或检查 DOM
        3. 识别选择器
        4. 执行操作
```

## 基础用法

```python
from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch(headless=True)
    page = browser.new_page()
    
    page.goto('http://localhost:5173')
    page.wait_for_load_state('networkidle')  # 关键:等待 JS 执行
    
    # 操作
    page.click('button#submit')
    page.fill('input[name="email"]', '[email protected]')
    
    # 截图
    page.screenshot(path='screenshot.png', full_page=True)
    
    browser.close()
```

## 启动服务器后测试

```bash
# 单服务器
npm run dev &
sleep 3
python test_script.py

# 或使用脚本管理
python with_server.py --server "npm run dev" --port 5173-- python test.py
```

## 常用操作

### 元素定位
```python
# 文本
page.click('text=登录')

# CSS 选择器
page.click('#submit-btn')
page.click('.form-input')

# 角色
page.click('role=button[name="提交"]')

# 组合
page.locator('form').locator('button').click()
```

### 等待策略
```python
# 等待元素出现
page.wait_for_selector('#content')

# 等待网络空闲
page.wait_for_load_state('networkidle')

# 固定等待(不推荐,但有时必要)
page.wait_for_timeout(1000)
```

### 表单操作
```python
page.fill('input[name="username"]', 'admin')
page.fill('input[name="password"]', '123456')
page.click('button[type="submit"]')
```

### 截图调试
```python
# 全页截图
page.screenshot(path='full.png', full_page=True)

# 元素截图
page.locator('#header').screenshot(path='header.png')
```

### 获取内容
```python
# 页面 HTML
html = page.content()

# 元素文本
text = page.locator('#result').text_content()

# 所有匹配元素
buttons = page.locator('button').all()
```

## 常见陷阱

|❌ 错误 | ✅ 正确 |
|--------|--------|
| 不等待直接操作 | 先`wait_for_load_state('networkidle')` |
| 硬编码等待时间 | 使用 `wait_for_selector` |
| 忘记关闭浏览器 | 使用 `with` 语句或 `browser.close()` |

## 最佳实践

- 始终使用 `headless=True` 运行
- 动态应用必须等待 `networkidle`
- 使用描述性选择器:`text=`、`role=`、ID
- 添加适当等待避免竞态条件
- 测试完成后关闭浏览器

Overview

This skill is a Web Application Testing toolkit that uses Playwright for front-end automation, UI debugging, screenshots, and browser log inspection. It targets local and dynamic web apps and helps validate UI behavior and end-to-end flows. The toolset includes common locator patterns, wait strategies, and server orchestration tips for reliable tests.

How this skill works

The skill drives a browser (Chromium) via Playwright to navigate pages, wait for network idle or specific selectors, interact with elements (click, fill, role/text-based selectors), and capture screenshots or page content. It supports starting a dev server when needed, probing the running app, identifying selectors, and executing scripted interactions. Built-in wait strategies and locator patterns reduce flakiness and reveal UI state for debugging.

When to use it

  • Testing local static HTML or single-page applications before deployment
  • Automating end-to-end checks for UI flows like login, forms, and navigation
  • Capturing screenshots for visual regression or debugging layout issues
  • Inspecting browser DOM, logs, and responses during development
  • Running automated checks in CI after starting the application server

Best practices

  • Always wait for networkidle on dynamic apps before interacting
  • Prefer descriptive selectors: text=, role=, and IDs over brittle CSS paths
  • Use wait_for_selector for element-driven synchronization instead of fixed sleeps
  • Run headless in automation (headless=True) and close the browser after tests
  • Encapsulate server startup/shutdown in scripts so tests run reliably in CI

Example use cases

  • Automate a login flow: navigate, fill username/password, click submit, assert result text
  • Smoke test a local dev server: start server, wait for networkidle, capture a full-page screenshot
  • Debug intermittent UI failures by taking element-level screenshots and saving page.content() at failure time
  • Validate form behavior: fill fields, submit, wait for selector with success message, extract response text
  • Run a CI job that starts the app, runs Playwright scripts, and fails on missing selectors or JS errors

FAQ

Do I need to run the dev server first?

For dynamic apps, yes: start the server or use a wrapper that launches the server before the Playwright script runs.

How do I avoid flaky tests?

Use networkidle and wait_for_selector for synchronization, prefer descriptive selectors, and avoid hard-coded sleep timers.