Files
novalon-manage-system/novalon-manage-web/e2e/role-based-tests/scenarios/authentication/login-flow.spec.ts
T
张翔 a1497a480b fix: 修复登录页面401错误处理问题
根本原因:
- request拦截器在收到401错误时立即重定向到登录页
- 这会中断Login.vue的错误处理逻辑
- 导致ElMessage.error()无法执行,错误消息toast无法显示

修复方案:
- 在登录页面时不执行重定向
- 允许Login.vue正常处理错误并显示toast消息
- 改进测试等待策略,确保toast消息出现
2026-04-04 23:25:36 +08:00

84 lines
3.0 KiB
TypeScript

import { test, expect } from '@playwright/test';
import { RoleFactory } from '../../roles/role-factory';
import { createAuthenticatedPage } from '../../shared/auth-helper';
test.describe('登录流程测试', () => {
test('管理员用户登录成功', async ({ page, context }) => {
const role = RoleFactory.getRole('admin');
await page.goto('/login');
await page.fill('input[placeholder*="用户名"]', role.credentials.username);
await page.fill('input[placeholder*="密码"]', role.credentials.password);
await page.click('button:has-text("登录")');
await expect(page).toHaveURL(/\/(dashboard|\/)?/, { timeout: 10000 });
await page.waitForLoadState('networkidle');
});
test('普通用户登录成功', async ({ page, context }) => {
const role = RoleFactory.getRole('user');
await page.goto('/login');
await page.fill('input[placeholder*="用户名"]', role.credentials.username);
await page.fill('input[placeholder*="密码"]', role.credentials.password);
await page.click('button:has-text("登录")');
await expect(page).toHaveURL(/\/(dashboard|\/)?/, { timeout: 10000 });
});
test('错误密码登录失败', async ({ page }) => {
await page.goto('/login');
await page.fill('input[placeholder*="用户名"]', 'admin');
await page.fill('input[placeholder*="密码"]', 'wrongpassword');
await Promise.all([
page.waitForResponse(resp => resp.url().includes('/auth/login') && resp.status() === 401),
page.click('button:has-text("登录")')
]);
const errorMessage = page.locator('.el-message');
await expect(errorMessage).toBeVisible({ timeout: 10000 });
await expect(errorMessage).toContainText(/用户名或密码错误|登录失败/i);
await expect(page).toHaveURL(/\/login/);
});
test('空用户名登录失败', async ({ page }) => {
await page.goto('/login');
await page.fill('input[placeholder*="密码"]', 'Test@123');
await page.click('input[placeholder*="用户名"]');
await page.click('input[placeholder*="密码"]');
await page.click('button:has-text("登录")');
const validationMessage = page.locator('.el-form-item__error');
await expect(validationMessage).toBeVisible({ timeout: 5000 });
});
test('空密码登录失败', async ({ page }) => {
await page.goto('/login');
await page.fill('input[placeholder*="用户名"]', 'admin');
await page.click('input[placeholder*="密码"]');
await page.click('input[placeholder*="用户名"]');
await page.click('button:has-text("登录")');
const validationMessage = page.locator('.el-form-item__error');
await expect(validationMessage).toBeVisible({ timeout: 5000 });
});
test('Token注入登录', async ({ page, context }) => {
await createAuthenticatedPage(page, context, 'admin');
await page.goto('/dashboard');
await expect(page).toHaveURL(/\/dashboard/);
await page.waitForLoadState('networkidle');
});
});