chore: detect document open for inspector (#9901)
To be used later for other global event listeners.
This commit is contained in:
parent
bd505ed07c
commit
4ff69529d4
|
|
@ -67,6 +67,7 @@ export class InjectedScript {
|
||||||
_evaluator: SelectorEvaluatorImpl;
|
_evaluator: SelectorEvaluatorImpl;
|
||||||
private _stableRafCount: number;
|
private _stableRafCount: number;
|
||||||
private _browserName: string;
|
private _browserName: string;
|
||||||
|
onGlobalListenersRemoved = new Set<() => void>();
|
||||||
|
|
||||||
constructor(stableRafCount: number, browserName: string, customEngines: { name: string, engine: SelectorEngine}[]) {
|
constructor(stableRafCount: number, browserName: string, customEngines: { name: string, engine: SelectorEngine}[]) {
|
||||||
this._evaluator = new SelectorEvaluatorImpl(new Map());
|
this._evaluator = new SelectorEvaluatorImpl(new Map());
|
||||||
|
|
@ -95,6 +96,8 @@ export class InjectedScript {
|
||||||
|
|
||||||
this._stableRafCount = stableRafCount;
|
this._stableRafCount = stableRafCount;
|
||||||
this._browserName = browserName;
|
this._browserName = browserName;
|
||||||
|
|
||||||
|
this._setupGlobalListenersRemovalDetection();
|
||||||
}
|
}
|
||||||
|
|
||||||
eval(expression: string): any {
|
eval(expression: string): any {
|
||||||
|
|
@ -769,6 +772,31 @@ export class InjectedScript {
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _setupGlobalListenersRemovalDetection() {
|
||||||
|
const customEventName = '__playwright_global_listeners_check__';
|
||||||
|
|
||||||
|
let seenEvent = false;
|
||||||
|
const handleCustomEvent = () => seenEvent = true;
|
||||||
|
window.addEventListener(customEventName, handleCustomEvent);
|
||||||
|
|
||||||
|
new MutationObserver(entries => {
|
||||||
|
const newDocumentElement = entries.some(entry => Array.from(entry.addedNodes).includes(document.documentElement));
|
||||||
|
if (!newDocumentElement)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// New documentElement - let's check whether listeners are still here.
|
||||||
|
seenEvent = false;
|
||||||
|
window.dispatchEvent(new CustomEvent(customEventName));
|
||||||
|
if (seenEvent)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Listener did not fire. Reattach the listener and notify.
|
||||||
|
window.addEventListener(customEventName, handleCustomEvent);
|
||||||
|
for (const callback of this.onGlobalListenersRemoved)
|
||||||
|
callback();
|
||||||
|
}).observe(document, { childList: true });
|
||||||
|
}
|
||||||
|
|
||||||
expect(progress: InjectedScriptProgress, element: Element, options: FrameExpectParams, elements: Element[]): { matches: boolean, received?: any } {
|
expect(progress: InjectedScriptProgress, element: Element, options: FrameExpectParams, elements: Element[]): { matches: boolean, received?: any } {
|
||||||
const injected = progress.injectedScript;
|
const injected = progress.injectedScript;
|
||||||
const expression = options.expression;
|
const expression = options.expression;
|
||||||
|
|
|
||||||
|
|
@ -116,17 +116,14 @@ export class Recorder {
|
||||||
this._glassPaneShadow.appendChild(styleElement);
|
this._glassPaneShadow.appendChild(styleElement);
|
||||||
|
|
||||||
this._refreshListenersIfNeeded();
|
this._refreshListenersIfNeeded();
|
||||||
setInterval(() => {
|
injectedScript.onGlobalListenersRemoved.add(() => this._refreshListenersIfNeeded());
|
||||||
this._refreshListenersIfNeeded();
|
|
||||||
if (params.isUnderTest && !(this as any)._reportedReadyForTest) {
|
|
||||||
(this as any)._reportedReadyForTest = true;
|
|
||||||
console.error('Recorder script ready for test');
|
|
||||||
}
|
|
||||||
}, 500);
|
|
||||||
globalThis._playwrightRefreshOverlay = () => {
|
globalThis._playwrightRefreshOverlay = () => {
|
||||||
this._pollRecorderMode().catch(e => console.log(e)); // eslint-disable-line no-console
|
this._pollRecorderMode().catch(e => console.log(e)); // eslint-disable-line no-console
|
||||||
};
|
};
|
||||||
globalThis._playwrightRefreshOverlay();
|
globalThis._playwrightRefreshOverlay();
|
||||||
|
if (params.isUnderTest)
|
||||||
|
console.error('Recorder script ready for test');
|
||||||
}
|
}
|
||||||
|
|
||||||
private _refreshListenersIfNeeded() {
|
private _refreshListenersIfNeeded() {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue