import { test, expect } from '@playwright/test' // 测试账号配置 const TEST_USERNAME = process.env.TEST_USERNAME || 'admin' const TEST_PASSWORD = process.env.TEST_PASSWORD || '123456' test.describe('审计日志测试', () => { test.beforeEach(async ({ page }) => { // 先登录 await page.goto('/login') await page.getByPlaceholder('用户名').fill(TEST_USERNAME) await page.getByPlaceholder('密码').fill(TEST_PASSWORD) await page.getByRole('button', { name: '登录' }).click() // 等待登录成功 await page.waitForURL(/^(?!.*\/login).*$/, { timeout: 15000 }) // 进入审计日志页面 await page.goto('/audit') await page.waitForLoadState('networkidle') }) test('审计日志页面正确显示', async ({ page }) => { // 验证页面元素 await expect(page.locator('.audit-container')).toBeVisible() }) test('显示搜索区域', async ({ page }) => { // 验证搜索卡片存在 await expect(page.locator('.search-card')).toBeVisible() // 验证搜索按钮 await expect(page.getByRole('button', { name: '搜索' })).toBeVisible() await expect(page.getByRole('button', { name: '重置' })).toBeVisible() }) test('显示统计卡片', async ({ page }) => { // 验证统计行存在 await expect(page.locator('.stat-row')).toBeVisible() // 验证统计卡片 await expect(page.getByText('总操作数')).toBeVisible() }) test('显示数据表格', async ({ page }) => { // 验证表格存在 await expect(page.locator('.el-table')).toBeVisible() // 验证表格卡片标题 await expect(page.getByText('审计日志列表')).toBeVisible() // 验证所有表头列 const thead = page.locator('thead') await expect(thead.getByText('时间')).toBeVisible() await expect(thead.getByText('操作用户')).toBeVisible() await expect(thead.getByText('操作类型')).toBeVisible() await expect(thead.getByText('资源类型')).toBeVisible() await expect(thead.getByText('资源 ID')).toBeVisible() await expect(thead.getByText('IP 地址')).toBeVisible() await expect(thead.getByText('详情')).toBeVisible() await expect(thead.getByText('操作', { exact: true })).toBeVisible() // 验证表格卡片内的刷新按钮 await expect(page.locator('.card-header').getByRole('button', { name: '刷新' })).toBeVisible() }) test('刷新功能正常', async ({ page }) => { // 查找刷新按钮 const refreshBtn = page.getByRole('button', { name: '刷新' }) if (await refreshBtn.isVisible()) { await refreshBtn.click() await page.waitForTimeout(1000) // 验证表格仍然存在 await expect(page.locator('.el-table')).toBeVisible() } }) test('搜索功能正常', async ({ page }) => { // 点击搜索 await page.getByRole('button', { name: '搜索' }).click() await page.waitForTimeout(1000) // 验证表格存在 await expect(page.locator('.el-table')).toBeVisible() }) test('重置功能正常', async ({ page }) => { // 点击重置 await page.getByRole('button', { name: '重置' }).click() await page.waitForTimeout(500) // 验证表格存在 await expect(page.locator('.el-table')).toBeVisible() }) test('操作类型筛选器存在', async ({ page }) => { // 验证操作类型选择器(表单标签) await expect(page.locator('.search-card').getByText('操作类型')).toBeVisible() }) test('资源类型筛选器存在', async ({ page }) => { // 验证资源类型选择器(表单标签) await expect(page.locator('.search-card').getByText('资源类型')).toBeVisible() }) test('时间范围筛选器存在', async ({ page }) => { // 验证时间范围选择器 await expect(page.getByText('时间范围')).toBeVisible() }) test('分页功能存在', async ({ page }) => { // 验证分页组件存在 await expect(page.locator('.el-pagination')).toBeVisible() }) test('表格列标题正确', async ({ page }) => { // 验证表格列标题 await expect(page.locator('.el-table')).toContainText('时间') await expect(page.locator('.el-table')).toContainText('操作用户') await expect(page.locator('.el-table')).toContainText('操作类型') await expect(page.locator('.el-table')).toContainText('资源类型') }) })