import { test, expect } from '@playwright/test'; /** * 用户认证模块最终验证测试 * 使用正确的测试数据和选择器 */ test.describe('用户认证 - 登录功能验证', () => { test.beforeEach(async ({ page }) => { // 访问登录页面 await page.goto('http://localhost:5174/login'); await page.waitForLoadState('networkidle'); }); test('应该成功登录并跳转到仪表盘', async ({ page }) => { // 填写登录表单(使用正确的演示账号) await page.fill('input[placeholder="请输入用户名"]', 'admin'); await page.fill('input[placeholder="请输入密码"]', 'admin123456'); // 点击登录按钮 await page.click('button:has-text("登录")'); // 等待页面跳转 await page.waitForURL('**/dashboard', { timeout: 10000 }); // 验证登录成功 expect(page.url()).toContain('dashboard'); }); test('应该拒绝错误的密码', async ({ page }) => { // 填写错误的密码 await page.fill('input[placeholder="请输入用户名"]', 'admin'); await page.fill('input[placeholder="请输入密码"]', 'wrongpassword'); // 点击登录按钮 await page.click('button:has-text("登录")'); // 等待错误提示出现(使用alert角色) const alert = page.locator('[role="alert"]'); await expect(alert).toBeVisible({ timeout: 5000 }); // 验证错误信息包含401或错误提示 const alertText = await alert.textContent(); expect(alertText).toMatch(/401|错误|失败/); // 验证仍在登录页面 expect(page.url()).toContain('login'); }); }); test.describe('用户认证 - 登出功能验证', () => { test.beforeEach(async ({ page }) => { // 先登录 await page.goto('http://localhost:5174/login'); await page.waitForLoadState('networkidle'); await page.fill('input[placeholder="请输入用户名"]', 'admin'); await page.fill('input[placeholder="请输入密码"]', 'admin123456'); await page.click('button:has-text("登录")'); await page.waitForURL('**/dashboard', { timeout: 10000 }); }); test('应该成功登出并返回登录页面', async ({ page }) => { // 点击用户下拉菜单(使用更通用的选择器) const userDropdown = page.locator('.user-dropdown, .el-dropdown, [class*="user"]').first(); await userDropdown.click(); // 等待下拉菜单出现 await page.waitForTimeout(500); // 点击退出按钮 const logoutButton = page.locator('text=退出, text=退出登录, text=logout').first(); await logoutButton.click(); // 等待跳转到登录页面 await page.waitForURL('**/login', { timeout: 10000 }); // 验证返回登录页面 expect(page.url()).toContain('login'); // 验证登录表单存在 const usernameInput = page.locator('input[placeholder="请输入用户名"]'); await expect(usernameInput).toBeVisible(); }); }); test.describe('用户认证 - 权限验证', () => { test.beforeEach(async ({ page }) => { // 先登录 await page.goto('http://localhost:5174/login'); await page.waitForLoadState('networkidle'); await page.fill('input[placeholder="请输入用户名"]', 'admin'); await page.fill('input[placeholder="请输入密码"]', 'admin123456'); await page.click('button:has-text("登录")'); await page.waitForURL('**/dashboard', { timeout: 10000 }); }); test('应该能够访问用户管理页面', async ({ page }) => { // 直接导航到用户管理页面 await page.goto('http://localhost:5174/sys/user'); await page.waitForLoadState('networkidle'); // 验证页面URL expect(page.url()).toContain('/sys/user'); // 验证页面内容(查找用户管理相关文本) const pageContent = await page.textContent('body'); expect(pageContent).toMatch(/用户|管理/); }); test('应该能够访问角色管理页面', async ({ page }) => { // 直接导航到角色管理页面 await page.goto('http://localhost:5174/sys/role'); await page.waitForLoadState('networkidle'); // 验证页面URL expect(page.url()).toContain('/sys/role'); // 验证页面内容(查找角色管理相关文本) const pageContent = await page.textContent('body'); expect(pageContent).toMatch(/角色|管理/); }); });