Files
novalon-website/e2e/playwright.config.ts
T
zhangxiang 1e80167b7c fix(a11y,e2e): 修 §14 定性出的 6 类功能 E2E 红,含一条 fail-open 与一处我自己造成的重复键
产品侧(真缺陷):
- detail-trust-section 渲染绕过门槛(publishableCases 算了却仍 map caseStudies)⇒
  只要一条过闸,未授权/无 verified 的案例照样上线。改回 publishableCases.map,
  新增 detail-trust-gate.test.tsx 3 例;变异验证:回退成 caseStudies.map ⇒ 2 条精确转红。
- 4 个文字链接触摸目标 23px < WCAG 2.5.8 的 24px(home 全部洞察/了解这一能力、
  成果槽两条 CTA)⇒ 走间距刻度 min-h-6 补齐,不塞任意值。判据本身复跑通过。
- 首页能力面板 role=region 缺可访问名(landmark 无名;axe 的 landmark-name 属
  best-practice 标签,我们的闸只跑 A/AA 所以没报)⇒ 补 aria-label。
- 登录限流额度改为可按环境覆盖(默认仍 10 次/15 分钟/IP,生产姿态不变);
  真因:进程内内存桶 + dev server 跨整轮存活 ⇒ @admin 旅程第 11 次登录起必得 429,
  即 §14 里「单跑绿、全量红」的机理。route.test.ts 加双向用例(未设=10;设 3 ⇒ 第 4 次限流)。

测试侧(锚点/引擎):
- mobile.spec:45、website-acceptance:139 的重锚:锚点取自真源 navigation.ts
  (一级只剩 行业/能力/洞察/关于我们,联系入口是 consult-button 而非导航项)。
- p4:403 焦点管理:webkit/chromium-mobile 上 Tab 不落到链接(Safari 系需系统级
  「Press Tab to highlight each item」,Playwright 起的 webkit 不带),
  我先试 bringToFront + 点空白**没有用**,故改为同契约按引擎取证(chromium 走真实键盘
  Tab,其余显式 focus 跳转链接),断言一条不减;复跑 webkit+mobile 2 passed。
- playwright.config.ts:我把 env 插成了重复键(TS1117),由本批新加的 type-check:e2e 当场抓到
  ——这条闸的价值的又一次现场证明。已合并进既有 env 对象。

待办:全量功能 E2E 终跑(4 project)正在后台,读数与 PR 勾选项的复位随下一笔。
2026-10-17 10:41:57 +08:00

