import { test, expect, Page } from '@playwright/test'; test.describe('用户管理集成测试', () => { test.beforeEach(async ({ page }) => { await page.goto('/login'); await page.fill('[data-testid="username-input"] input', 'admin'); await page.fill('[data-testid="password-input"] input', 'admin123456'); await page.click('[data-testid="login-button"]'); await expect(page).toHaveURL(/.*\//, { timeout: 15000 }); await page.goto('/system/user'); await page.waitForLoadState('networkidle'); }); test('用户列表页面 - 应正确显示', async ({ page }) => { await expect(page.locator('[data-testid="user-management-container"]')).toBeVisible(); await expect(page.locator('[data-testid="user-table"]')).toBeVisible(); await expect(page.locator('[data-testid="add-user-button"]')).toBeVisible(); }); test('搜索用户 - 按用户名搜索', async ({ page }) => { await page.fill('[data-testid="search-username-input"] input', 'admin'); await page.click('button:has-text("搜索")'); await page.waitForTimeout(1000); const rows = await page.locator('[data-testid="user-table"] tbody tr').count(); expect(rows).toBeGreaterThanOrEqual(1); }); test('新增用户 - 成功', async ({ page }) => { const timestamp = Date.now(); await page.click('[data-testid="add-user-button"]'); await expect(page.locator('.el-dialog')).toBeVisible(); await page.fill('[data-testid="username-input"] input', `testuser_${timestamp}`); await page.fill('[data-testid="email-input"] input', `test_${timestamp}@example.com`); await page.fill('[data-testid="phone-input"] input', '13800138000'); await page.fill('[data-testid="password-input"] input', 'Test@123456'); await page.click('[data-testid="submit-button"]'); await expect(page.locator('.el-message--success')).toBeVisible({ timeout: 10000 }); }); test('编辑用户 - 成功', async ({ page }) => { const rows = await page.locator('[data-testid="user-table"] tbody tr').count(); if (rows > 0) { await page.locator('[data-testid="user-table"] tbody tr').first() .locator('button:has-text("编辑")').click(); await expect(page.locator('.el-dialog')).toBeVisible(); await page.fill('[data-testid="email-input"] input', `updated_${Date.now()}@example.com`); await page.click('[data-testid="submit-button"]'); await expect(page.locator('.el-message--success')).toBeVisible({ timeout: 10000 }); } }); test('查看用户详情 - 成功', async ({ page }) => { const rows = await page.locator('[data-testid="user-table"] tbody tr').count(); if (rows > 0) { await page.locator('[data-testid="user-table"] tbody tr').first() .locator('button:has-text("查看")').click(); await expect(page.locator('.el-drawer')).toBeVisible(); await expect(page.locator('.el-descriptions')).toBeVisible(); } }); test('删除用户 - 成功', async ({ page }) => { const timestamp = Date.now(); await page.click('[data-testid="add-user-button"]'); await page.fill('[data-testid="username-input"] input', `delete_test_${timestamp}`); await page.fill('[data-testid="email-input"] input', `delete_${timestamp}@example.com`); await page.fill('[data-testid="phone-input"] input', '13900139000'); await page.fill('[data-testid="password-input"] input', 'Test@123456'); await page.click('[data-testid="submit-button"]'); await expect(page.locator('.el-message--success')).toBeVisible({ timeout: 10000 }); await page.fill('[data-testid="search-username-input"] input', `delete_test_${timestamp}`); await page.click('button:has-text("搜索")'); await page.waitForTimeout(1000); await page.locator('[data-testid="user-table"] tbody tr').first() .locator('button:has-text("删除")').click(); await page.click('.el-popconfirm button:has-text("确定")'); await expect(page.locator('.el-message--success')).toBeVisible({ timeout: 10000 }); }); test('分页功能 - 应正确切换页码', async ({ page }) => { const pagination = page.locator('.el-pagination'); await expect(pagination).toBeVisible(); const totalText = await pagination.locator('.el-pagination__total').textContent(); expect(totalText).toMatch(/共 \d+ 条/); }); test('表单验证 - 必填字段验证', async ({ page }) => { await page.click('[data-testid="add-user-button"]'); await page.click('[data-testid="submit-button"]'); await expect(page.locator('.el-form-item__error')).toBeVisible(); }); }); test.describe('用户管理权限测试', () => { test('无权限用户不应看到新增按钮', async ({ page, context }) => { await context.clearCookies(); await page.evaluate(() => localStorage.clear()); await page.goto('/login'); await page.fill('[data-testid="username-input"] input', 'testuser'); await page.fill('[data-testid="password-input"] input', 'test123456'); await page.click('[data-testid="login-button"]'); await page.goto('/system/user'); const addButton = page.locator('[data-testid="add-user-button"]'); const isVisible = await addButton.isVisible().catch(() => false); expect(isVisible).toBe(false); }); });