import { mkdtemp, writeFile, rm } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import path from 'node:path'; import { fileURLToPath, pathToFileURL } from 'node:url'; import { spawn } from 'node:child_process'; const root = fileURLToPath(new URL('../', import.meta.url)); const directory = await mkdtemp(path.join(tmpdir(), 'pw-environment-')); const url = (name) => pathToFileURL(path.join(root, name)).href; try { await writeFile( path.join(directory, 'playwright.config.mjs'), ` import config from ${JSON.stringify(url('playwright.config.js'))} export default { ...config, testDir: ${JSON.stringify(directory)}, testMatch: '**/*.spec.mjs', fullyParallel: false, workers: 1, outputDir: ${JSON.stringify(root)} + config.outputDir, reporter: config.reporter.map(([name, options]) => [name.startsWith('.') ? ${JSON.stringify(root)} + name : name, name === 'html' ? { ...options, outputFolder: ${JSON.stringify(root)} + options.outputFolder } : options]), webServer: config.webServer ? { ...config.webServer, cwd: ${JSON.stringify(root)} } : undefined } `, ); await writeFile( path.join(directory, 'environment.spec.mjs'), ` import { test, expect } from ${JSON.stringify(url('scripts/playwright-test.mjs'))} import { contextOptions } from ${JSON.stringify(url('scripts/browser-environment.mjs'))} import { captureStatic } from ${JSON.stringify(url('scripts/browser-capture.mjs'))} test('设备上下文、触摸、DPR、CSS 截图与稳定字体', async ({ page, baseURL }, info) => { const expected = contextOptions(info.project.name.replace('chrome-', '')) await page.route('**/__pw_environment__', route => route.fulfill({ contentType: 'text/html', body: '