08ea5fbe98
添加用户管理视图、API和状态管理文件
204 lines
6.7 KiB
TypeScript
204 lines
6.7 KiB
TypeScript
import { test, expect } from '@playwright/test';
|
|
import { MockManager } from './mock-manager';
|
|
|
|
test.describe('角色管理', () => {
|
|
test.beforeEach(async ({ page }) => {
|
|
const mockManager = new MockManager({
|
|
enabled: true,
|
|
mode: 'full',
|
|
mockPaths: [],
|
|
delay: 0,
|
|
logCalls: true,
|
|
validateResponses: true,
|
|
dataSource: 'memory'
|
|
});
|
|
|
|
mockManager.presetTestData({
|
|
menus: [
|
|
{
|
|
id: 1,
|
|
name: '仪表盘',
|
|
code: 'dashboard',
|
|
path: '/dashboard',
|
|
icon: 'DashboardOutlined',
|
|
sortOrder: 1,
|
|
status: 'active',
|
|
parentId: 0,
|
|
component: 'views/Dashboard.vue',
|
|
createBy: 'system',
|
|
updateBy: 'system',
|
|
createdAt: '2024-01-01T00:00:00.000Z',
|
|
updatedAt: '2024-01-01T00:00:00.000Z',
|
|
children: []
|
|
},
|
|
{
|
|
id: 3,
|
|
name: '角色管理',
|
|
code: 'role',
|
|
path: '/roles',
|
|
icon: 'LockOutlined',
|
|
sortOrder: 3,
|
|
status: 'active',
|
|
parentId: 0,
|
|
component: 'views/RoleManagement.vue',
|
|
createBy: 'system',
|
|
updateBy: 'system',
|
|
createdAt: '2024-01-01T00:00:00.000Z',
|
|
updatedAt: '2024-01-01T00:00:00.000Z',
|
|
children: []
|
|
}
|
|
],
|
|
roles: [
|
|
{
|
|
id: 1,
|
|
name: '超级管理员',
|
|
code: 'super_admin',
|
|
status: 'active',
|
|
permissions: ['*'],
|
|
createdAt: '2024-01-01T00:00:00.000Z',
|
|
updatedAt: '2024-01-01T00:00:00.000Z'
|
|
},
|
|
{
|
|
id: 2,
|
|
name: '普通用户',
|
|
code: 'user',
|
|
status: 'active',
|
|
permissions: ['user:view', 'user:create'],
|
|
createdAt: '2024-01-01T00:00:00.000Z',
|
|
updatedAt: '2024-01-01T00:00:00.000Z'
|
|
}
|
|
]
|
|
});
|
|
|
|
await mockManager.interceptAPIRequest(page);
|
|
|
|
await page.goto('/');
|
|
await page.waitForLoadState('networkidle');
|
|
await page.waitForTimeout(2000);
|
|
|
|
const usernameInput = page.locator('input[placeholder="请输入用户名"]');
|
|
const passwordInput = page.locator('input[placeholder="请输入密码"]');
|
|
const loginButton = page.locator('button[type="submit"]');
|
|
|
|
await usernameInput.waitFor({ state: 'visible', timeout: 10000 });
|
|
await passwordInput.waitFor({ state: 'visible', timeout: 10000 });
|
|
await loginButton.waitFor({ state: 'visible', timeout: 10000 });
|
|
|
|
await usernameInput.fill('admin');
|
|
await passwordInput.fill('admin123');
|
|
await loginButton.click();
|
|
|
|
await page.waitForURL(/.*dashboard/, { timeout: 10000 });
|
|
});
|
|
|
|
test('应该显示角色列表页面', async ({ page }) => {
|
|
await page.goto('/roles');
|
|
await expect(page.getByText(/角色管理/i)).toBeVisible();
|
|
await expect(page.getByRole('button', { name: /添加角色/i })).toBeVisible();
|
|
await expect(page.getByRole('button', { name: /刷新/i })).toBeVisible();
|
|
});
|
|
|
|
test('应该显示角色数据表格', async ({ page }) => {
|
|
await page.goto('/roles');
|
|
|
|
const table = page.locator('.ant-table');
|
|
await expect(table).toBeVisible();
|
|
|
|
await expect(page.getByText(/角色名称/i)).toBeVisible();
|
|
await expect(page.getByText(/角色编码/i)).toBeVisible();
|
|
await expect(page.getByText(/状态/i)).toBeVisible();
|
|
await expect(page.getByText(/创建时间/i)).toBeVisible();
|
|
});
|
|
|
|
test('应该能够创建新角色', async ({ page }) => {
|
|
await page.goto('/roles');
|
|
|
|
await page.getByRole('button', { name: /添加角色/i }).click();
|
|
await expect(page).toHaveURL(/.*roles\/create/);
|
|
|
|
await page.getByPlaceholder(/请输入角色名称/i).fill('测试角色');
|
|
await page.getByPlaceholder(/请输入角色编码/i).fill('TEST_ROLE');
|
|
await page.getByPlaceholder(/请输入角色描述/i).fill('这是一个测试角色');
|
|
|
|
await page.getByRole('button', { name: /提交/i }).click();
|
|
|
|
await expect(page).toHaveURL(/.*roles/);
|
|
await expect(page.getByText(/创建成功/i)).toBeVisible();
|
|
});
|
|
|
|
test('创建角色时应该验证必填字段', async ({ page }) => {
|
|
await page.goto('/roles');
|
|
await page.getByRole('button', { name: /添加角色/i }).click();
|
|
|
|
await page.getByRole('button', { name: /提交/i }).click();
|
|
|
|
await expect(page.getByText(/请输入角色名称/i)).toBeVisible();
|
|
await expect(page.getByText(/请输入角色编码/i)).toBeVisible();
|
|
});
|
|
|
|
test('应该能够编辑角色', async ({ page }) => {
|
|
await page.goto('/roles');
|
|
|
|
const editButton = page.getByRole('button').filter({ hasText: /编辑/i }).first();
|
|
if (await editButton.isVisible()) {
|
|
await editButton.click();
|
|
await expect(page).toHaveURL(/.*roles\/\d+\/edit/);
|
|
|
|
const roleNameInput = page.getByPlaceholder(/请输入角色名称/i);
|
|
await roleNameInput.clear();
|
|
await roleNameInput.fill('更新后的角色名称');
|
|
|
|
await page.getByRole('button', { name: /提交/i }).click();
|
|
|
|
await expect(page).toHaveURL(/.*roles/);
|
|
await expect(page.getByText(/更新成功/i)).toBeVisible();
|
|
}
|
|
});
|
|
|
|
test('应该能够删除角色', async ({ page }) => {
|
|
await page.goto('/roles');
|
|
|
|
const deleteButton = page.getByRole('button').filter({ hasText: /删除/i }).first();
|
|
if (await deleteButton.isVisible()) {
|
|
await deleteButton.click();
|
|
|
|
await expect(page.getByText(/确认删除/i)).toBeVisible();
|
|
await page.getByRole('button', { name: /确认/i }).click();
|
|
|
|
await expect(page.getByText(/删除成功/i)).toBeVisible();
|
|
}
|
|
});
|
|
|
|
test('应该能够查看角色详情', async ({ page }) => {
|
|
await page.goto('/roles');
|
|
|
|
const detailButton = page.getByRole('button').filter({ hasText: /详情/i }).first();
|
|
if (await detailButton.isVisible()) {
|
|
await detailButton.click();
|
|
await expect(page).toHaveURL(/.*roles\/\d+\/detail/);
|
|
await expect(page.getByText(/角色详情/i)).toBeVisible();
|
|
}
|
|
});
|
|
|
|
test('应该能够刷新角色列表', async ({ page }) => {
|
|
await page.goto('/roles');
|
|
|
|
await page.getByRole('button', { name: /刷新/i }).click();
|
|
|
|
await expect(page.getByText(/刷新成功/i)).toBeVisible();
|
|
});
|
|
|
|
test('应该支持状态切换', async ({ page }) => {
|
|
await page.goto('/roles/create');
|
|
|
|
const statusSelect = page.locator('.ant-select').filter({ hasText: /状态/i });
|
|
await statusSelect.click();
|
|
|
|
await expect(page.getByText(/启用/i)).toBeVisible();
|
|
await expect(page.getByText(/禁用/i)).toBeVisible();
|
|
|
|
await page.getByText(/禁用/i).click();
|
|
await expect(statusSelect).toContainText(/禁用/i);
|
|
});
|
|
});
|