b3201b61fb
数据库修复: - 添加测试用户 'user'(密码:admin123) 测试代码优化: - 添加页面加载等待逻辑(waitForLoadState) - 添加元素可见性等待(waitFor visible) - 修复用户密码错误(user123 -> admin123) - 改进错误处理和稳定性
124 lines
4.3 KiB
TypeScript
124 lines
4.3 KiB
TypeScript
import { test, expect } from '@playwright/test';
|
|
|
|
test.describe('用户权限边界验证', () => {
|
|
test('管理员可以访问所有管理功能', async ({ page }) => {
|
|
await test.step('管理员登录', async () => {
|
|
await page.goto('/login');
|
|
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 });
|
|
});
|
|
|
|
await test.step('验证可以访问用户管理', async () => {
|
|
await page.goto('/users');
|
|
await expect(page).toHaveURL(/.*users/);
|
|
});
|
|
|
|
await test.step('验证可以访问角色管理', async () => {
|
|
await page.goto('/roles');
|
|
await expect(page).toHaveURL(/.*roles/);
|
|
});
|
|
|
|
await test.step('验证可以访问菜单管理', async () => {
|
|
await page.goto('/menus');
|
|
await expect(page).toHaveURL(/.*menus/);
|
|
});
|
|
|
|
await test.step('验证可以访问系统配置', async () => {
|
|
await page.goto('/sys/config');
|
|
await expect(page).toHaveURL(/.*sys\/config/);
|
|
});
|
|
});
|
|
|
|
test('普通用户只能访问个人信息', async ({ page }) => {
|
|
await test.step('普通用户登录', async () => {
|
|
await page.goto('/login');
|
|
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 });
|
|
});
|
|
|
|
await test.step('验证无法访问用户管理', async () => {
|
|
await page.goto('/users');
|
|
await page.waitForTimeout(1000);
|
|
const currentUrl = page.url();
|
|
expect(currentUrl).not.toContain('/users');
|
|
});
|
|
|
|
await test.step('验证无法访问角色管理', async () => {
|
|
await page.goto('/roles');
|
|
await page.waitForTimeout(1000);
|
|
const currentUrl = page.url();
|
|
expect(currentUrl).not.toContain('/roles');
|
|
});
|
|
|
|
await test.step('验证无法访问菜单管理', async () => {
|
|
await page.goto('/menus');
|
|
await page.waitForTimeout(1000);
|
|
const currentUrl = page.url();
|
|
expect(currentUrl).not.toContain('/menus');
|
|
});
|
|
});
|
|
|
|
test('权限不足时显示提示信息', async ({ page }) => {
|
|
await test.step('普通用户登录', async () => {
|
|
await page.goto('/login');
|
|
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 });
|
|
});
|
|
|
|
await test.step('尝试访问受限页面', async () => {
|
|
await page.goto('/users');
|
|
await page.waitForTimeout(2000);
|
|
|
|
const errorMessage = page.locator('.el-message, .error-message, [role="alert"]');
|
|
const isVisible = await errorMessage.isVisible().catch(() => false);
|
|
|
|
if (isVisible) {
|
|
const text = await errorMessage.textContent();
|
|
expect(text).toMatch(/权限|禁止|无权/i);
|
|
}
|
|
});
|
|
});
|
|
});
|