energy-rules-config.spec.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**
  2. * ================================================================
  3. * 场景:能量规则配置全流程 E2E 测试
  4. * ================================================================
  5. * 目标:管理员通过 cfc-web Element UI 管理后台配置五维能量发放规则
  6. * 部署地址:http://cfc.iwintrue.com/admin
  7. *
  8. * 流程步骤:
  9. * 1. 查看能量规则列表(el-table 展示)
  10. * 2. 创建新规则(el-dialog 表单)
  11. * 3. 启用/禁用规则(el-switch 切换)
  12. * 4. 编辑/删除规则
  13. *
  14. * 前置条件:已登录管理员账号
  15. * ================================================================
  16. */
  17. const { test, expect } = require('@playwright/test');
  18. // 共享登录逻辑 — 在每个测试前确保已登录
  19. async function ensureLoggedIn(page) {
  20. // 检查是否已在管理后台(有侧边栏即为已登录)
  21. const sidebar = page.locator('.sidebar-container, .el-menu-vertical-demo, .el-aside');
  22. if (await sidebar.isVisible({ timeout: 3000 }).catch(() => false)) {
  23. return;
  24. }
  25. // 未登录则跳转登录页
  26. await page.goto('/login');
  27. await page.waitForSelector('.el-input__inner', { timeout: 15000 });
  28. // 填写用户名和密码
  29. await page.fill('.el-input__inner', 'admin', { timeout: 5000 });
  30. // 查找密码输入框(第二个)
  31. const passwordInputs = page.locator('.el-input__inner').filter({ hasPlaceholder: /密码/ });
  32. if (await passwordInputs.count() > 0) {
  33. await passwordInputs.first().fill('admin123');
  34. } else {
  35. // fallback: 填第二个输入框
  36. const inputs = page.locator('.el-input__inner');
  37. const count = await inputs.count();
  38. if (count >= 2) {
  39. await inputs.nth(1).fill('admin123');
  40. }
  41. }
  42. // 点击登录按钮
  43. await page.click('.el-button--primary.login-btn, .el-button[type="primary"]:not(.skip)');
  44. // 等待登录成功(跳转到 dashboard)
  45. await page.waitForURL(/\/dashboard|\/admin\/dashboard/, { timeout: 15000 });
  46. }
  47. test.describe('【场景流程】能量规则配置全流程', () => {
  48. test('[场景1] 查看能量规则列表 - 期望返回规则列表', async ({ page }) => {
  49. await ensureLoggedIn(page);
  50. await page.goto('/energy-rule');
  51. await page.waitForLoadState('networkidle');
  52. // Element UI table
  53. await expect(page.locator('.el-table')).toBeVisible({ timeout: 10000 });
  54. // 检查表格有数据行
  55. const rows = page.locator('.el-table__body tr');
  56. const count = await rows.count();
  57. expect(count).toBeGreaterThan(0);
  58. // 检查维度列
  59. const dimensionCell = page.locator('.el-table__body td').filter({ hasText: /^身$|^智$|^行$|^心$|^富$/ }).first();
  60. if (await dimensionCell.isVisible().catch(() => false)) {
  61. const text = await dimensionCell.textContent();
  62. expect(text).toBeTruthy();
  63. }
  64. });
  65. test('[场景2] 创建能量规则 - 期望规则创建成功', async ({ page }) => {
  66. await ensureLoggedIn(page);
  67. await page.goto('/energy-rule');
  68. await page.waitForLoadState('networkidle');
  69. // 点击"新增规则"按钮
  70. await page.click('el-button:has-text("新增规则"), .el-button:has-text("新增规则")');
  71. // 等待 dialog 弹出
  72. await page.waitForSelector('.el-dialog', { timeout: 10000 });
  73. // 填写规则编码
  74. await page.fill('input[placeholder="唯一编码,如 DAILY_CHECKIN"]', 'E2E_TEST_RULE');
  75. // 填写规则名称
  76. await page.fill('input[placeholder="规则名称"]', 'E2E Test Rule');
  77. // 选择维度
  78. const dimSelect = page.locator('.el-select').filter({ has: page.locator('.el-input[placeholder*="维度"]') });
  79. if (await dimSelect.isVisible({ timeout: 3000 }).catch(() => false)) {
  80. await dimSelect.click();
  81. await page.click('.el-select-dropdown__item:has-text("身")');
  82. }
  83. // 填写能量值
  84. const numInput = page.locator('.el-input-number input');
  85. if (await numInput.isVisible({ timeout: 3000 }).catch(() => false)) {
  86. await numInput.fill('5');
  87. }
  88. // 点击保存
  89. await page.click('.el-dialog .el-button--primary:has-text("保存")');
  90. // 等待成功提示
  91. await expect(page.locator('.el-message--success')).toBeVisible({ timeout: 10000 });
  92. });
  93. test('[场景3] 启用/禁用规则 - 期望状态切换成功', async ({ page }) => {
  94. await ensureLoggedIn(page);
  95. await page.goto('/energy-rule');
  96. await page.waitForLoadState('networkidle');
  97. // 等待表格加载
  98. await expect(page.locator('.el-table')).toBeVisible({ timeout: 10000 });
  99. // 点击第一行的开关
  100. const firstSwitch = page.locator('.el-table__body .el-switch').first();
  101. if (await firstSwitch.isVisible({ timeout: 5000 }).catch(() => false)) {
  102. await firstSwitch.click();
  103. await page.waitForTimeout(1000);
  104. // 等待成功消息
  105. const successMsg = page.locator('.el-message--success');
  106. if (await successMsg.isVisible({ timeout: 5000 }).catch(() => false)) {
  107. await expect(successMsg).toContainText(/状态已更新/);
  108. }
  109. }
  110. });
  111. test('[场景4] 编辑/删除规则 - 期望操作成功', async ({ page }) => {
  112. await ensureLoggedIn(page);
  113. await page.goto('/energy-rule');
  114. await page.waitForLoadState('networkidle');
  115. // 等待表格加载
  116. await expect(page.locator('.el-table')).toBeVisible({ timeout: 10000 });
  117. // 点击第一行的编辑按钮
  118. const firstRow = page.locator('.el-table__body tr').first();
  119. const editBtn = firstRow.locator('.el-button--primary.el-button--small:has-text("编辑")');
  120. if (await editBtn.isVisible({ timeout: 5000 }).catch(() => false)) {
  121. await editBtn.click();
  122. } else {
  123. // fallback: 点击第一行任意可交互元素
  124. await firstRow.click();
  125. }
  126. // 等待 dialog 弹出
  127. await page.waitForSelector('.el-dialog', { timeout: 5000 });
  128. // 修改能量值为 10
  129. const numInput = page.locator('.el-dialog .el-input-number input');
  130. if (await numInput.isVisible({ timeout: 3000 }).catch(() => false)) {
  131. await numInput.fill('10');
  132. }
  133. // 点击保存
  134. await page.click('.el-dialog .el-button--primary:has-text("保存")');
  135. // 等待成功提示
  136. await expect(page.locator('.el-message--success')).toBeVisible({ timeout: 10000 });
  137. });
  138. });