Files
gym-manage/gym-manage-uniapp/e2e/README.md
T

157 lines
4.9 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.
# 小程序 E2E 自动化测试
## 测试范围
| 套件 | 文件 | 用例数 | 覆盖内容 |
|---|---|---|---|
| 冒烟测试 | `smoke/app-launch.spec.js` | 2 | 首页加载、TabBar 导航(4 个 tab 互切) |
| 个人中心旅程 | `journeys/profile-journey.spec.js` | 2 | 我的课程页、个人中心(会员信息) |
| 团课预约旅程 | `journeys/booking-journey.spec.js` | 3 | 首页课程元素、搜索功能("瑜伽")、课程卡片→详情 |
---
## 环境要求
- 微信开发者工具(安装路径 `D:\微信web开发者工具\`
- 小程序项目已编译:`unpackage/dist/dev/mp-weixin`
- 服务端口:`63956`(默认,可通过 `WECHAT_DEVTOOLS_PORT` 环境变量修改)
---
## 触发方式
### 1. npm scripts(日常开发首选)
| 命令 | 说明 | IDE 管理 |
|---|---|---|
| `npm test` | 全量测试 | 自动启动,保持运行 |
| `npm run test:smoke` | 仅冒烟测试 | 自动启动,保持运行 |
| `npm run test:journeys` | 仅用户旅程测试 | 自动启动,保持运行 |
| `npm run test:profile` | 仅个人中心 | 自动启动,保持运行 |
| `npm run test:booking` | 仅团课预约 | 自动启动,保持运行 |
| `npm run test:all` | 全量 + 测试后关闭 IDE | 自动启动,自动关闭 |
| `npm run test:watch` | Watch 模式(文件变更自动重测) | 需要 IDE 预先运行 |
| `npm run test:quick` | 快速运行(IDE 需已开启) | 不管理 IDE |
| `npm run test:quick:smoke` | 快速冒烟(IDE 需已开启) | 不管理 IDE |
### 2. 一键脚本
```powershell
# 全量测试
node e2e/run-tests.js
# 仅冒烟
node e2e/run-tests.js --smoke
# 仅用户旅程
node e2e/run-tests.js --journeys
# 全量 + 关闭 IDECI 用)
node e2e/run-tests.js --close
# 详细日志
node e2e/run-tests.js --verbose
# Watch 模式
node e2e/run-tests.js --watch
```
### 3. IDE 保持运行 + 反复快测
```powershell
# 首次:启动 IDE
npm test
# 后续修改代码后:秒级快速验证
npm run test:quick
npm run test:quick:smoke
```
### 4. Git Pre-commit Hook
安装:
```bash
# 方式 AHusky(推荐)
npx husky init
npx husky add .husky/pre-commit "sh gym-manage-uniapp/scripts/pre-commit-miniapp-test.sh"
# 方式 B:手动
cp scripts/pre-commit-miniapp-test.sh .git/hooks/pre-commit
```
行为:
- 仅当 `gym-manage-uniapp/pages/``gym-manage-uniapp/e2e/` 有变更 **且** IDE 在运行时触发
- 不满足条件 → 静默跳过,不阻塞提交
- 失败时提示 `git commit --no-verify` 可跳过
### 5. Jenkins CI
```
Jenkins → Build with Parameters → TEST_SCOPE = miniapp
```
Pipeline 自动完成:编译 → 启动 IDE → 测试 → 关闭 IDE → 归档报告。
---
## 架构
```
e2e/
├── jest.config.js # Jest 配置(maxWorkers:1 顺序执行)
├── setup.js # 全局变量注入
├── globalSetup.js # 测试前准备
├── globalTeardown.js # 测试后清理(不关 IDE)
├── run-tests.js # ★ 主入口:IDE 管理 + 测试调度
├── helpers/
│ └── automator.js # 连接/断开 IDE(单例模式)
├── pages/ # Page Object 层
│ ├── HomePage.js
│ ├── LoginPage.js
│ ├── SearchPage.js
│ ├── CourseDetailPage.js
│ ├── ProfilePage.js
│ └── MyCoursesPage.js
├── smoke/
│ └── app-launch.spec.js # 冒烟测试
└── journeys/
├── profile-journey.spec.js
└── booking-journey.spec.js
```
---
## 执行顺序与状态管理
```
suite 执行顺序(jest.config.js 中 testMatch 固定):
1. smoke/app-launch.spec.js → 结束于 index tabbar ✓
2. journeys/profile-journey → 结束于 index tabbar ✓
3. journeys/booking-journey → 最后一个测试进入 course-detail(非 tabbar
↑ 放在最后执行,不影响后续套件
```
**关键约束**`miniprogram-automator``switchTab` / `reLaunch` 从非 tabbar 页面调用会超时。所有测试必须确保:
- 从 tabbar 页面开始(每个测试自己 `switchTab` 到目标 tab
- 最后一个套件的最后一个测试可以进入非 tabbar 页面
---
## 环境变量
| 变量 | 默认值 | 说明 |
|---|---|---|
| `WECHAT_DEVTOOLS_CLI` | `D:\微信web开发者工具\cli.bat` | 微信开发者工具 CLI 路径 |
| `UNIAPP_OUTPUT` | `unpackage/dist/dev/mp-weixin` | 小程序编译输出目录 |
| `WECHAT_DEVTOOLS_PORT` | `63956` | 自动化服务端口 |
---
## 已知限制
1. `miniprogram-automator``switchTab` / `reLaunch` 从非 tabbar 页(如 course-detail)调用会超时 10s
2. 微信开发者工具 CLI 路径含中文,需 `shell: true` 启动
3. 多个测试套件不能共享 IDE 时同时运行(`maxWorkers: 1`
4. 搜索页 `.clickResult()` 不会触发页面跳转(搜索结果为同页内展示)