2aa9d73325
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
68 lines
3.5 KiB
JavaScript
68 lines
3.5 KiB
JavaScript
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: '<meta name="viewport" content="width=device-width, initial-scale=1"><style>body{margin:0} div{width:100px;height:50px;background:orange;animation:move 3s infinite}@keyframes move{to{transform:translateX(100px)}}</style><div>环境验证</div>' }))
|
|
await page.goto(new URL('__pw_environment__', baseURL).href)
|
|
const actual = await page.evaluate(() => ({ width: innerWidth, height: innerHeight, dpr: devicePixelRatio, ua: navigator.userAgent, touch: navigator.maxTouchPoints > 0, language: navigator.language, timezone: Intl.DateTimeFormat().resolvedOptions().timeZone, reduced: matchMedia('(prefers-reduced-motion: reduce)').matches }))
|
|
expect(actual).toMatchObject({ width: expected.viewport.width, height: expected.viewport.height, dpr: expected.deviceScaleFactor, touch: expected.hasTouch, language: expected.locale, timezone: expected.timezoneId, reduced: true })
|
|
expect(actual.ua).toBe(expected.userAgent)
|
|
const image = await captureStatic(page)
|
|
expect(image.readUInt32BE(16)).toBe(expected.viewport.width)
|
|
expect(image.readUInt32BE(20)).toBe(expected.viewport.height)
|
|
expect((await captureStatic(page)).equals(image)).toBe(true)
|
|
})
|
|
`,
|
|
);
|
|
const env = {
|
|
...process.env,
|
|
PW_HEADLESS: '1',
|
|
UI_VISUAL_OFFSCREEN: '1',
|
|
PW_EXTENDED_LAYOUT: '1',
|
|
PW_WORKERS: '1',
|
|
};
|
|
delete env.PW_RUN_ID;
|
|
delete env.PLAYWRIGHT_HTML_OUTPUT_DIR;
|
|
const code = await new Promise((resolve, reject) => {
|
|
const child = spawn(
|
|
process.execPath,
|
|
[
|
|
path.join(root, 'node_modules/@playwright/test/cli.js'),
|
|
'test',
|
|
'--config',
|
|
path.join(directory, 'playwright.config.mjs'),
|
|
],
|
|
{ cwd: root, env, stdio: 'inherit' },
|
|
);
|
|
child.on('error', reject);
|
|
child.on('exit', (code) => resolve(code ?? 1));
|
|
});
|
|
process.exitCode = code;
|
|
} finally {
|
|
await rm(directory, { recursive: true, force: true });
|
|
}
|