import { test, expect } from '@playwright/test'; test.describe('管理员完整工作流', () => { test.describe.configure({ mode: 'serial' }); const timestamp = Date.now(); const roleName = `测试角色_${timestamp}`; const roleKey = `test_role_${timestamp}`; const username = `testuser_${timestamp}`; test('创建角色并分配权限', async ({ page }) => { await test.step('导航到角色管理', async () => { await page.goto('/dashboard'); await page.waitForLoadState('networkidle'); await page.locator('text=系统管理').click(); await page.waitForTimeout(500); await page.locator('text=角色管理').click(); await page.waitForLoadState('networkidle'); await expect(page).toHaveURL(/.*roles/, { timeout: 10000 }); }); await test.step('点击创建角色按钮', async () => { await page.locator('button:has-text("新增角色")').click(); await page.waitForSelector('.ant-modal', { state: 'visible', timeout: 5000 }); }); await test.step('填写角色信息', async () => { const dialog = page.locator('.ant-modal'); await dialog.locator('input').first().fill(roleName); await dialog.locator('input').nth(1).fill(roleKey); await dialog.locator('.ant-input-number .ant-input__inner').fill('99'); }); await test.step('提交表单', async () => { await page.locator('.ant-modal button:has-text("确定")').click(); await page.waitForSelector('.ant-modal', { state: 'hidden', timeout: 10000 }); await expect(page.locator('.ant-message-success')).toBeVisible({ timeout: 5000 }); }); }); test('创建用户并分配角色', async ({ page }) => { await test.step('导航到用户管理', async () => { await page.goto('/dashboard'); await page.waitForLoadState('networkidle'); await page.locator('text=系统管理').click(); await page.waitForTimeout(500); await page.locator('text=用户管理').click(); await page.waitForLoadState('networkidle'); await expect(page).toHaveURL(/.*users/, { timeout: 10000 }); }); await test.step('点击创建用户按钮', async () => { await page.locator('button:has-text("新增用户")').click(); await page.waitForSelector('.ant-modal', { state: 'visible', timeout: 5000 }); }); await test.step('填写用户信息', async () => { const dialog = page.locator('.ant-modal'); await dialog.locator('input').first().fill(username); await dialog.locator('input[type="password"]').fill('Test@123'); await dialog.locator('input').nth(2).fill(`测试用户${timestamp}`); await dialog.locator('input').nth(3).fill(`test_${timestamp}@example.com`); await dialog.locator('input').nth(4).fill('13800138000'); }); await test.step('提交表单', async () => { await page.locator('.ant-modal button:has-text("确定")').click(); await page.waitForSelector('.ant-modal', { state: 'hidden', timeout: 10000 }); await expect(page.locator('.ant-message-success')).toBeVisible({ timeout: 5000 }); }); await test.step('搜索新创建的用户', async () => { await page.waitForTimeout(1000); const searchInput = page.locator('input[placeholder*="搜索"]'); await searchInput.waitFor({ state: 'visible', timeout: 5000 }); await searchInput.fill(username); await page.locator('button:has-text("搜索")').click(); await page.waitForLoadState('networkidle'); await page.waitForTimeout(2000); }); await test.step('分配角色', async () => { const userRow = page.locator(`tr:has-text("${username}")`); await expect(userRow).toBeVisible({ timeout: 10000 }); await userRow.locator('button:has-text("分配角色")').click(); await page.waitForSelector('.ant-modal:has-text("分配角色")', { state: 'visible', timeout: 5000 }); const transfer = page.locator('.ant-transfer'); const leftPanel = transfer.locator('.ant-transfer-list').first(); const rightPanel = transfer.locator('.ant-transfer-list').last(); const rightPanelItems = await rightPanel.locator('.ant-checkbox').all(); let hasSuperAdminRole = false; for (const item of rightPanelItems) { const text = await item.textContent(); if (text?.includes('超级管理员')) { hasSuperAdminRole = true; break; } } if (!hasSuperAdminRole) { const leftPanelItems = await leftPanel.locator('.ant-checkbox').all(); let superAdminCheckbox = null; for (const item of leftPanelItems) { const text = await item.textContent(); if (text?.includes('超级管理员')) { superAdminCheckbox = item; break; } } if (superAdminCheckbox) { const isChecked = await superAdminCheckbox.locator('input').isChecked(); if (!isChecked) { await superAdminCheckbox.click(); await page.waitForTimeout(500); } const moveToRightButton = transfer.locator('.ant-transfer-operation button').nth(1); if (await moveToRightButton.isEnabled()) { await moveToRightButton.click(); await page.waitForTimeout(500); } } } await page.locator('.ant-modal:has-text("分配角色") button:has-text("确定")').click(); await page.waitForSelector('.ant-modal:has-text("分配角色")', { state: 'hidden', timeout: 10000 }); await expect(page.locator('.ant-message-success').last()).toBeVisible({ timeout: 5000 }); }); }); test('验证新用户登录', async ({ page }) => { await test.step('管理员登出', async () => { await page.goto('/dashboard'); await page.waitForLoadState('networkidle'); const avatarButton = page.locator('.ant-avatar').first(); await avatarButton.click({ timeout: 10000 }); await page.waitForTimeout(500); await page.locator('text=退出登录').click(); await page.waitForURL(/.*login/, { timeout: 10000 }); }); await test.step('新用户登录', async () => { await page.goto('/login'); await page.locator('input[placeholder*="用户名"]').fill(username); await page.locator('input[placeholder*="密码"]').fill('Test@123'); await page.locator('button:has-text("登录")').click(); await page.waitForURL('**/dashboard', { timeout: 30000 }); }); await test.step('验证用户已登录', async () => { await expect(page).toHaveURL(/.*dashboard/); }); }); test.skip('清理测试数据', async ({ page }) => { await test.step('管理员重新登录', async () => { await page.goto('/dashboard'); await page.waitForLoadState('networkidle'); const avatarButton = page.locator('.ant-avatar').first(); if (await avatarButton.isVisible()) { await avatarButton.click(); await page.waitForTimeout(500); await page.locator('text=退出登录').click(); } await page.goto('/login'); await page.locator('input[placeholder*="用户名"]').fill('admin'); await page.locator('input[placeholder*="密码"]').fill('Test@123'); await page.locator('button:has-text("登录")').click(); await page.waitForURL('**/dashboard'); }); await test.step('删除测试用户', async () => { await page.goto('/users'); await page.locator('input[placeholder*="搜索"]').fill(username); await page.locator('button:has-text("搜索")').click(); await page.waitForTimeout(1000); await page.locator('button:has-text("删除")').first().click(); await page.locator('button:has-text("确定")').click(); await expect(page.locator('.ant-message-success')).toBeVisible({ timeout: 5000 }); }); await test.step('删除测试角色', async () => { await page.goto('/roles'); await page.locator('input[placeholder*="搜索"]').fill(roleName); await page.locator('button:has-text("搜索")').click(); await page.waitForTimeout(1000); await page.locator('button:has-text("删除")').first().click(); await page.locator('button:has-text("确定")').click(); await expect(page.locator('.ant-message-success')).toBeVisible({ timeout: 5000 }); }); }); });