Selaa lähdekoodia

test(config): E2E测试配置和运行指南

Xiaogang Liao 2 kuukautta sitten
vanhempi
sitoutus
fed82b7a46
3 muutettua tiedostoa jossa 174 lisäystä ja 8 poistoa
  1. 119 0
      tests/E2E-TEST-GUIDE.md
  2. 11 2
      tests/package.json
  3. 44 6
      tests/playwright.config.js

+ 119 - 0
tests/E2E-TEST-GUIDE.md

@@ -0,0 +1,119 @@
+# E2E 测试运行指南
+
+## 环境准备
+
+### 1. 启动后端服务
+
+```bash
+cd /app/cfc/cfc-backend
+mvn spring-boot:run
+# 后端服务运行在 http://localhost:9082
+```
+
+### 2. 启动前端 H5 服务
+
+```bash
+cd /app/cfc/cfc-frontend
+npm run dev:h5
+# 前端 H5 服务运行在 http://localhost:8080
+```
+
+### 3. 安装 Playwright 依赖
+
+```bash
+cd /app/cfc/tests
+npm install
+npx playwright install
+```
+
+## 运行测试
+
+### 运行所有 E2E 测试
+
+```bash
+cd /app/cfc/tests
+npm run test:e2e
+```
+
+### 运行指定流程测试
+
+```bash
+# 运行测评订单流程
+npx playwright test tests/e2e/assessment-order-flow.spec.js
+
+# 运行家庭成员管理流程
+npx playwright test tests/e2e/family-member-management.spec.js
+
+# 运行心愿兑换流程
+npx playwright test tests/e2e/wish-exchange-flow.spec.js
+```
+
+### 调试模式(有 UI)
+
+```bash
+npm run test:e2e:headed
+```
+
+### 查看测试报告
+
+```bash
+npm run test:report
+# 报告保存在 playwright-report/ 目录
+```
+
+## 测试文件列表
+
+| 文件 | 描述 | 场景数 |
+|------|------|--------|
+| `assessment-order-flow.spec.js` | 测评订单全流程 | 7 |
+| `family-member-management.spec.js` | 家庭成员管理 | 9 |
+| `wish-exchange-flow.spec.js` | 心愿兑换审批 | 9 |
+| `planner-bind-invite.spec.js` | 规划师绑定邀请 | 7 |
+| `growth-record-sync.spec.js` | 成长记录同步 | 6 |
+| `energy-system.spec.js` | 能量系统收支 | 6 |
+| `product-purchase.spec.js` | 商品购买支付 | 9 |
+| `daily-checkin-streak.spec.js` | 连续签到奖励 | 8 |
+
+## 注意事项
+
+1. **页面路由**:测试使用 uni-app hash 路由格式(如 `/#/pages/assessment/apply`)
+2. **元素选择器**:测试使用 CSS class 选择器,需要与前端实现匹配
+3. **测试数据**:部分测试需要预设数据(如已绑定的家庭成员、已有的订单等)
+4. **超时设置**:复杂操作已设置较长的超时时间
+
+## 测试覆盖的里程碑
+
+每个测试文件都包含以下里程碑验证:
+
+- **Milestone-1**: 流程启动成功(如创建订单、添加成员)
+- **Milestone-2**: 核心操作成功(如支付、定价)
+- **Milestone-3**: 状态变更成功
+- **Milestone-4**: 数据关联/更新成功
+- **Milestone-5**: 完整流程成功
+
+## 故障排查
+
+### 问题:页面元素找不到
+
+**原因**:前端页面结构可能与测试选择器不匹配
+
+**解决**:
+1. 检查前端页面的实际 class 名称
+2. 更新测试中的选择器
+3. 使用 `page.waitForSelector()` 等待元素出现
+
+### 问题:API 请求超时
+
+**原因**:后端服务未启动或响应慢
+
+**解决**:
+1. 确保后端服务运行在 9082 端口
+2. 增加 timeout 配置
+
+### 问题:测试数据不存在
+
+**原因**:依赖的数据未初始化
+
+**解决**:
+1. 在测试前手动创建所需数据
+2. 或在测试中使用 setup 钩子准备数据

+ 11 - 2
tests/package.json

@@ -5,6 +5,15 @@
   "scripts": {
     "test": "jest",
     "test:integration": "jest --testMatch '**/integration/**/*.spec.js'",
-    "test:e2e": "playwright test"
-  }
+    "test:e2e": "playwright test",
+    "test:e2e:headed": "playwright test --headed",
+    "test:e2e:debug": "playwright test --debug",
+    "test:flow": "playwright test tests/e2e/*-flow.spec.js",
+    "test:all": "npm run test:e2e && npm run test:integration"
+  },
+  "devDependencies": {
+    "@playwright/test": "^1.40.0",
+    "jest": "^29.7.0"
+  },
+  "description": "浠艾福(CFC) 分层测试套件"
 }

+ 44 - 6
tests/playwright.config.js

@@ -1,11 +1,49 @@
+/**
+ * Playwright E2E 测试配置
+ *
+ * 运行 E2E 测试前需要:
+ * 1. 启动后端服务:cd cfc-backend && mvn spring-boot:run
+ * 2. 启动前端 H5:cd cfc-frontend && npm run dev:h5
+ *
+ * 运行测试:
+ *   npx playwright test                           # 运行所有测试
+ *   npx playwright test tests/e2e/assessment    # 运行指定测试文件
+ *   npx playwright test --grep "场景1"            # 运行包含特定场景的测试
+ */
+
 module.exports = {
   testDir: './e2e',
-  timeout: 30000,
+  timeout: 60000,
   retries: 0,
   use: {
-    // uni-app H5 dev server runs on 8080 by default
-    // Must run `npm run dev:h5` in cfc-frontend before running E2E tests
+    // uni-app H5 dev server (默认 8080)
     baseURL: 'http://localhost:8080',
-    headless: true
-  }
-}
+    headless: true,
+    viewport: { width: 375, height: 812 }, // iPhone X 尺寸
+    ignoreHTTPSErrors: true,
+    // 等待策略
+    actionTimeout: 15000,
+    navigationTimeout: 30000,
+  },
+  projects: [
+    {
+      name: 'chromium',
+      use: { browserName: 'chromium' },
+    },
+    {
+      name: 'webkit',
+      use: { browserName: 'webkit' },
+    },
+  ],
+  reporter: [
+    ['html', { outputFolder: 'playwright-report' }],
+    ['list'],
+  ],
+  // 全局测试钩子
+  globalSetup: async () => {
+    // 确保测试环境就绪
+  },
+  globalTeardown: async () => {
+    // 清理测试数据
+  },
+}