playwright.config.js 1.87 KB
/*
 * @Date: 2026-01-28 21:45:00
 * @Description: Playwright E2E 测试配置
 */
import { defineConfig, devices } from '@playwright/test'

export default defineConfig({
  // 测试文件位置
  testDir: './e2e',

  // 测试超时时间(毫秒)
  timeout: 30 * 1000,

  // 期望超时时间
  expect: {
    timeout: 5 * 1000,
  },

  // 失败时重试次数
  retries: process.env.CI ? 2 : 0,

  // 并行执行
  workers: process.env.CI ? 1 : undefined,

  // 报告器
  reporter: [
    ['html', { outputFolder: 'playwright-report' }],
    ['json', { outputFile: 'test-results/test-results.json' }],
    ['junit', { outputFile: 'test-results/test-results.xml' }],
  ],

  // 共享配置
  use: {
    // 基础 URL(开发服务器地址)
    baseURL: 'http://localhost:5173',

    // 追踪失败测试(用于调试)
    trace: 'on-first-retry',

    // 截图(仅失败时)
    screenshot: 'only-on-failure',

    // 视频录制(仅失败时)
    video: 'retain-on-failure',

    // 浏览器视口大小(移动端优先)
    viewport: { width: 375, height: 667 },

    // 忽略 HTTPS 错误
    ignoreHTTPSErrors: true,

    // 操作超时
    actionTimeout: 10 * 1000,
    navigationTimeout: 30 * 1000,
  },

  // 测试项目(不同浏览器和视口)
  projects: [
    {
      name: 'chromium-mobile',
      use: {
        ...devices['iPhone 12'],
        browserName: 'chromium',
      },
    },
    {
      name: 'chromium-desktop',
      use: {
        ...devices['Desktop Chrome'],
        viewport: { width: 1280, height: 720 },
      },
    },
    {
      name: 'webkit-mobile',
      use: {
        ...devices['iPhone 12'],
        browserName: 'webkit',
      },
    },
  ],

  // 开发服务器(测试前启动)
  webServer: {
    command: 'pnpm dev',
    url: 'http://localhost:5173',
    reuseExistingServer: !process.env.CI,
    timeout: 120 * 1000,
  },
})