2026-03-02 13:31:54 +08:00
|
|
|
|
import { test, expect } from '@playwright/test';
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-03-23 13:02:36 +08:00
|
|
|
|
* E2E测试 - API可用性验证
|
|
|
|
|
|
* 支持两种模式:
|
|
|
|
|
|
* - E2E_STRICT=false (默认): 连通性模式,401/403视为可达
|
|
|
|
|
|
* - E2E_STRICT=true: 严格业务模式,需要真实凭证
|
2026-03-02 13:31:54 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
test.describe('🦟 蚊子项目 E2E测试 - API可用性验证', () => {
|
2026-03-23 13:02:36 +08:00
|
|
|
|
|
2026-03-02 13:31:54 +08:00
|
|
|
|
const API_BASE_URL = process.env.API_BASE_URL || 'http://localhost:8080';
|
2026-03-23 13:02:36 +08:00
|
|
|
|
const E2E_STRICT = process.env.E2E_STRICT === 'true';
|
|
|
|
|
|
const E2E_USER_TOKEN = process.env.E2E_USER_TOKEN;
|
|
|
|
|
|
|
2026-03-02 13:31:54 +08:00
|
|
|
|
test('后端健康检查', async ({ request }) => {
|
|
|
|
|
|
const response = await request.get(`${API_BASE_URL}/actuator/health`);
|
2026-03-23 13:02:36 +08:00
|
|
|
|
|
2026-03-02 13:31:54 +08:00
|
|
|
|
expect(response.ok()).toBeTruthy();
|
2026-03-23 13:02:36 +08:00
|
|
|
|
|
2026-03-02 13:31:54 +08:00
|
|
|
|
const body = await response.json();
|
|
|
|
|
|
expect(body.status).toBe('UP');
|
2026-03-23 13:02:36 +08:00
|
|
|
|
|
2026-03-02 13:31:54 +08:00
|
|
|
|
console.log('✅ 后端服务健康检查通过');
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-03-23 13:02:36 +08:00
|
|
|
|
test('活动列表API可达性验证', async ({ request }) => {
|
|
|
|
|
|
// 活动列表API需要认证
|
|
|
|
|
|
const response = await request.get(`${API_BASE_URL}/api/v1/activities`);
|
|
|
|
|
|
|
|
|
|
|
|
const status = response.status();
|
|
|
|
|
|
|
|
|
|
|
|
// 严格模式下必须有真实凭证,无凭证则失败
|
|
|
|
|
|
if (E2E_STRICT) {
|
|
|
|
|
|
if (!E2E_USER_TOKEN) {
|
|
|
|
|
|
throw new Error('严格模式需要E2E_USER_TOKEN环境变量,但未提供,测试失败');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 严格模式:必须返回200
|
|
|
|
|
|
if (status === 401 || status === 403) {
|
|
|
|
|
|
throw new Error(`严格模式下活动列表API需要认证但未提供有效凭证(HTTP ${status})`);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (status >= 400 && status < 500) {
|
|
|
|
|
|
throw new Error(`活动列表API客户端错误(HTTP ${status})`);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (status >= 500) {
|
|
|
|
|
|
throw new Error(`活动列表API服务器错误(HTTP ${status}),服务异常`);
|
|
|
|
|
|
}
|
|
|
|
|
|
expect(status).toBe(200);
|
|
|
|
|
|
console.log(`✅ 严格模式:活动列表API业务成功,HTTP状态码: ${status}`);
|
2026-03-02 13:31:54 +08:00
|
|
|
|
} else {
|
2026-03-23 13:02:36 +08:00
|
|
|
|
// 连通性模式:401/403表示API可达但需要认证
|
|
|
|
|
|
if (status === 404) {
|
|
|
|
|
|
throw new Error(`活动列表API不存在(HTTP 404),端点路径可能错误`);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (status >= 500) {
|
|
|
|
|
|
throw new Error(`活动列表API服务器错误(HTTP ${status}),服务异常`);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 连通性模式允许401/403代表API可达
|
|
|
|
|
|
expect([401, 403, 200]).toContain(status);
|
|
|
|
|
|
console.log(`✅ 连通性模式:活动列表API可达,HTTP状态码: ${status}`);
|
2026-03-02 13:31:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-03-23 13:02:36 +08:00
|
|
|
|
test('前端服务可访问', async ({ page }, testInfo) => {
|
2026-03-23 18:42:57 +08:00
|
|
|
|
const FRONTEND_URL = process.env.PLAYWRIGHT_BASE_URL || 'http://localhost:5176';
|
2026-03-23 13:02:36 +08:00
|
|
|
|
|
2026-03-02 13:31:54 +08:00
|
|
|
|
await page.goto(FRONTEND_URL);
|
2026-03-23 13:02:36 +08:00
|
|
|
|
|
2026-03-02 13:31:54 +08:00
|
|
|
|
// 验证页面加载
|
|
|
|
|
|
await expect(page).toHaveTitle(/./);
|
2026-03-23 13:02:36 +08:00
|
|
|
|
|
|
|
|
|
|
// 截图记录到 Playwright 输出目录,避免污染仓库根目录
|
|
|
|
|
|
await page.screenshot({ path: testInfo.outputPath('frontend-check.png') });
|
|
|
|
|
|
|
2026-03-02 13:31:54 +08:00
|
|
|
|
console.log('✅ 前端服务可访问');
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|