Files
nanocode/skills/browser-automation/SKILL.md
T
Ubuntu 32b43f85bd a
2026-07-03 16:58:28 +08:00

76 lines
2.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
name: browser-automation
description: Browser automation testing skill using Playwright + CDP. Provides L1 screenshot verification, L2 functional testing, and L3 E2E regression testing. Always uses Playwright's Chromium (not Snap) for stability. Supports semantic element定位 via Accessibility Tree / data-testid / text / CSS fallback.
triggers:
- "测试一下"
- "截图看看"
- "验证一下"
- "浏览器测试"
- "E2E测试"
- "自动化测试"
- "打开.*搜索"
- "截图.*页面"
- "测试.*功能"
---
# Browser Automation Skill
基于 Playwright + CDP 的浏览器自动化测试能力。
## 核心原则
1. **永远用 Playwright 的 Chromium**,不用 Snap 版(`/snap/bin/chromium`)— Snap 沙箱会阻塞 CDP 连接导致挂起
2. **元素定位优先级**:AX Tree 语义 > data-testid > 文本 > CSS 选择器(兜底)
3. **不用图像识别定位**(那是 PyAutoGUI 路线),用 DOM/AX Tree 语义定位
4. **遇到 visibility 为 0 但实际存在的元素**,用 `page.evaluate` JS 直接操作 DOM 绕过
## 三级测试策略
| 级别 | 场景 | 工具 | 耗时 |
|------|------|------|------|
| L1 | "截图看看效果" | Playwright 截图 | 10-30s |
| L2 | "验证某个功能" | Playwright 脚本 + 断言 | 1-2min |
| L3 | "跑完整回归" | Playwright Test + spec 文件 | 3-5min |
## 环境检查
```bash
# Playwright Chromium 位置(优先用较新版本)
~/.cache/ms-playwright/chromium-1208/chrome-linux64/chrome
~/.cache/ms-playwright/chromium-1140/chrome-linux/chrome
# 启动参数必须包含:--no-sandbox
```
## 脚本位置
所有测试脚本放在:
```
/home/ubuntu/.nanobot/workspace/tests/
```
不污染 ~/nanocode 项目代码。
## 当前可用脚本
| 脚本 | 级别 | 用途 |
|------|------|------|
| `test_screenshot.py` | L1 | 任意 URL 截图 |
| `test_navigate.py` | L1 | 基础导航验证 |
| `test_baidu_search.py` | L2 | 百度搜索(JS 绕过 visibility) |
| `test_generic_search.py` | L2 | 通用搜索测试框架 |
## 常见问题处理
**问题:Playwright fill/click 报 "element not visible"**
- 原因:某些网站(如百度)在 headless 模式下元素 CSS visibility 异常
- 解决:用 `page.evaluate` 直接 JS 操作 DOM
**问题:页面加载后空白或验证弹窗**
- 原因:反爬虫机制检测到自动化浏览器
- 解决:设置真实 UA、隐藏 `navigator.webdriver`、添加随机延迟
**问题:超时**
- 先检查目标 URL 是否可达:`curl -sI URL`
- 检查是否有弹窗/iframe 阻塞