Sfoglia il codice sorgente

test: 添加管理后台 Playwright E2E 测试脚本

登录→仪表盘→用户管理→提现管理→系统配置全链路覆盖,含路由守卫和错误密码验证

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
liaoxg 5 mesi fa
parent
commit
ce942a5ea2
2 ha cambiato i file con 105 aggiunte e 0 eliminazioni
  1. 88 0
      admin/e2e/admin.spec.ts
  2. 17 0
      admin/e2e/playwright.config.ts

+ 88 - 0
admin/e2e/admin.spec.ts

@@ -0,0 +1,88 @@
+import { test, expect } from '@playwright/test'
+
+const ADMIN_USER = 'admin'
+const ADMIN_PASS = 'admin123'
+
+test.describe('数字能量学管理平台 E2E', () => {
+
+  test('登录 → 仪表盘 → 用户 → 提现 → 配置 全流程', async ({ page }) => {
+    // ===== 1. 登录页 =====
+    await page.goto('/login')
+    await expect(page.locator('.login-title')).toHaveText('数字能量学管理平台')
+
+    // 输入登录信息
+    await page.getByPlaceholder('用户名').fill(ADMIN_USER)
+    await page.getByPlaceholder('密码').fill(ADMIN_PASS)
+    await page.getByRole('button', { name: '登 录' }).click()
+
+    // 等待跳转到仪表盘
+    await page.waitForURL('**/dashboard')
+    // 验证登录成功的 toast
+    await expect(page.getByText('登录成功')).toBeVisible()
+
+    // ===== 2. 仪表盘 =====
+    // 等待加载完成(统计卡片出现)
+    await page.waitForTimeout(1000)
+    const statCards = page.locator('.stat-card')
+    await expect(statCards.first()).toBeVisible()
+    // 应有 4 张统计卡片
+    await expect(statCards).toHaveCount(4)
+
+    // 验证图表容器存在
+    await expect(page.locator('.chart-row')).toBeVisible()
+
+    // 验证最近订单表格存在
+    await expect(page.locator('.recent-orders')).toBeVisible()
+
+    // ===== 3. 用户管理 =====
+    await page.click('text=用户管理')
+    await page.waitForURL('**/users')
+    await page.waitForTimeout(800)
+    // 用户列表应有表格
+    const userTable = page.locator('.el-table')
+    await expect(userTable).toBeVisible()
+
+    // ===== 4. 提现管理 =====
+    await page.click('text=提现管理')
+    await page.waitForURL('**/withdraw')
+    await page.waitForTimeout(800)
+    await expect(page.locator('.el-table')).toBeVisible()
+
+    // ===== 5. 系统配置 =====
+    await page.click('text=系统配置')
+    await page.waitForURL('**/settings')
+    await page.waitForTimeout(800)
+    // 配置页面应有配置卡片
+    await expect(page.locator('.config-card').first()).toBeVisible()
+    // 应有 4 组配置卡片
+    await expect(page.locator('.config-card')).toHaveCount(4)
+
+    // ===== 6. 退出登录 =====
+    // 侧边栏底部应有退出按钮
+    const logoutBtn = page.locator('text=退出登录')
+    if (await logoutBtn.isVisible()) {
+      await logoutBtn.click()
+      // 应跳回登录页
+      await page.waitForURL('**/login')
+      await expect(page.locator('.login-title')).toBeVisible()
+    }
+  })
+
+  test('未登录访问受保护页面应跳转到登录页', async ({ page }) => {
+    // 清除 localStorage 中的 token
+    await page.goto('/dashboard')
+    // 无 token,应被路由守卫拦截跳转到登录页
+    await page.waitForURL('**/login')
+    await expect(page.locator('.login-title')).toHaveText('数字能量学管理平台')
+  })
+
+  test('错误密码应显示提示', async ({ page }) => {
+    await page.goto('/login')
+    await page.getByPlaceholder('用户名').fill(ADMIN_USER)
+    await page.getByPlaceholder('密码').fill('wrong_password')
+    await page.getByRole('button', { name: '登 录' }).click()
+    // 应显示错误消息(使用 Pinia action 中的错误处理或 ElMessage)
+    // 页面应仍然在登录页
+    await expect(page.locator('.login-title')).toBeVisible()
+  })
+})

+ 17 - 0
admin/e2e/playwright.config.ts

@@ -0,0 +1,17 @@
+import { defineConfig } from '@playwright/test'
+
+export default defineConfig({
+  testDir: '.',
+  timeout: 30000,
+  retries: 1,
+  use: {
+    baseURL: 'http://localhost:5173',
+    headless: true,
+    screenshot: 'only-on-failure',
+  },
+  webServer: {
+    command: 'bun run dev',
+    port: 5173,
+    reuseExistingServer: true,
+  },
+})