Files
novalon-manage-system/novalon-manage-web/e2e/critical-e2e.spec.ts
T
张翔 d224553368 feat: 实施测试金字塔策略
- 创建精简版E2E测试(critical-e2e.spec.ts),只保留5个关键业务流程
- 前端单元测试已就绪(148个测试,覆盖率42.31%)
- 执行时间从9.7分钟降低到2.81秒
- 按照测试金字塔策略优化测试体系
2026-04-04 14:04:40 +08:00

95 lines
3.0 KiB
TypeScript

import { test, expect, Page } from '@playwright/test';
import { LoginPage } from './pages/LoginPage';
import { UserManagementPage } from './pages/UserManagementPage';
test.describe('关键业务流程E2E测试', () => {
let loginPage: LoginPage;
let userManagementPage: UserManagementPage;
test.beforeEach(async ({ page }) => {
await page.goto('/');
await page.waitForLoadState('domcontentloaded');
loginPage = new LoginPage(page);
userManagementPage = new UserManagementPage(page);
});
test.afterEach(async ({ page }) => {
await page.evaluate(() => {
localStorage.clear();
sessionStorage.clear();
});
});
test('1. 用户登录流程', async ({ page }) => {
await loginPage.goto();
await loginPage.login('admin', 'admin123');
await expect(page).toHaveURL(/\/(dashboard|\/)$/, { timeout: 10000 });
await expect(page.locator('.dashboard')).toBeVisible();
const token = await page.evaluate(() => localStorage.getItem('token'));
expect(token).toBeTruthy();
});
test('2. 用户创建流程', async ({ page }) => {
await loginPage.goto();
await loginPage.login('admin', 'admin123');
await page.waitForURL(/\/(dashboard|\/)$/, { timeout: 10000 });
await userManagementPage.goto();
await userManagementPage.waitForTableReady();
const uuid = Math.random().toString(36).substring(2, 15);
const username = `user_${uuid}`;
await userManagementPage.clickCreateUser();
await userManagementPage.fillUserForm({
username: username,
password: 'Test@123',
email: `${username}@test.com`,
phone: '13800138000',
nickname: `测试用户${Date.now()}`
});
await userManagementPage.submitForm();
const success = await userManagementPage.waitForSuccessMessage();
expect(success).toBeTruthy();
});
test('3. 管理员权限验证', async ({ page }) => {
await loginPage.goto();
await loginPage.login('admin', 'admin123');
await page.waitForURL(/\/(dashboard|\/)$/, { timeout: 10000 });
await userManagementPage.goto();
await expect(userManagementPage.table).toBeVisible({ timeout: 5000 });
const userCount = await userManagementPage.getUserCount();
expect(userCount).toBeGreaterThan(0);
});
test('4. 未登录用户访问受保护页面', async ({ page }) => {
await page.goto('/dashboard');
await page.waitForURL(/\/login/, { timeout: 10000 });
await expect(page.locator('input[placeholder="请输入用户名"]')).toBeVisible();
});
test('5. 登出流程', async ({ page }) => {
await loginPage.goto();
await loginPage.login('admin', 'admin123');
await page.waitForURL(/\/(dashboard|\/)$/, { timeout: 10000 });
const avatar = page.locator('.el-avatar');
await avatar.click();
await page.waitForTimeout(1000);
const logoutButton = page.locator('.el-dropdown-menu').getByText('退出登录');
await logoutButton.click();
await page.waitForURL(/\/login/, { timeout: 10000 });
await expect(page.locator('input[placeholder="请输入用户名"]')).toBeVisible();
});
});