feat: add news management E2E tests

This commit is contained in:
张翔
2026-03-12 22:06:01 +08:00
parent f846b38807
commit 1cd7d30dc2
+144
View File
@@ -0,0 +1,144 @@
import { test, expect } from '../../fixtures/base.fixture';
import { AdminLoginPage, AdminContentPage } from '../../pages/AdminPage';
import { adminTestData, generateTestContent } from '../../data/admin-test-data';
test.describe('新闻动态管理E2E测试', () => {
let loginPage: AdminLoginPage;
let contentPage: AdminContentPage;
test.beforeEach(async ({ page }) => {
loginPage = new AdminLoginPage(page);
contentPage = new AdminContentPage(page);
await loginPage.goto();
await loginPage.login(adminTestData.users.admin.email, adminTestData.users.admin.password);
await expect(async () => {
await page.waitForURL(/\/admin/, { timeout: 10000 });
}).toPass({ timeout: 15000 });
});
test('应该能够创建新闻', async ({ page }) => {
const newsData = generateTestContent('news');
await contentPage.goto();
await contentPage.createContent(newsData);
await expect(page.locator('text=保存成功')).toBeVisible({ timeout: 5000 });
await contentPage.goto();
await contentPage.searchContent(newsData.title);
const newsCount = await contentPage.contentList.count();
expect(newsCount).toBeGreaterThan(0);
});
test('应该能够发布新闻', async ({ page }) => {
await contentPage.goto();
await contentPage.createButton.click();
await page.locator('select[name="type"]').selectOption('news');
const newsTitle = '要发布的新闻-' + Date.now();
await page.locator('input[name="title"]').fill(newsTitle);
await page.locator('input[name="slug"]').fill('published-news-' + Date.now());
await page.getByRole('button', { name: /发布/i }).click();
await expect(page.locator('text=保存成功')).toBeVisible({ timeout: 5000 });
await contentPage.goto();
await contentPage.searchContent(newsTitle);
const newsItem = contentPage.contentList.first();
const statusBadge = await newsItem.locator('span').nth(1).textContent();
expect(statusBadge).toContain('已发布');
});
test('应该能够将新闻设为草稿', async ({ page }) => {
await contentPage.goto();
await contentPage.searchContent('要发布的新闻');
const initialCount = await contentPage.contentList.count();
if (initialCount === 0) {
test.skip(true, '没有找到可编辑的新闻');
}
await contentPage.editContent(0);
await page.locator('select[name="status"]').selectOption('draft');
await page.getByRole('button', { name: /保存/i }).click();
await expect(page.locator('text=保存成功')).toBeVisible({ timeout: 5000 });
await contentPage.goto();
const newsItem = contentPage.contentList.first();
const statusBadge = await newsItem.locator('span').nth(1).textContent();
expect(statusBadge).toContain('草稿');
});
test('应该能够编辑新闻', async ({ page }) => {
await contentPage.goto();
await contentPage.searchContent('测试新闻');
const initialCount = await contentPage.contentList.count();
if (initialCount === 0) {
test.skip(true, '没有找到可编辑的新闻');
}
await contentPage.editContent(0);
const updatedTitle = '更新后的新闻标题-' + Date.now();
await page.locator('input[name="title"]').fill(updatedTitle);
await page.getByRole('button', { name: /保存/i }).click();
await expect(page.locator('text=保存成功')).toBeVisible({ timeout: 5000 });
});
test('应该能够删除新闻', async ({ page }) => {
const newsData = generateTestContent('news');
await contentPage.goto();
await contentPage.createContent(newsData);
await expect(page.locator('text=保存成功')).toBeVisible({ timeout: 5000 });
await contentPage.goto();
await contentPage.searchContent(newsData.title);
const initialCount = await contentPage.contentList.count();
if (initialCount === 0) {
test.skip(true, '没有找到可删除的新闻');
}
await contentPage.deleteContent(0);
await expect(contentPage.contentList).toHaveCount(initialCount - 1, { timeout: 5000 });
});
test('应该能够筛选新闻类型', async ({ page }) => {
await contentPage.goto();
const typeFilter = page.locator('select').first();
await typeFilter.selectOption('news');
await page.waitForTimeout(1000);
const items = await contentPage.contentList.all();
for (const item of items) {
const typeBadge = await item.locator('span').first().textContent();
expect(typeBadge).toContain('新闻');
}
});
test('应该能够按发布状态筛选新闻', async ({ page }) => {
await contentPage.goto();
const statusFilter = page.locator('select').nth(1);
await statusFilter.selectOption('draft');
await page.waitForTimeout(1000);
const items = await contentPage.contentList.all();
for (const item of items) {
const statusBadge = await item.locator('span').nth(1).textContent();
expect(statusBadge).toContain('草稿');
}
});
});