| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- /**
- * ================================================================
- * 场景:文章管理 E2E 测试
- * ================================================================
- * 目标:管理员通过 cfc-web Element UI 管理后台管理文章
- * 部署地址:http://localhost:8082
- *
- * 路由映射:
- * 文章列表 → /article-manage
- * 文章分类 → /article-categories
- * 知识标签 → /knowledge-tags
- * ================================================================
- *
- * v11 更新:文章工作流状态机
- * draft → submitForReview → pending → published
- * published → withdraw → withdrawn
- * rejected → reDraft → draft
- */
- 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 {
- 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)');
- await page.waitForURL(/\/dashboard|\/admin\/dashboard/, { timeout: 15000 });
- }
- test.describe('【场景流程】文章管理全流程', () => {
- test('[场景1] 管理员查看文章列表 - 期望返回文章列表', async ({ page }) => {
- await ensureLoggedIn(page);
- await page.goto('/article-manage');
- await page.waitForLoadState('networkidle');
- await expect(page.locator('.el-table')).toBeVisible({ timeout: 10000 });
- const rows = page.locator('.el-table__body tr');
- const count = await rows.count();
- expect(count).toBeGreaterThanOrEqual(0);
- });
- test('[场景2] 管理员管理文章分类 - 期望分类页面可用', async ({ page }) => {
- await ensureLoggedIn(page);
- await page.goto('/article-categories');
- await page.waitForLoadState('networkidle');
- await expect(page.locator('.el-table, .category-list')).toBeVisible({ timeout: 10000 });
- });
- test('[场景3] 管理员查看知识标签', async ({ page }) => {
- await ensureLoggedIn(page);
- await page.goto('/knowledge-tags');
- await page.waitForLoadState('networkidle');
- await expect(page.locator('.el-table')).toBeVisible({ timeout: 10000 });
- });
- // ================================================================
- // v11 新增:文章工作流测试(需前端运行后执行)
- // ================================================================
- test('[场景4] 创建草稿文章 → 提交审核 → 发布(需审核流程完成)', async ({ page }) => {
- await ensureLoggedIn(page);
- await page.goto('/article-edit');
- await page.waitForLoadState('networkidle');
- // 填写文章基本信息
- const titleInput = page.locator('input').first();
- await titleInput.fill('E2E_自动测试文章_' + Date.now());
- // 点击保存为草稿(不发布)
- const saveBtn = page.locator('button').filter({ hasText: /保存|发布/ }).first();
- await saveBtn.click();
- await page.waitForTimeout(1000);
- // 找到提交审核按钮并点击(如果有)
- const submitBtn = page.locator('button').filter({ hasText: /提交审核/ });
- if (await submitBtn.isVisible({ timeout: 2000 }).catch(() => false)) {
- await submitBtn.click();
- await page.waitForTimeout(1000);
- }
- // 验证页面无报错
- await expect(page.locator('.el-message--error, .el-form-item__error')).not.toBeVisible();
- });
- test('[场景5] 查看已发布文章 → 撤回(withdraw)按钮可用', async ({ page }) => {
- await ensureLoggedIn(page);
- await page.goto('/article-manage');
- await page.waitForLoadState('networkidle');
- // 查找已发布状态的文章行
- const publishedRows = page.locator('.el-table__row').filter({ hasText: 'published' });
- if (await publishedRows.count() > 0) {
- // 撤回按钮应该可见
- const withdrawBtn = page.locator('button').filter({ hasText: /撤回/ });
- const isVisible = await withdrawBtn.isVisible({ timeout: 2000 }).catch(() => false);
- if (isVisible) {
- await withdrawBtn.first().click();
- await page.waitForTimeout(1000);
- }
- }
- // 只要页面不报错即通过
- await expect(page.locator('.el-message--error')).not.toBeVisible();
- });
- });
|