/** * E2E 测试 — 核心业务流程:登录 → 驾驶舱 → 企业列表。 * * 前置条件:后端 http://localhost:8000 + 前端 http://localhost:3000 已启动。 */ import { test, expect, type Page } from "@playwright/test"; const API_BASE = "http://localhost:8000/api/v1"; const TEST_EMAIL = `e2e_flow_${Date.now()}@example.com`; const TEST_PASSWORD = "password123"; let accessToken = ""; test.describe("核心业务流程", () => { test.beforeAll(async () => { // 注册并登录获取 token await fetch(`${API_BASE}/auth/register`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ email: TEST_EMAIL, password: TEST_PASSWORD, name: "E2E流程用户", tenant_name: "E2E流程机构", role: "investor", }), }); const loginResp = await fetch(`${API_BASE}/auth/login`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ email: TEST_EMAIL, password: TEST_PASSWORD }), }); const loginData = await loginResp.json(); accessToken = loginData.data.access_token; }); test("登录后驾驶舱应显示 KPI 卡片", async ({ page }: { page: Page }) => { // 先通过 API 登录设置 token await page.goto("/login"); await page.fill('input[id="email"]', TEST_EMAIL); await page.fill('input[id="password"]', TEST_PASSWORD); await page.click('button[type="submit"]'); await expect(page).toHaveURL(/\/dashboard/); // 驾驶舱应显示 4 个 KPI 卡片 await expect(page.locator("h1")).toContainText("投资机构驾驶舱"); await expect(page.locator("text=被投企业")).toBeVisible(); await expect(page.locator("text=平均健康度")).toBeVisible(); await expect(page.locator("text=高风险事件")).toBeVisible(); await expect(page.locator("text=待审月报")).toBeVisible(); }); test("应能导航到企业列表页", async ({ page }: { page: Page }) => { // 登录 await page.goto("/login"); await page.fill('input[id="email"]', TEST_EMAIL); await page.fill('input[id="password"]', TEST_PASSWORD); await page.click('button[type="submit"]'); await expect(page).toHaveURL(/\/dashboard/); // 点击"被投企业" KPI 卡片 await page.click('a[href="/companies"]'); // 应到达企业列表页 await expect(page).toHaveURL(/\/companies/); await expect(page.locator("h1")).toContainText("被投企业"); }); test("企业列表页应显示搜索栏和添加按钮", async ({ page }: { page: Page }) => { // 登录 await page.goto("/login"); await page.fill('input[id="email"]', TEST_EMAIL); await page.fill('input[id="password"]', TEST_PASSWORD); await page.click('button[type="submit"]'); await expect(page).toHaveURL(/\/dashboard/); // 通过 sidebar 导航到企业列表(避免 page.goto 导致 AuthGuard 重新初始化) await page.click('a[href="/companies"]'); await expect(page).toHaveURL(/\/companies/); await expect(page.locator("h1")).toContainText("被投企业"); // 搜索框应可见 await expect(page.locator('input[placeholder="搜索企业名称..."]')).toBeVisible(); // 添加企业按钮应可见 await expect(page.getByRole('button', { name: '添加企业' })).toBeVisible(); }); test("通过 API 创建企业后列表应显示", async ({ page }: { page: Page }) => { // 通过 API 创建企业 const companyName = `E2E测试企业_${Date.now()}`; const companyResp = await fetch(`${API_BASE}/companies`, { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${accessToken}`, }, body: JSON.stringify({ name: companyName, industry: "AI", }), }); expect(companyResp.ok).toBeTruthy(); // 登录 await page.goto("/login"); await page.fill('input[id="email"]', TEST_EMAIL); await page.fill('input[id="password"]', TEST_PASSWORD); await page.click('button[type="submit"]'); await expect(page).toHaveURL(/\/dashboard/); // 通过 sidebar 导航到企业列表 await page.click('a[href="/companies"]'); await expect(page).toHaveURL(/\/companies/); // 等待列表加载完成 await page.waitForTimeout(2000); // 应显示刚创建的企业或空状态 const hasCompanies = await page.locator(`text=${companyName}`).count(); const hasEmptyState = await page.getByText('暂无企业', { exact: true }).count(); expect(hasCompanies > 0 || hasEmptyState > 0).toBeTruthy(); }); });