更新说明文档
大石头 authored at 2022-04-16 15:30:05
2.06 KiB
YuQue
import { test, expect } from '@playwright/test';
import { collectErrors, expectNoErrors } from './helpers';

/** 分类列表页走查:7 个分类 /{code} 与分页 */
const CATS = ['core', 'xcode', 'cube', 'blood', 'iot', 'tech', 'events'];

test.describe('分类列表页', () => {
  test.beforeEach(async () => { await new Promise((r) => setTimeout(r, 300)); });

  for (const code of CATS) {
    test(`/${code} 加载:面包屑/分类头/文章卡片/无错误`, async ({ page }) => {
      const errors = collectErrors(page);
      const resp = await page.goto(`/${code}`);
      expect(resp!.status(), `/${code} 返回 ${resp!.status()}`).toBe(200);
      await expect(page.locator('.crumb')).toBeVisible();
      await expect(page.locator('.page-head h1')).not.toBeEmpty();
      // 列表有数据或空态友好提示(二选一)
      const hasCard = await page.locator('.article-card').count();
      expect(hasCard, `/${code} 既无文章卡片也无空态提示`).toBeGreaterThan(0);
      // 卡片链接有效
      const bad = await page.locator('.article-card').evaluateAll((as) =>
        as.map((a) => a.getAttribute('href')).filter((h) => !h || h === '#'),
      );
      expect(bad, `/${code} 存在无效卡片链接`).toEqual([]);
      expectNoErrors(errors);
    });
  }

  test('列表页无前端错误 + 分类头统计文案', async ({ page }) => {
    await page.goto('/core');
    await expect(page.locator('.page-head .desc, .page-head h1').first()).toBeVisible();
    expectNoErrors(collectErrors(page));
  });

  test('多页分类:分页控件可用且第 2 页可达', async ({ page }) => {
    await page.goto('/core');
    const pager = page.locator('.pagination');
    if ((await pager.count()) > 0) {
      const next = page.locator('.pagination a[aria-label="下一页"], .pagination .next a');
      if ((await next.count()) > 0) {
        await next.first().click();
        await page.waitForLoadState('load');
        expect(page.url()).toContain('-2');
        await expect(page.locator('.article-card').first()).toBeVisible();
      }
    }
  });
});