From 2808240c7130ad306ff7f4f3d46abd2465fec5f7 Mon Sep 17 00:00:00 2001 From: Nikolay Vitkov <34244704+Lightning00Blade@users.noreply.github.com> Date: Mon, 15 May 2023 16:39:47 +0200 Subject: [PATCH] chore: implement Frames for BiDi (#10121) --- .github/workflows/ci.yml | 3 +- package-lock.json | 16 +- packages/puppeteer-core/package.json | 2 +- .../puppeteer-core/src/api/ElementHandle.ts | 2 +- packages/puppeteer-core/src/api/Frame.ts | 879 ++++++++++++++++++ .../puppeteer-core/src/api/HTTPRequest.ts | 2 +- .../puppeteer-core/src/api/HTTPResponse.ts | 2 +- packages/puppeteer-core/src/api/Page.ts | 12 +- packages/puppeteer-core/src/api/api.ts | 1 + .../src/common/ElementHandle.ts | 11 +- packages/puppeteer-core/src/common/Frame.ts | 681 +------------- .../puppeteer-core/src/common/FrameManager.ts | 7 +- .../puppeteer-core/src/common/FrameTree.ts | 5 +- .../puppeteer-core/src/common/HTTPRequest.ts | 2 +- .../puppeteer-core/src/common/HTTPResponse.ts | 2 +- packages/puppeteer-core/src/common/Page.ts | 24 +- .../puppeteer-core/src/common/QueryHandler.ts | 2 +- .../puppeteer-core/src/common/bidi/Browser.ts | 8 +- .../src/common/bidi/BrowserContext.ts | 35 +- .../src/common/bidi/Connection.ts | 25 +- .../puppeteer-core/src/common/bidi/Context.ts | 55 +- .../puppeteer-core/src/common/bidi/Frame.ts | 125 +++ .../src/common/bidi/FrameManager.ts | 156 ++++ .../puppeteer-core/src/common/bidi/Page.ts | 142 ++- .../src/common/bidi/Serializer.ts | 12 +- .../puppeteer-core/src/common/bidi/utils.ts | 2 +- packages/puppeteer-core/src/common/common.ts | 1 - test/TestExpectations.json | 62 +- test/src/frame.spec.ts | 2 +- test/src/page.spec.ts | 6 +- test/src/utils.ts | 2 +- tools/mochaRunner/src/main.ts | 1 + 32 files changed, 1521 insertions(+), 766 deletions(-) create mode 100644 packages/puppeteer-core/src/api/Frame.ts create mode 100644 packages/puppeteer-core/src/common/bidi/Frame.ts create mode 100644 packages/puppeteer-core/src/common/bidi/FrameManager.ts diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c732a56bef2..9f58ca2c974 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -194,7 +194,8 @@ jobs: matrix: os: - ubuntu-latest - - macos-latest + # Disabled as BiDi has issue on mac https://bugzilla.mozilla.org/show_bug.cgi?id=1832778 + # - macos-latest suite: - firefox-bidi - firefox-headful diff --git a/package-lock.json b/package-lock.json index df425baa9cb..c09cdec8590 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2710,9 +2710,9 @@ "license": "ISC" }, "node_modules/chromium-bidi": { - "version": "0.4.7", - "resolved": "https://registry.npmjs.org/chromium-bidi/-/chromium-bidi-0.4.7.tgz", - "integrity": "sha512-6+mJuFXwTMU6I3vYLs6IL8A1DyQTPjCfIL971X0aMPVGRbGnNfl6i6Cl0NMbxi2bRYLGESt9T2ZIMRM5PAEcIQ==", + "version": "0.4.9", + "resolved": "https://registry.npmjs.org/chromium-bidi/-/chromium-bidi-0.4.9.tgz", + "integrity": "sha512-u3DC6XwgLCA9QJ5ak1voPslCmacQdulZNCPsI3qNXxSnEcZS7DFIbww+5RM2bznMEje7cc0oydavRLRvOIZtHw==", "dependencies": { "mitt": "3.0.0" }, @@ -9464,7 +9464,7 @@ "license": "Apache-2.0", "dependencies": { "@puppeteer/browsers": "1.2.0", - "chromium-bidi": "0.4.7", + "chromium-bidi": "0.4.9", "cross-fetch": "3.1.5", "debug": "4.3.4", "devtools-protocol": "0.0.1120988", @@ -11441,9 +11441,9 @@ "version": "1.1.4" }, "chromium-bidi": { - "version": "0.4.7", - "resolved": "https://registry.npmjs.org/chromium-bidi/-/chromium-bidi-0.4.7.tgz", - "integrity": "sha512-6+mJuFXwTMU6I3vYLs6IL8A1DyQTPjCfIL971X0aMPVGRbGnNfl6i6Cl0NMbxi2bRYLGESt9T2ZIMRM5PAEcIQ==", + "version": "0.4.9", + "resolved": "https://registry.npmjs.org/chromium-bidi/-/chromium-bidi-0.4.9.tgz", + "integrity": "sha512-u3DC6XwgLCA9QJ5ak1voPslCmacQdulZNCPsI3qNXxSnEcZS7DFIbww+5RM2bznMEje7cc0oydavRLRvOIZtHw==", "requires": { "mitt": "3.0.0" } @@ -14503,7 +14503,7 @@ "version": "file:packages/puppeteer-core", "requires": { "@puppeteer/browsers": "1.2.0", - "chromium-bidi": "0.4.7", + "chromium-bidi": "0.4.9", "cross-fetch": "3.1.5", "debug": "4.3.4", "devtools-protocol": "0.0.1120988", diff --git a/packages/puppeteer-core/package.json b/packages/puppeteer-core/package.json index 5623922f6e0..0b5c6caf540 100644 --- a/packages/puppeteer-core/package.json +++ b/packages/puppeteer-core/package.json @@ -132,7 +132,7 @@ "author": "The Chromium Authors", "license": "Apache-2.0", "dependencies": { - "chromium-bidi": "0.4.7", + "chromium-bidi": "0.4.9", "cross-fetch": "3.1.5", "debug": "4.3.4", "devtools-protocol": "0.0.1120988", diff --git a/packages/puppeteer-core/src/api/ElementHandle.ts b/packages/puppeteer-core/src/api/ElementHandle.ts index 09c409736ed..f4b30a391a4 100644 --- a/packages/puppeteer-core/src/api/ElementHandle.ts +++ b/packages/puppeteer-core/src/api/ElementHandle.ts @@ -18,7 +18,6 @@ import {Protocol} from 'devtools-protocol'; import {CDPSession} from '../common/Connection.js'; import {ExecutionContext} from '../common/ExecutionContext.js'; -import {Frame} from '../common/Frame.js'; import {MouseClickOptions} from '../common/Input.js'; import {WaitForSelectorOptions} from '../common/IsolatedWorld.js'; import { @@ -30,6 +29,7 @@ import { } from '../common/types.js'; import {KeyInput} from '../common/USKeyboardLayout.js'; +import {Frame} from './Frame.js'; import {JSHandle} from './JSHandle.js'; import {ScreenshotOptions} from './Page.js'; diff --git a/packages/puppeteer-core/src/api/Frame.ts b/packages/puppeteer-core/src/api/Frame.ts new file mode 100644 index 00000000000..00dac50c079 --- /dev/null +++ b/packages/puppeteer-core/src/api/Frame.ts @@ -0,0 +1,879 @@ +/** + * 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 {ClickOptions, ElementHandle} from '../api/ElementHandle.js'; +import {HTTPResponse} from '../api/HTTPResponse.js'; +import {Page, WaitTimeoutOptions} from '../api/Page.js'; +import {CDPSession} from '../common/Connection.js'; +import {DeviceRequestPrompt} from '../common/DeviceRequestPrompt.js'; +import {ExecutionContext} from '../common/ExecutionContext.js'; +import { + IsolatedWorldChart, + WaitForSelectorOptions, +} from '../common/IsolatedWorld.js'; +import {PuppeteerLifeCycleEvent} from '../common/LifecycleWatcher.js'; +import { + EvaluateFunc, + EvaluateFuncWith, + HandleFor, + NodeFor, +} from '../common/types.js'; + +/** + * @public + */ +export interface FrameWaitForFunctionOptions { + /** + * An interval at which the `pageFunction` is executed, defaults to `raf`. If + * `polling` is a number, then it is treated as an interval in milliseconds at + * which the function would be executed. If `polling` is a string, then it can + * be one of the following values: + * + * - `raf` - to constantly execute `pageFunction` in `requestAnimationFrame` + * callback. This is the tightest polling mode which is suitable to observe + * styling changes. + * + * - `mutation` - to execute `pageFunction` on every DOM mutation. + */ + polling?: 'raf' | 'mutation' | number; + /** + * Maximum time to wait in milliseconds. Defaults to `30000` (30 seconds). + * Pass `0` to disable the timeout. Puppeteer's default timeout can be changed + * using {@link Page.setDefaultTimeout}. + */ + timeout?: number; + /** + * A signal object that allows you to cancel a waitForFunction call. + */ + signal?: AbortSignal; +} + +/** + * @public + */ +export interface FrameAddScriptTagOptions { + /** + * URL of the script to be added. + */ + url?: string; + /** + * Path to a JavaScript file to be injected into the frame. + * + * @remarks + * If `path` is a relative path, it is resolved relative to the current + * working directory (`process.cwd()` in Node.js). + */ + path?: string; + /** + * JavaScript to be injected into the frame. + */ + content?: string; + /** + * Sets the `type` of the script. Use `module` in order to load an ES2015 module. + */ + type?: string; + /** + * Sets the `id` of the script. + */ + id?: string; +} + +/** + * @public + */ +export interface FrameAddStyleTagOptions { + /** + * the URL of the CSS file to be added. + */ + url?: string; + /** + * The path to a CSS file to be injected into the frame. + * @remarks + * If `path` is a relative path, it is resolved relative to the current + * working directory (`process.cwd()` in Node.js). + */ + path?: string; + /** + * Raw CSS content to be injected into the frame. + */ + content?: string; +} + +/** + * Represents a DOM frame. + * + * To understand frames, you can think of frames as `