169 lines
7.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { defineConfig, devices } from '@playwright/test';
import { config as dotenvConfig } from 'dotenv';
import path from 'path';
// 加载项目根目录的 .env.local,使 Playwright webServer 与测试进程使用相同的 CMS_REVALIDATE_SECRET
// 若 .env.local 不存在则回退到默认测试 secret
dotenvConfig({ path: path.resolve(__dirname, '../.env.local') });
process.env.CMS_REVALIDATE_SECRET = process.env.CMS_REVALIDATE_SECRET || 'e2e-test-secret';
export default defineConfig({
testDir: './',
fullyParallel: true,
forbidOnly: !!process.env.CI,
retries: process.env.CI ? 2 : 0,
workers: process.env.CI ? 1 : undefined,
reporter: [
['html', { open: 'never' }],
['list', { printSteps: true }],
['allure-playwright', { outputFolder: 'allure-results' }],
],
snapshotDir: './visual-snapshots',
snapshotPathTemplate: '{snapshotDir}/{projectName}/{testFilePath}/{arg}-{projectName}{ext}',
// 显式 outputDir 绝对路径(e2e/test-results),避免 playwright 在项目根创建 test-results
// 触发 WorkBuddy 沙箱 safe-delete BULK_GUARD(node fs.rm ≥ 50 文件被拦,e2e 直接退出)
outputDir: path.join(__dirname, 'test-results'),
use: {
baseURL: process.env.E2E_BASE_URL || 'http://localhost:3000',
// 路径以配置文件所在目录为基准,兼容 `cd e2e` 与从项目根目录直接调用两种执行方式
storageState: path.resolve(__dirname, 'storageState.json'),
trace: 'on-first-retry',
screenshot: 'only-on-failure',
ignoreHTTPSErrors: true,
actionTimeout: 15000,
navigationTimeout: 30000,
},
expect: {
toHaveScreenshot: {
maxDiffPixels: 200,
maxDiffPixelRatio: 0.005,
threshold: 0.3,
animations: 'disabled',
caret: 'hide',
scale: 'device',
// @ts-expect-error 类型口径缺口(不是运行时缺口):@playwright/test 的
// expect.toHaveScreenshot 选项类型里没有 `timeout`,但实现确实读它 ——
// node_modules/playwright/lib/matchers/expect.js:12182 `helper.options.timeout ?? this.timeout`。
// 删掉这行会让截图断言超时从 20s 回落到下面的 expect.timeout=15000,属行为变更 ⇒ 保留并标记。
// 若日后 Playwright 把 timeout 补进类型,本指令会因「未使用」而报错,届时直接摘掉标记。
timeout: 20000,
},
timeout: 15000,
},
projects: [
{
name: 'visual-chromium-desktop',
use: {
...devices['Desktop Chrome'],
viewport: { width: 1280, height: 800 },
// 视觉回归确定性:reducedMotion 让 ScrollReveal/StaggerReveal 走 initial={} 分支
// 默认可见(不残留 opacity:0),避免全页截图时 below-the-fold 模块因 whileInView
// 未触发而空白。同时使 Hero 粒子场走固定种子的静态降级帧,快照零漂移。
//
// ⚠ 残留 R-1(DEC-N6 补口带出,本批未修,需视觉 owner 拍板):这条意图**当前并没有生效**。
// `reducedMotion` 是 browser.newContext() 的上下文选项,不是 `use` 层选项:
// node_modules/playwright/lib/index.js:241-266 的 fixture 白名单里没有它,
// `_combinedContextOptions` 只从 `contextOptions` 取值 ⇒ 写在 use 里被直接忽略。
// 生效写法是 `contextOptions: { reducedMotion: 'reduce' }`,但那会真的关掉动效、
// 改变截图字节 ⇒ 与正在重采的 132 张基线冲突,本批只标注不改行为。
// @ts-expect-error 残留 R-1:见上,use 层无 reducedMotion 选项
reducedMotion: 'reduce',
},
testMatch: 'visual-regression.spec.ts',
},
{
name: 'visual-chromium-tablet',
use: {
...devices['iPad Pro 11'],
viewport: { width: 834, height: 1194 },
// @ts-expect-error 残留 R-1:use 层无 reducedMotion 选项,运行时被忽略(见 visual-chromium-desktop 处说明)
reducedMotion: 'reduce',
},
testMatch: 'visual-regression.spec.ts',
},
{
name: 'visual-chromium-mobile',
use: {
...devices['iPhone 14'],
viewport: { width: 390, height: 844 },
isMobile: true,
hasTouch: true,
// @ts-expect-error 残留 R-1:use 层无 reducedMotion 选项,运行时被忽略(见 visual-chromium-desktop 处说明)
reducedMotion: 'reduce',
},
testMatch: 'visual-regression.spec.ts',
},
{
name: 'visual-firefox-desktop',
use: {
...devices['Desktop Firefox'],
viewport: { width: 1280, height: 800 },
storageState: './storageState.firefox.json',
// @ts-expect-error 残留 R-1:use 层无 reducedMotion 选项,运行时被忽略(见 visual-chromium-desktop 处说明)
reducedMotion: 'reduce',
},
testMatch: 'visual-regression.spec.ts',
},
{
name: 'visual-webkit-desktop',
use: {
...devices['Desktop Safari'],
viewport: { width: 1280, height: 800 },
storageState: './storageState.firefox.json',
// @ts-expect-error 残留 R-1:use 层无 reducedMotion 选项,运行时被忽略(见 visual-chromium-desktop 处说明)
reducedMotion: 'reduce',
},
testMatch: 'visual-regression.spec.ts',
},
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
testIgnore: 'visual-regression.spec.ts',
},
{
name: 'chromium-mobile',
use: {
...devices['iPhone 14'],
viewport: { width: 390, height: 844 },
isMobile: true,
hasTouch: true,
},
testIgnore: 'visual-regression.spec.ts',
},
{
name: 'firefox',
use: { ...devices['Desktop Firefox'], storageState: './storageState.firefox.json' },
testIgnore: 'visual-regression.spec.ts',
},
{
name: 'webkit',
use: { ...devices['Desktop Safari'], storageState: './storageState.firefox.json' },
testIgnore: 'visual-regression.spec.ts',
},
],
webServer: {
// E2E_TARGET=production 时打构建产物(验收 A-10:dev server 下预渲染、ISR、生产响应头
// 与构建期才暴露的问题永不被测)。dev 仍是默认目标,因为多数用例依赖热更新提速。
// 产物是 output:'standalone',Next 16 明确告警 `"next start" does not work with
// "output: standalone"` ⇒ 生产目标必须直接起 dist/standalone/server.js,否则测的是不受支持的降级路径。
command:
process.env.E2E_TARGET === 'production'
? 'HOSTNAME=localhost PORT=3000 node dist/standalone/server.js'
: 'npm run dev',
url: 'http://localhost:3000',
// 复用已有服务会让「测的是当前代码」这一前提失效(门禁脚本曾把 next-server 留在 :3000 达
// 39 分钟),故 CI 与 production 目标一律新起服务。
reuseExistingServer: !process.env.CI && process.env.E2E_TARGET !== 'production',
timeout: 180000,
cwd: '..',
env: {
CMS_REVALIDATE_SECRET: process.env.CMS_REVALIDATE_SECRET || 'e2e-test-secret',
// 登录限流是**进程内内存桶**,而这里的 server 跨整轮用例存活 ⇒ `@admin` 旅程每条各自登录一次,
// 全量功能 E2E 第 11 次起必得 429(§14 的 UJ-03/06/07:单跑绿、全量红的真因)。
// 只在 E2E harness 抬高额度;生产与默认环境不经过这里,仍是 10 次/15 分钟/IP
// (默认值与覆盖行为由 src/app/api/auth/login/route.test.ts 的双向用例钉住)。
LOGIN_RATE_LIMIT_MAX: '500',
},
},
});