import { test, expect } from '@playwright/test'; /** * E2E测试 - API可用性验证 * 支持两种模式: * - E2E_STRICT=false (默认): 连通性模式,401/403视为可达 * - E2E_STRICT=true: 严格业务模式,需要真实凭证 */ test.describe('🦟 蚊子项目 E2E测试 - API可用性验证', () => { const API_BASE_URL = process.env.API_BASE_URL || 'http://localhost:8080'; const E2E_STRICT = process.env.E2E_STRICT === 'true'; const E2E_USER_TOKEN = process.env.E2E_USER_TOKEN; test('后端健康检查', async ({ request }) => { const response = await request.get(`${API_BASE_URL}/actuator/health`); expect(response.ok()).toBeTruthy(); const body = await response.json(); expect(body.status).toBe('UP'); console.log('✅ 后端服务健康检查通过'); }); 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}`); } else { // 连通性模式: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}`); } }); test('前端服务可访问', async ({ page }, testInfo) => { const FRONTEND_URL = process.env.PLAYWRIGHT_BASE_URL || 'http://localhost:5176'; await page.goto(FRONTEND_URL); // 验证页面加载 await expect(page).toHaveTitle(/./); // 截图记录到 Playwright 输出目录,避免污染仓库根目录 await page.screenshot({ path: testInfo.outputPath('frontend-check.png') }); console.log('✅ 前端服务可访问'); }); });