| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- /**
- * ================================================================
- * 场景:能量规则配置全流程 E2E 测试
- * ================================================================
- * 目标:管理员通过 cfc-web Element UI 管理后台配置五维能量发放规则
- * 部署地址:http://cfc.iwintrue.com/admin
- *
- * 流程步骤:
- * 1. 查看能量规则列表(el-table 展示)
- * 2. 创建新规则(el-dialog 表单)
- * 3. 启用/禁用规则(el-switch 切换)
- * 4. 编辑/删除规则
- *
- * 前置条件:已登录管理员账号
- * ================================================================
- */
- const { test, expect } = require('@playwright/test');
- // 共享登录逻辑 — 在每个测试前确保已登录
- async function ensureLoggedIn(page) {
- // 检查是否已在管理后台(有侧边栏即为已登录)
- const sidebar = page.locator('.sidebar-container, .el-menu-vertical-demo, .el-aside');
- if (await sidebar.isVisible({ timeout: 3000 }).catch(() => false)) {
- return;
- }
- // 未登录则跳转登录页
- await page.goto('/login');
- await page.waitForSelector('.el-input__inner', { timeout: 15000 });
- // 填写用户名和密码
- await page.fill('.el-input__inner', 'admin', { timeout: 5000 });
- // 查找密码输入框(第二个)
- const passwordInputs = page.locator('.el-input__inner').filter({ hasPlaceholder: /密码/ });
- if (await passwordInputs.count() > 0) {
- await passwordInputs.first().fill('admin123');
- } else {
- // fallback: 填第二个输入框
- const inputs = page.locator('.el-input__inner');
- const count = await inputs.count();
- if (count >= 2) {
- await inputs.nth(1).fill('admin123');
- }
- }
- // 点击登录按钮
- await page.click('.el-button--primary.login-btn, .el-button[type="primary"]:not(.skip)');
- // 等待登录成功(跳转到 dashboard)
- await page.waitForURL(/\/dashboard|\/admin\/dashboard/, { timeout: 15000 });
- }
- test.describe('【场景流程】能量规则配置全流程', () => {
- test('[场景1] 查看能量规则列表 - 期望返回规则列表', async ({ page }) => {
- await ensureLoggedIn(page);
- await page.goto('/energy-rule');
- await page.waitForLoadState('networkidle');
- // Element UI table
- await expect(page.locator('.el-table')).toBeVisible({ timeout: 10000 });
- // 检查表格有数据行
- const rows = page.locator('.el-table__body tr');
- const count = await rows.count();
- expect(count).toBeGreaterThan(0);
- // 检查维度列
- const dimensionCell = page.locator('.el-table__body td').filter({ hasText: /^身$|^智$|^行$|^心$|^富$/ }).first();
- if (await dimensionCell.isVisible().catch(() => false)) {
- const text = await dimensionCell.textContent();
- expect(text).toBeTruthy();
- }
- });
- test('[场景2] 创建能量规则 - 期望规则创建成功', async ({ page }) => {
- await ensureLoggedIn(page);
- await page.goto('/energy-rule');
- await page.waitForLoadState('networkidle');
- // 点击"新增规则"按钮
- await page.click('el-button:has-text("新增规则"), .el-button:has-text("新增规则")');
- // 等待 dialog 弹出
- await page.waitForSelector('.el-dialog', { timeout: 10000 });
- // 填写规则编码
- await page.fill('input[placeholder="唯一编码,如 DAILY_CHECKIN"]', 'E2E_TEST_RULE');
- // 填写规则名称
- await page.fill('input[placeholder="规则名称"]', 'E2E Test Rule');
- // 选择维度
- const dimSelect = page.locator('.el-select').filter({ has: page.locator('.el-input[placeholder*="维度"]') });
- if (await dimSelect.isVisible({ timeout: 3000 }).catch(() => false)) {
- await dimSelect.click();
- await page.click('.el-select-dropdown__item:has-text("身")');
- }
- // 填写能量值
- const numInput = page.locator('.el-input-number input');
- if (await numInput.isVisible({ timeout: 3000 }).catch(() => false)) {
- await numInput.fill('5');
- }
- // 点击保存
- await page.click('.el-dialog .el-button--primary:has-text("保存")');
- // 等待成功提示
- await expect(page.locator('.el-message--success')).toBeVisible({ timeout: 10000 });
- });
- test('[场景3] 启用/禁用规则 - 期望状态切换成功', async ({ page }) => {
- await ensureLoggedIn(page);
- await page.goto('/energy-rule');
- await page.waitForLoadState('networkidle');
- // 等待表格加载
- await expect(page.locator('.el-table')).toBeVisible({ timeout: 10000 });
- // 点击第一行的开关
- const firstSwitch = page.locator('.el-table__body .el-switch').first();
- if (await firstSwitch.isVisible({ timeout: 5000 }).catch(() => false)) {
- await firstSwitch.click();
- await page.waitForTimeout(1000);
- // 等待成功消息
- const successMsg = page.locator('.el-message--success');
- if (await successMsg.isVisible({ timeout: 5000 }).catch(() => false)) {
- await expect(successMsg).toContainText(/状态已更新/);
- }
- }
- });
- test('[场景4] 编辑/删除规则 - 期望操作成功', async ({ page }) => {
- await ensureLoggedIn(page);
- await page.goto('/energy-rule');
- await page.waitForLoadState('networkidle');
- // 等待表格加载
- await expect(page.locator('.el-table')).toBeVisible({ timeout: 10000 });
- // 点击第一行的编辑按钮
- const firstRow = page.locator('.el-table__body tr').first();
- const editBtn = firstRow.locator('.el-button--primary.el-button--small:has-text("编辑")');
- if (await editBtn.isVisible({ timeout: 5000 }).catch(() => false)) {
- await editBtn.click();
- } else {
- // fallback: 点击第一行任意可交互元素
- await firstRow.click();
- }
- // 等待 dialog 弹出
- await page.waitForSelector('.el-dialog', { timeout: 5000 });
- // 修改能量值为 10
- const numInput = page.locator('.el-dialog .el-input-number input');
- if (await numInput.isVisible({ timeout: 3000 }).catch(() => false)) {
- await numInput.fill('10');
- }
- // 点击保存
- await page.click('.el-dialog .el-button--primary:has-text("保存")');
- // 等待成功提示
- await expect(page.locator('.el-message--success')).toBeVisible({ timeout: 10000 });
- });
- });
|