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[type="submit"]'); await expect(page).toHaveURL(/\/(dashboard|home)?/, { timeout: 10000 }); const usernameDisplay = page.locator(`text=${role.displayName}`); await expect(usernameDisplay).toBeVisible(); }); 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[type="submit"]'); await expect(page).toHaveURL(/\/(dashboard|home)?/, { timeout: 10000 }); }); test('错误密码登录失败', async ({ page }) => { await page.goto('/login'); await page.fill('input[placeholder*="用户名"]', 'admin'); await page.fill('input[placeholder*="密码"]', 'wrongpassword'); await page.click('button[type="submit"]'); const errorMessage = page.locator('text=/用户名或密码错误|登录失败/i'); await expect(errorMessage).toBeVisible({ timeout: 5000 }); await expect(page).toHaveURL(/\/login/); }); test('空用户名登录失败', async ({ page }) => { await page.goto('/login'); await page.fill('input[placeholder*="密码"]', 'Test@123'); await page.click('button[type="submit"]'); const validationMessage = page.locator('text=/请输入用户名|用户名不能为空/i'); await expect(validationMessage).toBeVisible({ timeout: 5000 }); }); test('空密码登录失败', async ({ page }) => { await page.goto('/login'); await page.fill('input[placeholder*="用户名"]', 'admin'); await page.click('button[type="submit"]'); const validationMessage = page.locator('text=/请输入密码|密码不能为空/i'); 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/); const usernameDisplay = page.locator('text=超级管理员'); await expect(usernameDisplay).toBeVisible(); }); });