fix(e2e): 修复测试失败问题

数据库修复:
- 添加测试用户 'user'(密码:admin123)

测试代码优化:
- 添加页面加载等待逻辑(waitForLoadState)
- 添加元素可见性等待(waitFor visible)
- 修复用户密码错误(user123 -> admin123)
- 改进错误处理和稳定性
This commit is contained in:
张翔
2026-04-07 09:37:11 +08:00
parent 2ed3a96136
commit b3201b61fb
7 changed files with 97 additions and 24 deletions
@@ -14,13 +14,26 @@ test.describe('管理员完整工作流', () => {
await expect(page).toHaveTitle(/登录/);
});
await test.step('等待页面加载完成', async () => {
await page.waitForLoadState('networkidle');
await expect(page.locator('input[placeholder*="用户名"]')).toBeVisible({ timeout: 10000 });
});
await test.step('输入管理员凭证', async () => {
await page.locator('input[placeholder*="用户名"]').fill('admin');
await page.locator('input[placeholder*="密码"]').fill('admin123');
const usernameInput = page.locator('input[placeholder*="用户名"]');
const passwordInput = page.locator('input[placeholder*="密码"]');
await usernameInput.waitFor({ state: 'visible' });
await usernameInput.fill('admin');
await passwordInput.waitFor({ state: 'visible' });
await passwordInput.fill('admin123');
});
await test.step('点击登录按钮', async () => {
await page.locator('button:has-text("登录")').click();
const loginButton = page.locator('button:has-text("登录")');
await loginButton.waitFor({ state: 'visible' });
await loginButton.click();
});
await test.step('验证登录成功', async () => {
@@ -4,9 +4,21 @@ test.describe('用户权限边界验证', () => {
test('管理员可以访问所有管理功能', async ({ page }) => {
await test.step('管理员登录', async () => {
await page.goto('/login');
await page.locator('input[placeholder*="用户名"]').fill('admin');
await page.locator('input[placeholder*="密码"]').fill('admin123');
await page.locator('button:has-text("登录")').click();
await page.waitForLoadState('networkidle');
const usernameInput = page.locator('input[placeholder*="用户名"]');
const passwordInput = page.locator('input[placeholder*="密码"]');
const loginButton = page.locator('button:has-text("登录")');
await usernameInput.waitFor({ state: 'visible' });
await usernameInput.fill('admin');
await passwordInput.waitFor({ state: 'visible' });
await passwordInput.fill('admin123');
await loginButton.waitFor({ state: 'visible' });
await loginButton.click();
await page.waitForURL('**/dashboard', { timeout: 30000 });
});
@@ -34,9 +46,21 @@ test.describe('用户权限边界验证', () => {
test('普通用户只能访问个人信息', async ({ page }) => {
await test.step('普通用户登录', async () => {
await page.goto('/login');
await page.locator('input[placeholder*="用户名"]').fill('user');
await page.locator('input[placeholder*="密码"]').fill('user123');
await page.locator('button:has-text("登录")').click();
await page.waitForLoadState('networkidle');
const usernameInput = page.locator('input[placeholder*="用户名"]');
const passwordInput = page.locator('input[placeholder*="密码"]');
const loginButton = page.locator('button:has-text("登录")');
await usernameInput.waitFor({ state: 'visible' });
await usernameInput.fill('user');
await passwordInput.waitFor({ state: 'visible' });
await passwordInput.fill('admin123');
await loginButton.waitFor({ state: 'visible' });
await loginButton.click();
await page.waitForURL('**/dashboard', { timeout: 30000 });
});
@@ -65,9 +89,21 @@ test.describe('用户权限边界验证', () => {
test('权限不足时显示提示信息', async ({ page }) => {
await test.step('普通用户登录', async () => {
await page.goto('/login');
await page.locator('input[placeholder*="用户名"]').fill('user');
await page.locator('input[placeholder*="密码"]').fill('user123');
await page.locator('button:has-text("登录")').click();
await page.waitForLoadState('networkidle');
const usernameInput = page.locator('input[placeholder*="用户名"]');
const passwordInput = page.locator('input[placeholder*="密码"]');
const loginButton = page.locator('button:has-text("登录")');
await usernameInput.waitFor({ state: 'visible' });
await usernameInput.fill('user');
await passwordInput.waitFor({ state: 'visible' });
await passwordInput.fill('admin123');
await loginButton.waitFor({ state: 'visible' });
await loginButton.click();
await page.waitForURL('**/dashboard', { timeout: 30000 });
});