Files
monkey-ai-web/scripts/playwright-test.mjs
T
zhanjunhao d0ef462921 chore(tooling): 对齐协作规范与浏览器验收工具
验证:713 项单测、8 视口环境、代码及格式检查通过;未运行生产构建。
2026-11-17 14:48:50 +08:00

70 lines
2.3 KiB
JavaScript

/*
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 { 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 };