feat: 修改playwright配置

This commit is contained in:
zhanjunhao
2026-11-13 16:15:08 +08:00
parent 28b71237df
commit 19fd943660
+53 -27
View File
@@ -19,24 +19,51 @@ For commercial licensing, please contact support@quantumnous.com
import { defineConfig, devices } from '@playwright/test';
const baseURL = process.env.PLAYWRIGHT_BASE_URL || 'http://127.0.0.1:4175';
const target = process.env.PW_TARGET || 'local-dev';
if (!['local-dist', 'local-dev', 'remote'].includes(target)) {
throw new Error('PW_TARGET must be local-dist, local-dev or remote');
}
if (target === 'remote' && !process.env.PW_BASE_URL) {
throw new Error('PW_BASE_URL is required for remote tests');
}
const baseURL = new URL(process.env.PW_BASE_URL || 'http://127.0.0.1:4175/');
if (!['http:', 'https:'].includes(baseURL.protocol) || baseURL.username || baseURL.password || baseURL.search || baseURL.hash) {
throw new Error('PW_BASE_URL must be an HTTP(S) URL without credentials, query or hash');
}
if (!baseURL.pathname.endsWith('/')) throw new Error('PW_BASE_URL directory path must end with /');
if (target !== 'remote' && (baseURL.hostname !== '127.0.0.1' || baseURL.protocol !== 'http:' || baseURL.pathname !== '/')) {
throw new Error('Local targets require http://127.0.0.1:<port>/');
}
const isCI = Boolean(process.env.CI && process.env.CI !== 'false');
const headless = process.env.PW_HEADLESS === undefined ? isCI : process.env.PW_HEADLESS === '1';
const viewports = [
['desktop', 1920, 1080],
['mobile', 360, 800],
];
if (process.env.PW_EXTENDED_LAYOUT === '1') {
viewports.push(['tablet', 768, 1024], ['desktop-boundary', 1024, 768], ['laptop-hd', 1366, 768], ['laptop-wxga', 1440, 900], ['qhd', 2560, 1440], ['uhd', 3840, 2160]);
}
export default defineConfig({
testDir: './e2e',
testMatch: '**/*.spec.js',
outputDir: 'test-results',
fullyParallel: false,
forbidOnly: Boolean(process.env.CI),
retries: 0,
workers: 1,
timeout: 45000,
expect: { timeout: 10000 },
forbidOnly: isCI || process.env.PW_REQUIRE_ALL === '1',
failOnFlakyTests: isCI,
retries: isCI ? 1 : 0,
timeout: 30000,
expect: { timeout: 5000 },
reporter: [
['list'],
['html', { outputFolder: 'playwright-report', open: 'never' }],
...(isCI ? [['junit', { outputFile: 'test-results/junit.xml' }]] : []),
],
outputDir: 'test-results',
metadata: { platform: process.platform, node: process.version, target, baseURL: baseURL.href },
use: {
baseURL,
baseURL: baseURL.href,
headless,
launchOptions:
process.env.UI_VISUAL_OFFSCREEN === '1'
? {
@@ -48,28 +75,27 @@ export default defineConfig({
: {},
locale: 'zh-CN',
timezoneId: 'Asia/Shanghai',
deviceScaleFactor: 1,
colorScheme: 'light',
reducedMotion: 'reduce',
trace: 'retain-on-failure',
screenshot: 'only-on-failure',
video: 'off',
serviceWorkers: 'block',
video: process.env.PW_VIDEO === '1' ? 'retain-on-failure' : 'off',
},
projects: [
{
name: 'chrome',
use: { ...devices['Desktop Chrome'], channel: 'chrome' },
projects: viewports.map(([name, width, height]) => ({
name: `chrome-${name}`,
use: {
...(name === 'mobile' ? devices['Pixel 5'] : devices['Desktop Chrome']),
channel: 'chrome',
viewport: { width, height },
},
{
name: 'mobile-chrome',
use: { ...devices['Pixel 5'], channel: 'chrome' },
},
],
webServer: process.env.PLAYWRIGHT_BASE_URL
? undefined
: {
command: 'npm run dev -- --host 127.0.0.1 --port 4175 --strictPort',
url: baseURL,
reuseExistingServer: !process.env.CI,
timeout: 120000,
env: { VITE_REACT_APP_SERVER_URL: '' },
},
})),
webServer: target === 'remote' ? undefined : {
command: target === 'local-dist'
? `npm run preview -- --host 127.0.0.1 --port ${baseURL.port || 80}`
: `npm run dev -- --host 127.0.0.1 --port ${baseURL.port || 80} --strictPort`,
url: baseURL.href,
reuseExistingServer: false,
timeout: 30000,
},
});