chore(tooling): 对齐协作规范与 Playwright 运行配置
统一设备配置和浏览器启动入口,加入窗口焦点守卫,并按运行隔离报告和追踪文件。
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
Copyright (C) 2025 QuantumNous
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
For commercial licensing, please contact support@quantumnous.com
|
||||
*/
|
||||
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { createHash } from 'node:crypto';
|
||||
import { devices } from '@playwright/test';
|
||||
import { browserStabilityArgs } from './playwright-runtime.mjs';
|
||||
|
||||
// The review JSON is also read by Python; keep dimensions and device choices there.
|
||||
const config = JSON.parse(
|
||||
readFileSync(new URL('./ui-review/config.json', import.meta.url), 'utf8'),
|
||||
);
|
||||
const aliases = {
|
||||
'desktop-boundary': 'laptop',
|
||||
'laptop-hd': 'hd',
|
||||
'laptop-wxga': 'wxga',
|
||||
};
|
||||
export const browserChannel = config.browser.channel;
|
||||
export const projectViewports = [
|
||||
'desktop',
|
||||
'mobile',
|
||||
'tablet',
|
||||
'desktop-boundary',
|
||||
'laptop-hd',
|
||||
'laptop-wxga',
|
||||
'qhd',
|
||||
'uhd',
|
||||
];
|
||||
|
||||
export function deviceOptions(name) {
|
||||
const key = aliases[name] || name;
|
||||
const size = config.viewports[key];
|
||||
if (!size) throw new Error(`Unknown viewport: ${name}`);
|
||||
const descriptor =
|
||||
devices[
|
||||
config.browser[key === 'mobile' ? 'mobileDevice' : 'desktopDevice']
|
||||
];
|
||||
if (!descriptor) throw new Error(`Unknown device profile for ${name}`);
|
||||
const { defaultBrowserType, ...context } = descriptor;
|
||||
return { ...context, viewport: { width: size[0], height: size[1] } };
|
||||
}
|
||||
|
||||
export function contextOptions(name, reducedMotion = 'reduce') {
|
||||
return {
|
||||
...deviceOptions(name),
|
||||
locale: config.browser.locale,
|
||||
timezoneId: config.browser.timezone,
|
||||
colorScheme: 'light',
|
||||
reducedMotion,
|
||||
};
|
||||
}
|
||||
|
||||
export function environmentFingerprint(name, reducedMotion = 'reduce') {
|
||||
return createHash('sha256')
|
||||
.update(
|
||||
JSON.stringify({
|
||||
version: 2,
|
||||
channel: browserChannel,
|
||||
options: contextOptions(name, reducedMotion),
|
||||
launchArgs: browserStabilityArgs,
|
||||
screenshotScale: 'css',
|
||||
}),
|
||||
)
|
||||
.digest('hex');
|
||||
}
|
||||
Reference in New Issue
Block a user