Web 端 (Playwright): - fixtures: test-data 工厂 + API Client (乐观锁 version) + 增强 auth fixture - pages: LoginPage, PatientListPage, PatientDetailPage, HealthDataPage, AppointmentPage - flows: 患者全流程, 体征数据链路, 预约排班链路, 随访管理链路, 告警处理链路 - smoke tests 迁移到 smoke/ 目录,import 路径更新 - playwright.config.ts 更新: globalSetup 环境检查, 60s timeout, video retain 小程序端 (Vitest + miniprogram-automator): - helpers: AutomatorClient, MpApiClient, MpAuthHelper, MpNavigator - flows: 患者健康数据查看, 体征数据录入, 积分签到兑换, 积分商城浏览 - vitest.config.ts + check-readiness.ts - vitest 4.1.5 依赖安装 Playwright 发现 15 个测试 (5 flow + 10 smoke),全部就绪
67 lines
2.2 KiB
TypeScript
67 lines
2.2 KiB
TypeScript
// apps/web/e2e/fixtures/auth.fixture.ts
|
|
import { test as base, type Page } from '@playwright/test';
|
|
import { ApiClient } from './api-client';
|
|
|
|
const API_BASE = process.env.E2E_API_URL || 'http://localhost:3000/api/v1';
|
|
|
|
type E2eFixtures = {
|
|
api: ApiClient;
|
|
authenticatedPage: Page;
|
|
};
|
|
|
|
let loginPromise: Promise<{ access_token: string; refresh_token: string; user: object }> | null = null;
|
|
|
|
function login() {
|
|
if (!loginPromise) {
|
|
loginPromise = (async () => {
|
|
for (let attempt = 0; attempt < 3; attempt++) {
|
|
try {
|
|
const res = await fetch(`${API_BASE}/auth/login`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
username: process.env.E2E_ADMIN_USER || 'admin',
|
|
password: process.env.E2E_ADMIN_PASS || 'Admin@2026',
|
|
}),
|
|
});
|
|
const json = await res.json();
|
|
if (json.success) return json.data;
|
|
} catch { /* retry */ }
|
|
await new Promise((r) => setTimeout(r, 500 * (attempt + 1)));
|
|
}
|
|
throw new Error('Login failed after 3 attempts');
|
|
})();
|
|
}
|
|
return loginPromise;
|
|
}
|
|
|
|
export const test = base.extend<E2eFixtures>({
|
|
api: async ({}, use) => {
|
|
const client = new ApiClient();
|
|
await client.loginAsAdmin();
|
|
await use(client);
|
|
},
|
|
|
|
authenticatedPage: async ({ page }, use) => {
|
|
const { access_token, refresh_token, user } = await login();
|
|
await page.addInitScript((args) => {
|
|
localStorage.setItem('access_token', args.token);
|
|
localStorage.setItem('refresh_token', args.refresh);
|
|
localStorage.setItem('user', JSON.stringify(args.userData));
|
|
}, { token: access_token, refresh: refresh_token, userData: user });
|
|
await use(page);
|
|
},
|
|
|
|
page: async ({ page }, use) => {
|
|
const { access_token, refresh_token, user } = await login();
|
|
await page.addInitScript((args) => {
|
|
localStorage.setItem('access_token', args.token);
|
|
localStorage.setItem('refresh_token', args.refresh);
|
|
localStorage.setItem('user', JSON.stringify(args.userData));
|
|
}, { token: access_token, refresh: refresh_token, userData: user });
|
|
await use(page);
|
|
},
|
|
});
|
|
|
|
export { expect } from '@playwright/test';
|