원클릭으로
weapp-automation
微信小程序自动化测试工具集,支持启动微信开发者工具、页面导航、元素操作、截图、控制台日志读取等功能。用于功能测试、UI回归测试、性能监控等场景。当用户需要自动化测试小程序、控制微信开发者工具、读取小程序控制台日志或进行UI截图对比时触发此skill。
Codex 또는 Claude로 설치 이 Prompt를 복사해 Codex, Claude 또는 다른 어시스턴트에 붙여 넣으면 Skill 페이지를 검토하고 설치를 진행할 수 있습니다.
메뉴
微信小程序自动化测试工具集,支持启动微信开发者工具、页面导航、元素操作、截图、控制台日志读取等功能。用于功能测试、UI回归测试、性能监控等场景。当用户需要自动化测试小程序、控制微信开发者工具、读取小程序控制台日志或进行UI截图对比时触发此skill。
Codex 또는 Claude로 설치 이 Prompt를 복사해 Codex, Claude 또는 다른 어시스턴트에 붙여 넣으면 Skill 페이지를 검토하고 설치를 진행할 수 있습니다.
SOC 직업 분류 기준
| name | weapp-automation |
| description | 微信小程序自动化测试工具集,支持启动微信开发者工具、页面导航、元素操作、截图、控制台日志读取等功能。用于功能测试、UI回归测试、性能监控等场景。当用户需要自动化测试小程序、控制微信开发者工具、读取小程序控制台日志或进行UI截图对比时触发此skill。 |
本skill提供完整的微信小程序自动化测试能力,包括:
npm install -g miniprogram-automator
或使用 npx(无需全局安装):
npx miniprogram-automator --help
You can now read the data and internal rendering tree to assert if classes/data are correctly attached before taking a screenshot.
runner.navigate("pages/index/index")
runner.get_wxml(".hero-title") # Extract WXML DOM structure to ensure class rendering is correct
runner.get_data("diseaseInfo") # Read page's internal `data.diseaseInfo`
result = runner.run().get_results()
# For CLI:
# python scripts/weapp_automation.py -p /path/to/project -a get_wxml -s ".hero-title"
# python scripts/weapp_automation.py -p /path/to/project -a get_data --path "diseaseInfo"
根据操作系统选择正确的CLI路径:
macOS(默认):
/Applications/wechatwebdevtools.app/Contents/MacOS/cli
Windows:
C:\Program Files (x86)\Tencent\微信web开发者工具\cli.bat
自定义配置: 如果开发者工具使用非默认端口(9420),可在配置中指定:
config = AutomationConfig(
project_path="/path/to/miniprogram",
ws_endpoint="ws://localhost:9420" # 自定义WebSocket端口
)
from weapp_launcher import WeappLauncher
from weapp_automation import AutomationConfig, WeappTestRunner
# 1. 启动开发者工具
launcher = WeappLauncher()
launcher.open_project("/path/to/miniprogram")
# 2. 创建测试配置
config = AutomationConfig(
project_path="/path/to/miniprogram",
cli_path="/Applications/wechatwebdevtools.app/Contents/MacOS/cli"
)
# 3. 执行测试
runner = WeappTestRunner(config)
result = (runner
.navigate("pages/index/index")
.screenshot("home.png")
.click(".button")
.screenshot("after_click.png")
.get_results())
# 4. 查看结果
print(runner.get_summary())
控制微信开发者工具的启动和关闭。
from weapp_launcher import WeappLauncher
launcher = WeappLauncher()
# 打开项目
launcher.open_project("/path/to/miniprogram")
# 关闭项目
launcher.close_project("/path/to/miniprogram")
# 退出开发者工具
launcher.quit_wechatdevtools()
执行页面自动化操作。
from weapp_automation import AutomationConfig, WeappAutomation
config = AutomationConfig(project_path="/path/to/miniprogram")
auto = WeappAutomation(config)
# 导航
auto.navigate_to("pages/detail/detail")
# 点击元素
auto.click(".submit-button")
# 输入文本
auto.input_text("input[name='search']", "关键词")
# 截图
auto.screenshot("result.png")
# 滚动
auto.scroll(".scroll-view", "down", 500)
# 获取元素文本
result = auto.get_element_text(".title")
print(result["data"]["text"])
使用流畅的链式 API 编写测试流程。引擎底层会自动将所有 actions 打包为单独的一个 Node.js 脚本,并复用同一个 WebSocket 进行批量处理,极大提升了测试渲染速度与连接稳定性。
from weapp_automation import WeappTestRunner
runner = WeappTestRunner(config)
# 所有的操作会被收集,直到调用 get_results() 或 run() 时才真正连通 DevTools 一次性执行
result = (runner
.navigate("pages/index/index")
.wait(2)
.screenshot("home.png")
.click(".category-item")
.wait(1)
.screenshot("category.png")
.scroll(".product-list", "down", 800)
.screenshot("scrolled.png")
.get_results())
summary = runner.get_summary()
print(f"通过: {summary['passed']}/{summary['total']}")
读取和分析小程序控制台日志。
from console_reader import ConsoleReader, LogLevel
reader = ConsoleReader("/path/to/miniprogram")
# 读取日志
logs = reader.read_logs_from_script()
# 获取错误
errors = reader.get_errors()
for error in errors:
print(f"[ERROR] {error.message}")
# 导出报告
reader.export_to_markdown("logs_report.md")
from console_reader import PerformanceMonitor
monitor = PerformanceMonitor("/path/to/miniprogram")
metrics = monitor.collect_metrics()
monitor.export_report("performance.json")
预定义的测试场景模板。
from test_scenarios import TestScenarios
scenarios = TestScenarios("/path/to/miniprogram")
result = scenarios.smoke_test()
pages = ["pages/index/index", "pages/category/category", "pages/cart/cart"]
result = scenarios.navigation_flow_test(pages)
form_data = {
"input[name='username']": "testuser",
"input[name='email']": "test@example.com"
}
result = scenarios.form_submission_test(form_data)
pages = ["pages/index/index", "pages/profile/profile"]
result = scenarios.ui_regression_test(pages, baseline_dir="./baseline")
steps = [
{"action": "navigate", "page": "pages/index/index"},
{"action": "click", "selector": ".product"},
{"action": "click", "selector": ".add-to-cart"},
{"action": "navigate", "page": "pages/cart/cart"},
{"action": "screenshot", "filename": "cart.png"}
]
result = scenarios.user_journey_test(steps)
python scripts/weapp_launcher.py --project /path/to/miniprogram --action open
python scripts/weapp_launcher.py --project /path/to/miniprogram --action quit
# 导航
python scripts/weapp_automation.py -p /path/to/miniprogram -a navigate --page pages/index/index
# 点击
python scripts/weapp_automation.py -p /path/to/miniprogram -a click -s ".button"
# 输入
python scripts/weapp_automation.py -p /path/to/miniprogram -a input -s "input" -t "hello"
# 截图
python scripts/weapp_automation.py -p /path/to/miniprogram -a screenshot -f test.png
# 滚动
python scripts/weapp_automation.py -p /path/to/miniprogram -a scroll -s ".scroll-view" --direction down --distance 500
# 读取日志
python scripts/console_reader.py -p /path/to/miniprogram -a read
# 导出错误报告
python scripts/console_reader.py -p /path/to/miniprogram -a export --format markdown -o report.md
# 冒烟测试
python scripts/test_scenarios.py -p /path/to/miniprogram -s smoke
# 导航测试
python scripts/test_scenarios.py -p /path/to/miniprogram -s navigation --pages "pages/index/index,pages/about/about"
# UI回归测试
python scripts/test_scenarios.py -p /path/to/miniprogram -s ui --pages "pages/index/index,pages/profile/profile"
使用类似CSS的选择器定位元素:
| 选择器 | 示例 | 说明 |
|---|---|---|
| 类选择器 | .button | 选择class为button的元素 |
| ID选择器 | #submit | 选择id为submit的元素 |
| 标签选择器 | view | 选择view组件 |
| 属性选择器 | [data-id='123'] | 选择data-id属性为123的元素 |
| 后代选择器 | .list .item | 选择list内的item |
问题: Error: connect ECONNREFUSED
解决:
问题: Element not found
解决:
问题: 截图文件为空或不存在
解决:
详细API文档参见 references/api_reference.md
| 脚本 | 用途 |
|---|---|
scripts/weapp_launcher.py | 启动/关闭开发者工具 |
scripts/weapp_automation.py | 页面自动化操作 |
scripts/console_reader.py | 控制台日志读取 |
scripts/test_scenarios.py | 预定义测试场景 |