chore(tooling): 对齐协作规范与 Playwright 运行配置
统一设备配置和浏览器启动入口,加入窗口焦点守卫,并按运行隔离报告和追踪文件。
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
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 };
|
||||
Reference in New Issue
Block a user