import { defineConfig, devices } from '@playwright/test' const PORT = 5199 const PREVIEW_PORT = 4173 /** * 使用系统已安装的浏览器,而不是下载 Playwright 自带的 Chromium。 * * 原因:本机对 Playwright CDN 的下载会超时(197MB 拉到 10% 就断), * 而系统已装有 Chrome / Edge,直接驱动即可完成同样的端到端验证。 * 若需换回自带浏览器,设置 PLAYWRIGHT_CHANNEL=chromium。 */ const CHANNEL = process.env.PLAYWRIGHT_CHANNEL ?? 'chrome' const browserUse = { ...devices['Desktop Chrome'], channel: CHANNEL === 'chromium' ? undefined : CHANNEL, trace: 'retain-on-failure' as const, } export default defineConfig({ testDir: './e2e', fullyParallel: true, forbidOnly: Boolean(process.env.CI), retries: process.env.CI ? 1 : 0, reporter: [['list']], projects: [ { // 交互行为用 dev server 验证,不必每次都跑生产构建 name: `${CHANNEL}-app`, testIgnore: /pwa\.spec\.ts/, use: { ...browserUse, baseURL: `http://localhost:${PORT}` }, }, { // Service Worker 只在生产构建里生成,因此 PWA 用例必须跑 preview name: `${CHANNEL}-pwa`, testMatch: /pwa\.spec\.ts/, use: { ...browserUse, baseURL: `http://localhost:${PREVIEW_PORT}` }, }, ], webServer: [ { command: `npm run dev -- --port ${PORT} --strictPort`, url: `http://localhost:${PORT}`, reuseExistingServer: !process.env.CI, timeout: 120_000, }, { command: `npm run build && npm run preview -- --port ${PREVIEW_PORT} --strictPort`, url: `http://localhost:${PREVIEW_PORT}`, reuseExistingServer: !process.env.CI, timeout: 180_000, }, ], })