playwright.config.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { defineConfig, devices } from '@playwright/test';
  2. export default defineConfig({
  3. testDir: './specs',
  4. fullyParallel: true,
  5. forbidOnly: !!process.env.CI,
  6. retries: process.env.CI ? 2 : 0,
  7. // 单 worker 运行,避免多会话同时测试时资源竞争
  8. // 可通过 PW_WORKERS=2 环境变量覆盖
  9. workers: parseInt(process.env.PW_WORKERS || '1', 10),
  10. // 增加默认超时时间(E2E 测试需要更长时间)
  11. timeout: 120000, // 120秒单个测试超时
  12. reporter: [
  13. ['html'],
  14. ['list'],
  15. ['junit', { outputFile: 'test-results/junit.xml' }]
  16. ],
  17. use: {
  18. baseURL: process.env.E2E_BASE_URL || 'http://localhost:8080',
  19. trace: 'on-first-retry',
  20. screenshot: 'only-on-failure',
  21. video: 'retain-on-failure',
  22. // 在每个测试前设置测试模式标志,使 Checkbox 组件使用原生版本
  23. initScripts: [
  24. { content: 'window.__PLAYWRIGHT_TEST__ = true;' }
  25. ],
  26. },
  27. projects: [
  28. {
  29. name: 'chromium',
  30. use: { ...devices['Desktop Chrome'] },
  31. },
  32. {
  33. name: 'firefox',
  34. use: { ...devices['Desktop Firefox'] },
  35. },
  36. {
  37. name: 'webkit',
  38. use: { ...devices['Desktop Safari'] },
  39. },
  40. {
  41. name: 'Mobile Chrome',
  42. use: { ...devices['Pixel 5'] },
  43. },
  44. {
  45. name: 'Mobile Safari',
  46. use: { ...devices['iPhone 12'] },
  47. },
  48. ],
  49. // 只在没有运行中的服务器时才启动 webServer
  50. // 设置 E2E_START_SERVER=1 来强制启动服务器
  51. webServer: process.env.E2E_START_SERVER === '1' ? {
  52. command: 'npm run dev',
  53. url: 'http://localhost:8080',
  54. timeout: 120000,
  55. } : undefined,
  56. });