| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- /**
- * ================================================================
- * 场景:后台管理全流程 E2E 测试
- * ================================================================
- * 用户故事:管理员进行用户管理、数据看板、能量规则配置等
- *
- * 流程步骤:
- * 1. 管理员创建用户
- * 2. 管理员查看用户列表
- * 3. 管理员查看数据看板
- * 4. 能量规则CRUD
- * 5. 系统健康检查
- *
- * 关键里程碑:
- * - [Milestone-1] 用户创建成功
- * - [Milestone-2] 规则管理成功
- * ================================================================
- *
- * 运行:npx playwright test tests/e2e/admin-panel.spec.js
- */
- const { test, expect } = require('@playwright/test');
- test.describe('【场景流程】后台管理全流程', () => {
- // ===== 场景1:管理员创建用户 =====
- test('[场景1] 管理员创建用户 - 期望创建成功', async ({ page }) => {
- // ========== Given:管理员进入用户管理页 ==========
- await page.goto('/#/pages/admin/user-list');
- await page.waitForLoadState('networkidle');
- // ========== When:创建用户 ==========
- const createBtn = page.locator('.create-btn, .add-user-btn');
- if (await createBtn.isVisible()) {
- await createBtn.click();
- } else {
- await page.goto('/#/pages/admin/user-create');
- await page.waitForLoadState('networkidle');
- }
- await page.waitForSelector('.user-form, .create-form', { timeout: 5000 });
- // 填写用户信息
- const phoneInput = page.locator('input[name="phone"], .phone-input');
- if (await phoneInput.isVisible()) {
- await phoneInput.fill('138' + String(Date.now()).slice(-8));
- }
- const nameInput = page.locator('input[name="nickname"], .name-input');
- if (await nameInput.isVisible()) {
- await nameInput.fill('测试用户_' + Date.now());
- }
- // 选择角色
- const roleSelect = page.locator('.role-picker, select[name="role"]');
- if (await roleSelect.isVisible()) {
- await roleSelect.selectOption('parent');
- }
- // 提交
- await page.click('.submit-btn, .save-btn');
- // ========== Then:Milestone-1 - 创建成功 ========
- await page.waitForSelector('.success-tip, .create-success, .toast-success', { timeout: 10000 });
- expect(await page.locator('.success-tip, .create-success').isVisible()).toBeTruthy();
- });
- // ===== 场景2:管理员查看用户列表 =====
- test('[场景2] 管理员查看用户列表 - 期望返回分页列表', async ({ page }) => {
- // ========== Given:管理员进入用户列表页 ==========
- await page.goto('/#/pages/admin/user-list');
- await page.waitForLoadState('networkidle');
- // ========== When:查看列表 ==========
- await page.waitForSelector('.user-list, .user-item', { timeout: 10000 });
- // ========== Then:返回用户列表 ========
- const userItems = page.locator('.user-item, .user-row');
- const count = await userItems.count();
- expect(count).toBeGreaterThan(0);
- });
- // ===== 场景3:管理员查看数据看板 =====
- test('[场景3] 管理员查看数据看板 - 期望返回统计数据', async ({ page }) => {
- // ========== Given:管理员进入看板页 ==========
- await page.goto('/#/pages/admin/dashboard');
- await page.waitForLoadState('networkidle');
- // ========== When:查看看板 ==========
- await page.waitForSelector('.dashboard-card, .stat-card, .data-overview', { timeout: 10000 });
- // ========== Then:返回统计数据 ========
- const statCards = page.locator('.stat-card, .dashboard-card');
- const count = await statCards.count();
- expect(count).toBeGreaterThan(0);
- });
- // ===== 场景4:能量规则CRUD =====
- test('[场景4] 能量规则CRUD - 期望规则管理成功', async ({ page }) => {
- // ========== Given:管理员进入能量规则页 ==========
- await page.goto('/#/pages/admin/energy-rules');
- await page.waitForLoadState('networkidle');
- // ========== When:创建规则 ==========
- const createBtn = page.locator('.create-btn, .add-rule-btn');
- if (await createBtn.isVisible()) {
- await createBtn.click();
- } else {
- await page.goto('/#/pages/admin/energy-rule-create');
- await page.waitForLoadState('networkidle');
- }
- await page.waitForSelector('.rule-form', { timeout: 5000 });
- // 填写规则信息
- const nameInput = page.locator('input[name="name"], .rule-name-input');
- if (await nameInput.isVisible()) {
- await nameInput.fill('测试规则_' + Date.now());
- }
- const valueInput = page.locator('input[name="energyValue"], .value-input');
- if (await valueInput.isVisible()) {
- await valueInput.fill('5');
- }
- await page.click('.submit-btn, .save-btn');
- // ========== Then:Milestone-2 - 规则创建成功 ========
- await page.waitForSelector('.success-tip, .rule-created', { timeout: 10000 });
- expect(await page.locator('.success-tip, .rule-created').isVisible()).toBeTruthy();
- });
- // ===== 场景5:系统健康检查 =====
- test('[场景5] 系统健康检查 - 期望返回健康状态', async ({ page }) => {
- // ========== Given:管理员进入健康检查页 ==========
- await page.goto('/#/pages/admin/health-check');
- await page.waitForLoadState('networkidle');
- // ========== When:执行检查 ==========
- const checkBtn = page.locator('.check-btn, .run-check-btn');
- if (await checkBtn.isVisible()) {
- await checkBtn.click();
- }
- // ========== Then:返回健康状态 ========
- await page.waitForSelector('.health-status, .check-result', { timeout: 15000 });
- const statusBadge = page.locator('.status-badge, .health-badge');
- if (await statusBadge.isVisible()) {
- const statusText = await statusBadge.textContent();
- expect(statusText).toBeTruthy();
- }
- });
- });
|