Files
wenzi/frontend/e2e/tests/api-smoke.spec.ts

80 lines
2.8 KiB
TypeScript
Raw Normal View History

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('✅ 前端服务可访问');
});
});