admin-panel.spec.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**
  2. * ================================================================
  3. * 场景:后台管理全流程 E2E 测试
  4. * ================================================================
  5. * 用户故事:管理员进行用户管理、数据看板、能量规则配置等
  6. *
  7. * 流程步骤:
  8. * 1. 管理员创建用户
  9. * 2. 管理员查看用户列表
  10. * 3. 管理员查看数据看板
  11. * 4. 能量规则CRUD
  12. * 5. 系统健康检查
  13. *
  14. * 关键里程碑:
  15. * - [Milestone-1] 用户创建成功
  16. * - [Milestone-2] 规则管理成功
  17. * ================================================================
  18. *
  19. * 运行:npx playwright test tests/e2e/admin-panel.spec.js
  20. */
  21. const { test, expect } = require('@playwright/test');
  22. test.describe('【场景流程】后台管理全流程', () => {
  23. // ===== 场景1:管理员创建用户 =====
  24. test('[场景1] 管理员创建用户 - 期望创建成功', async ({ page }) => {
  25. // ========== Given:管理员进入用户管理页 ==========
  26. await page.goto('/#/pages/admin/user-list');
  27. await page.waitForLoadState('networkidle');
  28. // ========== When:创建用户 ==========
  29. const createBtn = page.locator('.create-btn, .add-user-btn');
  30. if (await createBtn.isVisible()) {
  31. await createBtn.click();
  32. } else {
  33. await page.goto('/#/pages/admin/user-create');
  34. await page.waitForLoadState('networkidle');
  35. }
  36. await page.waitForSelector('.user-form, .create-form', { timeout: 5000 });
  37. // 填写用户信息
  38. const phoneInput = page.locator('input[name="phone"], .phone-input');
  39. if (await phoneInput.isVisible()) {
  40. await phoneInput.fill('138' + String(Date.now()).slice(-8));
  41. }
  42. const nameInput = page.locator('input[name="nickname"], .name-input');
  43. if (await nameInput.isVisible()) {
  44. await nameInput.fill('测试用户_' + Date.now());
  45. }
  46. // 选择角色
  47. const roleSelect = page.locator('.role-picker, select[name="role"]');
  48. if (await roleSelect.isVisible()) {
  49. await roleSelect.selectOption('parent');
  50. }
  51. // 提交
  52. await page.click('.submit-btn, .save-btn');
  53. // ========== Then:Milestone-1 - 创建成功 ========
  54. await page.waitForSelector('.success-tip, .create-success, .toast-success', { timeout: 10000 });
  55. expect(await page.locator('.success-tip, .create-success').isVisible()).toBeTruthy();
  56. });
  57. // ===== 场景2:管理员查看用户列表 =====
  58. test('[场景2] 管理员查看用户列表 - 期望返回分页列表', async ({ page }) => {
  59. // ========== Given:管理员进入用户列表页 ==========
  60. await page.goto('/#/pages/admin/user-list');
  61. await page.waitForLoadState('networkidle');
  62. // ========== When:查看列表 ==========
  63. await page.waitForSelector('.user-list, .user-item', { timeout: 10000 });
  64. // ========== Then:返回用户列表 ========
  65. const userItems = page.locator('.user-item, .user-row');
  66. const count = await userItems.count();
  67. expect(count).toBeGreaterThan(0);
  68. });
  69. // ===== 场景3:管理员查看数据看板 =====
  70. test('[场景3] 管理员查看数据看板 - 期望返回统计数据', async ({ page }) => {
  71. // ========== Given:管理员进入看板页 ==========
  72. await page.goto('/#/pages/admin/dashboard');
  73. await page.waitForLoadState('networkidle');
  74. // ========== When:查看看板 ==========
  75. await page.waitForSelector('.dashboard-card, .stat-card, .data-overview', { timeout: 10000 });
  76. // ========== Then:返回统计数据 ========
  77. const statCards = page.locator('.stat-card, .dashboard-card');
  78. const count = await statCards.count();
  79. expect(count).toBeGreaterThan(0);
  80. });
  81. // ===== 场景4:能量规则CRUD =====
  82. test('[场景4] 能量规则CRUD - 期望规则管理成功', async ({ page }) => {
  83. // ========== Given:管理员进入能量规则页 ==========
  84. await page.goto('/#/pages/admin/energy-rules');
  85. await page.waitForLoadState('networkidle');
  86. // ========== When:创建规则 ==========
  87. const createBtn = page.locator('.create-btn, .add-rule-btn');
  88. if (await createBtn.isVisible()) {
  89. await createBtn.click();
  90. } else {
  91. await page.goto('/#/pages/admin/energy-rule-create');
  92. await page.waitForLoadState('networkidle');
  93. }
  94. await page.waitForSelector('.rule-form', { timeout: 5000 });
  95. // 填写规则信息
  96. const nameInput = page.locator('input[name="name"], .rule-name-input');
  97. if (await nameInput.isVisible()) {
  98. await nameInput.fill('测试规则_' + Date.now());
  99. }
  100. const valueInput = page.locator('input[name="energyValue"], .value-input');
  101. if (await valueInput.isVisible()) {
  102. await valueInput.fill('5');
  103. }
  104. await page.click('.submit-btn, .save-btn');
  105. // ========== Then:Milestone-2 - 规则创建成功 ========
  106. await page.waitForSelector('.success-tip, .rule-created', { timeout: 10000 });
  107. expect(await page.locator('.success-tip, .rule-created').isVisible()).toBeTruthy();
  108. });
  109. // ===== 场景5:系统健康检查 =====
  110. test('[场景5] 系统健康检查 - 期望返回健康状态', async ({ page }) => {
  111. // ========== Given:管理员进入健康检查页 ==========
  112. await page.goto('/#/pages/admin/health-check');
  113. await page.waitForLoadState('networkidle');
  114. // ========== When:执行检查 ==========
  115. const checkBtn = page.locator('.check-btn, .run-check-btn');
  116. if (await checkBtn.isVisible()) {
  117. await checkBtn.click();
  118. }
  119. // ========== Then:返回健康状态 ========
  120. await page.waitForSelector('.health-status, .check-result', { timeout: 15000 });
  121. const statusBadge = page.locator('.status-badge, .health-badge');
  122. if (await statusBadge.isVisible()) {
  123. const statusText = await statusBadge.textContent();
  124. expect(statusText).toBeTruthy();
  125. }
  126. });
  127. });