chore: rename DOMWorld to IsolatedWorld (#8761)

This commit is contained in:
jrandolf 2022-08-09 15:17:42 +02:00 committed by GitHub
parent 932a053d02
commit 837b10b15e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 48 additions and 39 deletions

View File

@ -17,7 +17,11 @@
import {Protocol} from 'devtools-protocol'; import {Protocol} from 'devtools-protocol';
import {assert} from './assert.js'; import {assert} from './assert.js';
import {CDPSession} from './Connection.js'; import {CDPSession} from './Connection.js';
import {DOMWorld, PageBinding, WaitForSelectorOptions} from './DOMWorld.js'; import {
IsolatedWorld,
PageBinding,
WaitForSelectorOptions,
} from './IsolatedWorld.js';
import {ElementHandle} from './ElementHandle.js'; import {ElementHandle} from './ElementHandle.js';
import {JSHandle} from './JSHandle.js'; import {JSHandle} from './JSHandle.js';
import {InternalQueryHandler} from './QueryHandler.js'; import {InternalQueryHandler} from './QueryHandler.js';
@ -101,19 +105,19 @@ const queryOne = async (
}; };
const waitFor = async ( const waitFor = async (
domWorld: DOMWorld, isolatedWorld: IsolatedWorld,
selector: string, selector: string,
options: WaitForSelectorOptions options: WaitForSelectorOptions
): Promise<ElementHandle<Element> | null> => { ): Promise<ElementHandle<Element> | null> => {
const binding: PageBinding = { const binding: PageBinding = {
name: 'ariaQuerySelector', name: 'ariaQuerySelector',
pptrFunction: async (selector: string) => { pptrFunction: async (selector: string) => {
const root = options.root || (await domWorld._document()); const root = options.root || (await isolatedWorld._document());
const element = await queryOne(root, selector); const element = await queryOne(root, selector);
return element; return element;
}, },
}; };
return (await domWorld._waitForSelectorInPage( return (await isolatedWorld._waitForSelectorInPage(
(_: Element, selector: string) => { (_: Element, selector: string) => {
return ( return (
globalThis as unknown as { globalThis as unknown as {
@ -135,11 +139,13 @@ const queryAll = async (
const {name, role} = parseAriaSelector(selector); const {name, role} = parseAriaSelector(selector);
const res = await queryAXTree(exeCtx._client, element, name, role); const res = await queryAXTree(exeCtx._client, element, name, role);
const world = exeCtx._world!; const world = exeCtx._world!;
return (await Promise.all( return Promise.all(
res.map(axNode => { res.map(axNode => {
return world.adoptBackendNode(axNode.backendDOMNodeId); return world.adoptBackendNode(axNode.backendDOMNodeId) as Promise<
ElementHandle<Node>
>;
}) })
)) as Array<ElementHandle<Node>>; );
}; };
const queryAllArray = async ( const queryAllArray = async (

View File

@ -1,7 +1,7 @@
import {Protocol} from 'devtools-protocol'; import {Protocol} from 'devtools-protocol';
import {assert} from './assert.js'; import {assert} from './assert.js';
import {CDPSession} from './Connection.js'; import {CDPSession} from './Connection.js';
import {WaitForSelectorOptions} from './DOMWorld.js'; import {WaitForSelectorOptions} from './IsolatedWorld.js';
import {ExecutionContext} from './ExecutionContext.js'; import {ExecutionContext} from './ExecutionContext.js';
import {Frame, FrameManager} from './FrameManager.js'; import {Frame, FrameManager} from './FrameManager.js';
import { import {

View File

@ -17,7 +17,7 @@
import {Protocol} from 'devtools-protocol'; import {Protocol} from 'devtools-protocol';
import {assert} from './assert.js'; import {assert} from './assert.js';
import {CDPSession} from './Connection.js'; import {CDPSession} from './Connection.js';
import {DOMWorld} from './DOMWorld.js'; import {IsolatedWorld} from './IsolatedWorld.js';
import {ElementHandle} from './ElementHandle.js'; import {ElementHandle} from './ElementHandle.js';
import {Frame} from './FrameManager.js'; import {Frame} from './FrameManager.js';
import {JSHandle} from './JSHandle.js'; import {JSHandle} from './JSHandle.js';
@ -57,7 +57,7 @@ export class ExecutionContext {
/** /**
* @internal * @internal
*/ */
_world?: DOMWorld; _world?: IsolatedWorld;
/** /**
* @internal * @internal
*/ */
@ -73,7 +73,7 @@ export class ExecutionContext {
constructor( constructor(
client: CDPSession, client: CDPSession,
contextPayload: Protocol.Runtime.ExecutionContextDescription, contextPayload: Protocol.Runtime.ExecutionContextDescription,
world?: DOMWorld world?: IsolatedWorld
) { ) {
this._client = client; this._client = client;
this._world = world; this._world = world;

View File

@ -17,7 +17,7 @@
import {Protocol} from 'devtools-protocol'; import {Protocol} from 'devtools-protocol';
import {assert} from './assert.js'; import {assert} from './assert.js';
import {CDPSession} from './Connection.js'; import {CDPSession} from './Connection.js';
import {DOMWorld, WaitForSelectorOptions} from './DOMWorld.js'; import {IsolatedWorld, WaitForSelectorOptions} from './IsolatedWorld.js';
import {ElementHandle} from './ElementHandle.js'; import {ElementHandle} from './ElementHandle.js';
import {EventEmitter} from './EventEmitter.js'; import {EventEmitter} from './EventEmitter.js';
import {EVALUATION_SCRIPT_URL, ExecutionContext} from './ExecutionContext.js'; import {EVALUATION_SCRIPT_URL, ExecutionContext} from './ExecutionContext.js';
@ -530,7 +530,7 @@ export class FrameManager extends EventEmitter {
const frameId = auxData && auxData.frameId; const frameId = auxData && auxData.frameId;
const frame = const frame =
typeof frameId === 'string' ? this.#frames.get(frameId) : undefined; typeof frameId === 'string' ? this.#frames.get(frameId) : undefined;
let world: DOMWorld | undefined; let world: IsolatedWorld | undefined;
if (frame) { if (frame) {
// Only care about execution contexts created for the current session. // Only care about execution contexts created for the current session.
if (frame._client() !== session) { if (frame._client() !== session) {
@ -763,11 +763,11 @@ export class Frame {
/** /**
* @internal * @internal
*/ */
_mainWorld!: DOMWorld; _mainWorld!: IsolatedWorld;
/** /**
* @internal * @internal
*/ */
_secondaryWorld!: DOMWorld; _secondaryWorld!: IsolatedWorld;
/** /**
* @internal * @internal
*/ */
@ -803,13 +803,13 @@ export class Frame {
*/ */
_updateClient(client: CDPSession): void { _updateClient(client: CDPSession): void {
this.#client = client; this.#client = client;
this._mainWorld = new DOMWorld( this._mainWorld = new IsolatedWorld(
this.#client, this.#client,
this._frameManager, this._frameManager,
this, this,
this._frameManager._timeoutSettings this._frameManager._timeoutSettings
); );
this._secondaryWorld = new DOMWorld( this._secondaryWorld = new IsolatedWorld(
this.#client, this.#client,
this._frameManager, this._frameManager,
this, this,

View File

@ -72,7 +72,7 @@ export interface PageBinding {
/** /**
* @internal * @internal
*/ */
export class DOMWorld { export class IsolatedWorld {
#frameManager: FrameManager; #frameManager: FrameManager;
#client: CDPSession; #client: CDPSession;
#frame: Frame; #frame: Frame;
@ -564,7 +564,7 @@ export class DOMWorld {
// Previous operation added the binding so we are done. // Previous operation added the binding so we are done.
if ( if (
this.#ctxBindings.has( this.#ctxBindings.has(
DOMWorld.#bindingIdentifier(name, context._contextId) IsolatedWorld.#bindingIdentifier(name, context._contextId)
) )
) { ) {
return; return;
@ -604,7 +604,7 @@ export class DOMWorld {
} }
} }
this.#ctxBindings.add( this.#ctxBindings.add(
DOMWorld.#bindingIdentifier(name, context._contextId) IsolatedWorld.#bindingIdentifier(name, context._contextId)
); );
}; };
@ -632,7 +632,7 @@ export class DOMWorld {
if ( if (
type !== 'internal' || type !== 'internal' ||
!this.#ctxBindings.has( !this.#ctxBindings.has(
DOMWorld.#bindingIdentifier(name, context._contextId) IsolatedWorld.#bindingIdentifier(name, context._contextId)
) )
) { ) {
return; return;
@ -695,7 +695,7 @@ export class DOMWorld {
return checkWaitForOptions(node, waitForVisible, waitForHidden); return checkWaitForOptions(node, waitForVisible, waitForHidden);
} }
const waitTaskOptions: WaitTaskOptions = { const waitTaskOptions: WaitTaskOptions = {
domWorld: this, isolatedWorld: this,
predicateBody: makePredicateString(predicate, queryOne), predicateBody: makePredicateString(predicate, queryOne),
predicateAcceptsContextElement: true, predicateAcceptsContextElement: true,
title, title,
@ -723,7 +723,7 @@ export class DOMWorld {
const {polling = 'raf', timeout = this.#timeoutSettings.timeout()} = const {polling = 'raf', timeout = this.#timeoutSettings.timeout()} =
options; options;
const waitTaskOptions: WaitTaskOptions = { const waitTaskOptions: WaitTaskOptions = {
domWorld: this, isolatedWorld: this,
predicateBody: pageFunction, predicateBody: pageFunction,
predicateAcceptsContextElement: false, predicateAcceptsContextElement: false,
title: 'function', title: 'function',
@ -769,7 +769,7 @@ export class DOMWorld {
* @internal * @internal
*/ */
export interface WaitTaskOptions { export interface WaitTaskOptions {
domWorld: DOMWorld; isolatedWorld: IsolatedWorld;
predicateBody: Function | string; predicateBody: Function | string;
predicateAcceptsContextElement: boolean; predicateAcceptsContextElement: boolean;
title: string; title: string;
@ -786,7 +786,7 @@ const noop = (): void => {};
* @internal * @internal
*/ */
export class WaitTask { export class WaitTask {
#domWorld: DOMWorld; #isolatedWorld: IsolatedWorld;
#polling: 'raf' | 'mutation' | number; #polling: 'raf' | 'mutation' | number;
#timeout: number; #timeout: number;
#predicateBody: string; #predicateBody: string;
@ -824,7 +824,7 @@ export class WaitTask {
return `return (${predicateBody})(...args);`; return `return (${predicateBody})(...args);`;
} }
this.#domWorld = options.domWorld; this.#isolatedWorld = options.isolatedWorld;
this.#polling = options.polling; this.#polling = options.polling;
this.#timeout = options.timeout; this.#timeout = options.timeout;
this.#root = options.root || null; this.#root = options.root || null;
@ -834,9 +834,9 @@ export class WaitTask {
this.#args = options.args; this.#args = options.args;
this.#binding = options.binding; this.#binding = options.binding;
this.#runCount = 0; this.#runCount = 0;
this.#domWorld._waitTasks.add(this); this.#isolatedWorld._waitTasks.add(this);
if (this.#binding) { if (this.#binding) {
this.#domWorld._boundFunctions.set( this.#isolatedWorld._boundFunctions.set(
this.#binding.name, this.#binding.name,
this.#binding.pptrFunction this.#binding.pptrFunction
); );
@ -868,12 +868,15 @@ export class WaitTask {
const runCount = ++this.#runCount; const runCount = ++this.#runCount;
let success: JSHandle | null = null; let success: JSHandle | null = null;
let error: Error | null = null; let error: Error | null = null;
const context = await this.#domWorld.executionContext(); const context = await this.#isolatedWorld.executionContext();
if (this.#terminated || runCount !== this.#runCount) { if (this.#terminated || runCount !== this.#runCount) {
return; return;
} }
if (this.#binding) { if (this.#binding) {
await this.#domWorld._addBindingToContext(context, this.#binding.name); await this.#isolatedWorld._addBindingToContext(
context,
this.#binding.name
);
} }
if (this.#terminated || runCount !== this.#runCount) { if (this.#terminated || runCount !== this.#runCount) {
return; return;
@ -904,7 +907,7 @@ export class WaitTask {
// throw an error - ignore this predicate run altogether. // throw an error - ignore this predicate run altogether.
if ( if (
!error && !error &&
(await this.#domWorld (await this.#isolatedWorld
.evaluate(s => { .evaluate(s => {
return !s; return !s;
}, success) }, success)
@ -922,7 +925,7 @@ export class WaitTask {
if (error.message.includes('TypeError: binding is not a function')) { if (error.message.includes('TypeError: binding is not a function')) {
return this.rerun(); return this.rerun();
} }
// When frame is detached the task should have been terminated by the DOMWorld. // When frame is detached the task should have been terminated by the IsolatedWorld.
// This can fail if we were adding this task while the frame was detached, // This can fail if we were adding this task while the frame was detached,
// so we terminate here instead. // so we terminate here instead.
if ( if (
@ -960,7 +963,7 @@ export class WaitTask {
#cleanup(): void { #cleanup(): void {
this.#timeoutTimer !== undefined && clearTimeout(this.#timeoutTimer); this.#timeoutTimer !== undefined && clearTimeout(this.#timeoutTimer);
this.#domWorld._waitTasks.delete(this); this.#isolatedWorld._waitTasks.delete(this);
} }
} }

View File

@ -23,7 +23,7 @@ import {CDPSession, CDPSessionEmittedEvents} from './Connection.js';
import {ConsoleMessage, ConsoleMessageType} from './ConsoleMessage.js'; import {ConsoleMessage, ConsoleMessageType} from './ConsoleMessage.js';
import {Coverage} from './Coverage.js'; import {Coverage} from './Coverage.js';
import {Dialog} from './Dialog.js'; import {Dialog} from './Dialog.js';
import {WaitForSelectorOptions} from './DOMWorld.js'; import {WaitForSelectorOptions} from './IsolatedWorld.js';
import {ElementHandle} from './ElementHandle.js'; import {ElementHandle} from './ElementHandle.js';
import {EmulationManager} from './EmulationManager.js'; import {EmulationManager} from './EmulationManager.js';
import {EventEmitter, Handler} from './EventEmitter.js'; import {EventEmitter, Handler} from './EventEmitter.js';

View File

@ -15,7 +15,7 @@
*/ */
import {ariaHandler} from './AriaQueryHandler.js'; import {ariaHandler} from './AriaQueryHandler.js';
import {DOMWorld, WaitForSelectorOptions} from './DOMWorld.js'; import {IsolatedWorld, WaitForSelectorOptions} from './IsolatedWorld.js';
import {ElementHandle} from './ElementHandle.js'; import {ElementHandle} from './ElementHandle.js';
import {JSHandle} from './JSHandle.js'; import {JSHandle} from './JSHandle.js';
@ -72,7 +72,7 @@ export interface InternalQueryHandler {
* Akin to {@link Window.prototype.querySelectorAll}. * Akin to {@link Window.prototype.querySelectorAll}.
*/ */
waitFor?: ( waitFor?: (
domWorld: DOMWorld, isolatedWorld: IsolatedWorld,
selector: string, selector: string,
options: WaitForSelectorOptions options: WaitForSelectorOptions
) => Promise<ElementHandle<Node> | null>; ) => Promise<ElementHandle<Node> | null>;
@ -95,11 +95,11 @@ function internalizeCustomQueryHandler(
return null; return null;
}; };
internalHandler.waitFor = ( internalHandler.waitFor = (
domWorld: DOMWorld, isolatedWorld: IsolatedWorld,
selector: string, selector: string,
options: WaitForSelectorOptions options: WaitForSelectorOptions
) => { ) => {
return domWorld._waitForSelectorInPage(queryOne, selector, options); return isolatedWorld._waitForSelectorInPage(queryOne, selector, options);
}; };
} }

View File

@ -18,7 +18,6 @@ export * from './common/Connection.js';
export * from './common/ConnectionTransport.js'; export * from './common/ConnectionTransport.js';
export * from './common/ConsoleMessage.js'; export * from './common/ConsoleMessage.js';
export * from './common/Coverage.js'; export * from './common/Coverage.js';
export * from './common/DOMWorld.js';
export * from './common/Debug.js'; export * from './common/Debug.js';
export * from './common/DeviceDescriptors.js'; export * from './common/DeviceDescriptors.js';
export * from './common/Dialog.js'; export * from './common/Dialog.js';
@ -33,6 +32,7 @@ export * from './common/FrameManager.js';
export * from './common/HTTPRequest.js'; export * from './common/HTTPRequest.js';
export * from './common/HTTPResponse.js'; export * from './common/HTTPResponse.js';
export * from './common/Input.js'; export * from './common/Input.js';
export * from './common/IsolatedWorld.js';
export * from './common/JSHandle.js'; export * from './common/JSHandle.js';
export * from './common/LifecycleWatcher.js'; export * from './common/LifecycleWatcher.js';
export * from './common/NetworkConditions.js'; export * from './common/NetworkConditions.js';