From f3c7499e677ebe0a883347e86510a77ecc570eb0 Mon Sep 17 00:00:00 2001 From: jrandolf <101637635+jrandolf@users.noreply.github.com> Date: Fri, 1 Sep 2023 14:12:29 +0200 Subject: [PATCH] chore: refactor handles to use realms (#10830) --- docs/api/puppeteer.jshandle.evaluate.md | 2 +- docs/api/puppeteer.jshandle.evaluatehandle.md | 2 +- docs/api/puppeteer.jshandle.getproperties.md | 4 +- docs/api/puppeteer.jshandle.getproperty.md | 2 +- docs/api/puppeteer.jshandle.getproperty_1.md | 2 +- .../puppeteer-core/src/api/ElementHandle.ts | 11 +- .../puppeteer-core/src/api/Environment.ts | 27 +++++ packages/puppeteer-core/src/api/Frame.ts | 67 +++++------ packages/puppeteer-core/src/api/JSHandle.ts | 69 +++++++++-- packages/puppeteer-core/src/api/Realm.ts | 21 ++-- .../src/common/AriaQueryHandler.ts | 19 ++-- .../src/common/ElementHandle.ts | 60 ++-------- .../src/common/ExecutionContext.ts | 26 ++--- packages/puppeteer-core/src/common/Frame.ts | 23 ++-- .../puppeteer-core/src/common/FrameManager.ts | 10 +- .../src/common/IsolatedWorld.ts | 73 ++++++------ .../puppeteer-core/src/common/JSHandle.ts | 107 +++--------------- packages/puppeteer-core/src/common/Page.ts | 57 ++++++---- .../puppeteer-core/src/common/QueryHandler.ts | 2 - .../puppeteer-core/src/common/WebWorker.ts | 30 +++-- .../src/common/bidi/BrowsingContext.ts | 5 +- .../src/common/bidi/ElementHandle.ts | 21 ++-- .../puppeteer-core/src/common/bidi/Frame.ts | 15 +-- .../src/common/bidi/JSHandle.ts | 89 ++------------- .../puppeteer-core/src/common/bidi/Page.ts | 12 +- .../puppeteer-core/src/common/bidi/Realm.ts | 42 +++---- .../puppeteer-core/src/common/bidi/Sandbox.ts | 31 ++--- .../src/common/bidi/Serializer.ts | 20 ++-- packages/puppeteer-core/src/common/util.ts | 12 +- test/TestExpectations.json | 12 -- test/src/evaluation.spec.ts | 20 ---- test/src/frame.spec.ts | 39 +------ test/src/worker.spec.ts | 2 +- 33 files changed, 386 insertions(+), 548 deletions(-) create mode 100644 packages/puppeteer-core/src/api/Environment.ts diff --git a/docs/api/puppeteer.jshandle.evaluate.md b/docs/api/puppeteer.jshandle.evaluate.md index 78b4b1df..a888acd1 100644 --- a/docs/api/puppeteer.jshandle.evaluate.md +++ b/docs/api/puppeteer.jshandle.evaluate.md @@ -10,7 +10,7 @@ Evaluates the given function with the current handle as its first argument. ```typescript class JSHandle { - abstract evaluate< + evaluate< Params extends unknown[], Func extends EvaluateFuncWith = EvaluateFuncWith, >( diff --git a/docs/api/puppeteer.jshandle.evaluatehandle.md b/docs/api/puppeteer.jshandle.evaluatehandle.md index 339f1cc4..4c5c35f5 100644 --- a/docs/api/puppeteer.jshandle.evaluatehandle.md +++ b/docs/api/puppeteer.jshandle.evaluatehandle.md @@ -10,7 +10,7 @@ Evaluates the given function with the current handle as its first argument. ```typescript class JSHandle { - abstract evaluateHandle< + evaluateHandle< Params extends unknown[], Func extends EvaluateFuncWith = EvaluateFuncWith, >( diff --git a/docs/api/puppeteer.jshandle.getproperties.md b/docs/api/puppeteer.jshandle.getproperties.md index c0bf247a..f6ddb06c 100644 --- a/docs/api/puppeteer.jshandle.getproperties.md +++ b/docs/api/puppeteer.jshandle.getproperties.md @@ -10,13 +10,13 @@ Gets a map of handles representing the properties of the current handle. ```typescript class JSHandle { - abstract getProperties(): Promise>>; + getProperties(): Promise>; } ``` **Returns:** -Promise<Map<string, [JSHandle](./puppeteer.jshandle.md)<unknown>>> +Promise<Map<string, [JSHandle](./puppeteer.jshandle.md)>> ## Example diff --git a/docs/api/puppeteer.jshandle.getproperty.md b/docs/api/puppeteer.jshandle.getproperty.md index a2516f27..8493699f 100644 --- a/docs/api/puppeteer.jshandle.getproperty.md +++ b/docs/api/puppeteer.jshandle.getproperty.md @@ -10,7 +10,7 @@ Fetches a single property from the referenced object. ```typescript class JSHandle { - abstract getProperty( + getProperty( propertyName: HandleOr ): Promise>; } diff --git a/docs/api/puppeteer.jshandle.getproperty_1.md b/docs/api/puppeteer.jshandle.getproperty_1.md index 7c2c9f00..a61431d4 100644 --- a/docs/api/puppeteer.jshandle.getproperty_1.md +++ b/docs/api/puppeteer.jshandle.getproperty_1.md @@ -8,7 +8,7 @@ sidebar_label: JSHandle.getProperty_1 ```typescript class JSHandle { - abstract getProperty(propertyName: string): Promise>; + getProperty(propertyName: string): Promise>; } ``` diff --git a/packages/puppeteer-core/src/api/ElementHandle.ts b/packages/puppeteer-core/src/api/ElementHandle.ts index b8aa7cab..3ffdf0c1 100644 --- a/packages/puppeteer-core/src/api/ElementHandle.ts +++ b/packages/puppeteer-core/src/api/ElementHandle.ts @@ -142,7 +142,7 @@ export abstract class ElementHandle< /** * @internal */ - protected handle; + protected readonly handle; /** * @internal @@ -208,7 +208,7 @@ export abstract class ElementHandle< /** * @internal */ - override evaluateHandle< + override async evaluateHandle< Params extends unknown[], Func extends EvaluateFuncWith = EvaluateFuncWith< ElementType, @@ -218,7 +218,7 @@ export abstract class ElementHandle< pageFunction: Func | string, ...args: Params ): Promise>>> { - return this.handle.evaluateHandle(pageFunction, ...args); + return await this.handle.evaluateHandle(pageFunction, ...args); } /** @@ -1269,11 +1269,6 @@ export abstract class ElementHandle< }); } - /** - * @internal - */ - abstract assertElementHasWorld(): asserts this; - /** * If the element is a form input, you can use {@link ElementHandle.autofill} * to test if the form is compatible with the browser's autofill diff --git a/packages/puppeteer-core/src/api/Environment.ts b/packages/puppeteer-core/src/api/Environment.ts new file mode 100644 index 00000000..29e3cdab --- /dev/null +++ b/packages/puppeteer-core/src/api/Environment.ts @@ -0,0 +1,27 @@ +/** + * Copyright 2023 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import {CDPSession} from '../common/Connection.js'; + +import {Realm} from './Realm.js'; + +/** + * @internal + */ +export interface Environment { + get client(): CDPSession; + mainRealm(): Realm; +} diff --git a/packages/puppeteer-core/src/api/Frame.ts b/packages/puppeteer-core/src/api/Frame.ts index 3134c3ed..34b6dce5 100644 --- a/packages/puppeteer-core/src/api/Frame.ts +++ b/packages/puppeteer-core/src/api/Frame.ts @@ -20,7 +20,6 @@ import {Page, WaitTimeoutOptions} from '../api/Page.js'; import {CDPSession} from '../common/Connection.js'; import {DeviceRequestPrompt} from '../common/DeviceRequestPrompt.js'; import {EventEmitter} from '../common/EventEmitter.js'; -import {ExecutionContext} from '../common/ExecutionContext.js'; import {getQueryHandlerAndSelector} from '../common/GetQueryHandler.js'; import {transposeIterableHandle} from '../common/HandleIterator.js'; import { @@ -309,14 +308,7 @@ export abstract class Frame extends EventEmitter { /** * @internal */ - abstract _client(): CDPSession; - - /** - * @internal - */ - executionContext(): Promise { - throw new Error('Not implemented'); - } + abstract get client(): CDPSession; /** * @internal @@ -475,7 +467,7 @@ export abstract class Frame extends EventEmitter { * @returns A promise to the result of the function. */ @throwIfDetached - $eval< + async $eval< Selector extends string, Params extends unknown[], Func extends EvaluateFuncWith, Params> = EvaluateFuncWith< @@ -488,7 +480,7 @@ export abstract class Frame extends EventEmitter { ...args: Params ): Promise>> { pageFunction = withSourcePuppeteerURLIfNone(this.$eval.name, pageFunction); - return this.mainRealm().$eval(selector, pageFunction, ...args); + return await this.mainRealm().$eval(selector, pageFunction, ...args); } /** @@ -512,7 +504,7 @@ export abstract class Frame extends EventEmitter { * @returns A promise to the result of the function. */ @throwIfDetached - $$eval< + async $$eval< Selector extends string, Params extends unknown[], Func extends EvaluateFuncWith< @@ -525,7 +517,7 @@ export abstract class Frame extends EventEmitter { ...args: Params ): Promise>> { pageFunction = withSourcePuppeteerURLIfNone(this.$$eval.name, pageFunction); - return this.mainRealm().$$eval(selector, pageFunction, ...args); + return await this.mainRealm().$$eval(selector, pageFunction, ...args); } /** @@ -539,8 +531,8 @@ export abstract class Frame extends EventEmitter { * @param expression - the XPath expression to evaluate. */ @throwIfDetached - $x(expression: string): Promise>> { - return this.mainRealm().$x(expression); + async $x(expression: string): Promise>> { + return await this.mainRealm().$x(expression); } /** @@ -659,7 +651,7 @@ export abstract class Frame extends EventEmitter { * @returns the promise which resolve when the `pageFunction` returns a truthy value. */ @throwIfDetached - waitForFunction< + async waitForFunction< Params extends unknown[], Func extends EvaluateFunc = EvaluateFunc, >( @@ -667,18 +659,18 @@ export abstract class Frame extends EventEmitter { options: FrameWaitForFunctionOptions = {}, ...args: Params ): Promise>>> { - return this.mainRealm().waitForFunction( + return await (this.mainRealm().waitForFunction( pageFunction, options, ...args - ) as Promise>>>; + ) as Promise>>>); } /** * The full HTML contents of the frame, including the DOCTYPE. */ @throwIfDetached - content(): Promise { - return this.isolatedRealm().content(); + async content(): Promise { + return await this.isolatedRealm().content(); } /** @@ -922,8 +914,11 @@ export abstract class Frame extends EventEmitter { * @param selector - The selector to query for. */ @throwIfDetached - click(selector: string, options: Readonly = {}): Promise { - return this.isolatedRealm().click(selector, options); + async click( + selector: string, + options: Readonly = {} + ): Promise { + return await this.isolatedRealm().click(selector, options); } /** @@ -933,8 +928,8 @@ export abstract class Frame extends EventEmitter { * @throws Throws if there's no element matching `selector`. */ @throwIfDetached - focus(selector: string): Promise { - return this.isolatedRealm().focus(selector); + async focus(selector: string): Promise { + return await this.isolatedRealm().focus(selector); } /** @@ -945,8 +940,8 @@ export abstract class Frame extends EventEmitter { * @throws Throws if there's no element matching `selector`. */ @throwIfDetached - hover(selector: string): Promise { - return this.isolatedRealm().hover(selector); + async hover(selector: string): Promise { + return await this.isolatedRealm().hover(selector); } /** @@ -968,8 +963,8 @@ export abstract class Frame extends EventEmitter { * @throws Throws if there's no `