Files
everything-is-suitable/everything-is-suitable-test/e2e/auth-validated.spec.ts
T
张翔 08ea5fbe98 feat(admin): 添加用户管理相关文件
添加用户管理视图、API和状态管理文件
2026-03-28 14:37:29 +08:00

171 lines
5.6 KiB
TypeScript

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 }) => {
// 监听登录API请求和响应
let loginResponse: any = null;
page.on('response', async response => {
if (response.url().includes('/api/sys/auth/login')) {
try {
const body = await response.json();
loginResponse = body;
} catch (e) {
// 忽略非JSON响应
}
}
});
// 填写登录表单(使用正确的演示账号)
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');
// 验证响应包含token
expect(loginResponse).not.toBeNull();
expect(loginResponse.token).toBeDefined();
expect(loginResponse.user).toBeDefined();
});
test('应该拒绝错误的密码', async ({ page }) => {
// 填写错误的密码
await page.fill('input[placeholder="请输入用户名"]', 'admin');
await page.fill('input[placeholder="请输入密码"]', 'wrongpassword');
// 点击登录按钮
await page.click('button:has-text("登录")');
// 等待错误提示
const errorMessage = page.locator('.el-message--error');
await expect(errorMessage).toBeVisible({ timeout: 5000 });
// 验证错误信息
const errorText = await errorMessage.textContent();
expect(errorText).toContain('用户名或密码错误');
// 验证仍在登录页面
expect(page.url()).toContain('login');
});
test('应该验证用户名不能为空', async ({ page }) => {
// 只填写密码
await page.fill('input[placeholder="请输入密码"]', 'admin123456');
// 点击登录按钮
await page.click('button:has-text("登录")');
// 验证表单验证错误
const usernameInput = page.locator('input[placeholder="请输入用户名"]');
await expect(usernameInput).toHaveAttribute('aria-invalid', 'true');
});
test('应该验证密码不能为空', async ({ page }) => {
// 只填写用户名
await page.fill('input[placeholder="请输入用户名"]', 'admin');
// 点击登录按钮
await page.click('button:has-text("登录")');
// 验证表单验证错误
const passwordInput = page.locator('input[placeholder="请输入密码"]');
await expect(passwordInput).toHaveAttribute('aria-invalid', 'true');
});
});
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');
await userDropdown.click();
// 点击退出按钮
const logoutButton = page.locator('text=退出登录');
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 }) => {
// 点击系统管理菜单
const sysMenu = page.locator('.el-sub-menu:has-text("系统管理")');
await sysMenu.click();
// 点击用户管理
const userMenu = page.locator('.el-menu-item:has-text("用户管理")');
await userMenu.click();
// 等待页面加载
await page.waitForURL('**/user', { timeout: 10000 });
// 验证页面标题
const pageTitle = page.locator('.page-title');
await expect(pageTitle).toContainText('用户管理');
});
test('应该能够访问角色管理页面', async ({ page }) => {
// 点击系统管理菜单
const sysMenu = page.locator('.el-sub-menu:has-text("系统管理")');
await sysMenu.click();
// 点击角色管理
const roleMenu = page.locator('.el-menu-item:has-text("角色管理")');
await roleMenu.click();
// 等待页面加载
await page.waitForURL('**/role', { timeout: 10000 });
// 验证页面标题
const pageTitle = page.locator('.page-title');
await expect(pageTitle).toContainText('角色管理');
});
});