Files
monkey-ai-web/scripts/playwright-test.mjs
zhanjunhao 2aa9d73325 chore(repo): 移除重复版权文件头并纳入共享单测
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
2026-11-20 11:23:49 +08:00

51 lines
1.5 KiB
JavaScript

import { test as base, expect } from '@playwright/test';
import {
checkFocus,
createFocusProbe,
installGuards,
openBackgroundPage,
} from './playwright-runner-focus.mjs';
import { prepareStaticCapture } from './browser-capture.mjs';
export const test = base.extend({
focusProbe: [
async ({ headless }, use) => {
const probe = headless ? null : createFocusProbe();
if (!headless && !probe)
throw new Error(
'焦点状态未确认:当前系统不支持前台窗口探测,请使用 PW_HEADLESS=1',
);
try {
await use(probe);
} finally {
probe?.dispose();
}
},
{ scope: 'worker' },
],
page: async ({ browser, context, focusProbe, headless }, use, testInfo) => {
if (!headless && process.env.CI && !['0', 'false'].includes(process.env.CI))
throw new Error('CI requires headless Chrome');
const page = await openBackgroundPage(browser, context);
const guards = installGuards(browser, context, page);
const screenshot = page.screenshot.bind(page);
page.screenshot = async (options = {}) => {
await prepareStaticCapture(page);
return screenshot({ scale: 'css', ...options });
};
try {
await checkFocus(focusProbe, '页面创建后', testInfo);
await use(page);
await checkFocus(focusProbe, '用例结束后', testInfo);
} finally {
guards.restore();
expect(
guards.violations,
'焦点守护:用例不得新建或切换标签页/窗口',
).toEqual([]);
}
},
});
export { expect };