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', // 同上:contact 是 5 次/小时/IP 的进程内桶,全量跑里多次提交会 429。 CONTACT_RATE_LIMIT_MAX: '200', }, }, });