import { defineConfig, devices } from '@playwright/test' /** * Playwright配置文件 * E2E测试配置 */ export default defineConfig({ // 测试目录 testDir: './tests/e2e', // 测试文件匹配模式 testMatch: '**/*.spec.ts', // 完全并行运行测试文件 fullyParallel: true, // 在CI中失败时不重试 // 在本地开发中重试失败的测试 retries: process.env.CI ? 0 : 2, // 在CI中限制并行工作线程数 // 在本地使用所有可用线程 workers: process.env.CI ? 2 : undefined, // 测试报告 reporter: [ ['html', { outputFolder: 'test_reports/playwright-report' }], ['json', { outputFile: 'test_reports/playwright-results.json' }], ['junit', { outputFile: 'test_reports/playwright-junit.xml' }], ['list'] ], // 全局设置 use: { // 基础URL baseURL: 'http://localhost:5173', // 收集失败测试的追踪信息 trace: 'retain-on-failure', // 截图配置 screenshot: 'only-on-failure', // 视频配置 video: 'retain-on-failure', // 操作超时时间 actionTimeout: 10 * 1000, // 10秒 navigationTimeout: 30 * 1000, // 30秒 // 浏览器视口大小 viewport: { width: 1280, height: 720 }, // 忽略HTTPS错误 ignoreHTTPSErrors: true, // 等待超时 waitUntil: 'networkidle' }, // 项目配置(不同浏览器) projects: [ { name: 'chromium', use: { ...devices['Desktop Chrome'] }, }, { name: 'firefox', use: { ...devices['Desktop Firefox'] }, }, { name: 'webkit', use: { ...devices['Desktop Safari'] }, }, /* 测试移动端视图 */ { name: 'Mobile Chrome', use: { ...devices['Pixel 5'] }, }, { name: 'Mobile Safari', use: { ...devices['iPhone 12'] }, }, ], // 开发服务器配置(启动测试前自动启动) webServer: { command: 'npm run dev', url: 'http://localhost:5173', reuseExistingServer: !process.env.CI, timeout: 120 * 1000, // 2分钟 }, // 全局setup globalSetup: './tests/e2e/global-setup.ts', // 全局teardown globalTeardown: './tests/e2e/global-teardown.ts', // 期望超时 expect: { timeout: 5000 } })