2019-12-19 16:53:24 -08:00
|
|
|
/**
|
|
|
|
|
* Copyright 2019 Google Inc. All rights reserved.
|
|
|
|
|
* Modifications copyright (c) Microsoft Corporation.
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2020-08-24 06:51:51 -07:00
|
|
|
import * as dialog from '../dialog';
|
|
|
|
|
import * as dom from '../dom';
|
2022-04-06 13:57:14 -08:00
|
|
|
import type * as frames from '../frames';
|
2022-04-07 19:18:22 -08:00
|
|
|
import type { RegisteredListener } from '../../utils/eventsHelper';
|
|
|
|
|
import { eventsHelper } from '../../utils/eventsHelper';
|
2024-08-07 06:20:12 -07:00
|
|
|
import type { PageDelegate } from '../page';
|
2024-06-27 09:29:20 -07:00
|
|
|
import { InitScript } from '../page';
|
2022-04-06 13:57:14 -08:00
|
|
|
import { Page, Worker } from '../page';
|
|
|
|
|
import type * as types from '../types';
|
2020-03-05 17:22:57 -08:00
|
|
|
import { getAccessibilityTree } from './ffAccessibility';
|
2022-04-06 13:57:14 -08:00
|
|
|
import type { FFBrowserContext } from './ffBrowser';
|
2023-09-27 14:09:56 -07:00
|
|
|
import { FFSession } from './ffConnection';
|
2019-12-19 16:53:24 -08:00
|
|
|
import { FFExecutionContext } from './ffExecutionContext';
|
2020-10-19 10:07:33 -07:00
|
|
|
import { RawKeyboardImpl, RawMouseImpl, RawTouchscreenImpl } from './ffInput';
|
2020-08-18 15:38:29 -07:00
|
|
|
import { FFNetworkManager } from './ffNetworkManager';
|
2022-04-06 13:57:14 -08:00
|
|
|
import type { Protocol } from './protocol';
|
|
|
|
|
import type { Progress } from '../progress';
|
2021-04-19 22:37:38 +02:00
|
|
|
import { splitErrorMessage } from '../../utils/stackTrace';
|
2024-01-30 14:36:51 -08:00
|
|
|
import { debugLogger } from '../../utils/debugLogger';
|
2023-05-04 15:11:46 -07:00
|
|
|
import { BrowserContext } from '../browserContext';
|
2023-10-17 21:34:02 -07:00
|
|
|
import { TargetClosedError } from '../errors';
|
2019-12-19 16:53:24 -08:00
|
|
|
|
2021-05-12 22:19:27 +00:00
|
|
|
export const UTILITY_WORLD_NAME = '__playwright_utility_world__';
|
2020-01-16 12:57:28 -08:00
|
|
|
|
2019-12-23 11:39:57 -08:00
|
|
|
export class FFPage implements PageDelegate {
|
2024-03-13 12:22:40 +01:00
|
|
|
readonly cspErrorsAsynchronousForInlineScripts = true;
|
2019-12-19 16:53:24 -08:00
|
|
|
readonly rawMouse: RawMouseImpl;
|
|
|
|
|
readonly rawKeyboard: RawKeyboardImpl;
|
2020-10-19 10:07:33 -07:00
|
|
|
readonly rawTouchscreen: RawTouchscreenImpl;
|
2019-12-19 16:53:24 -08:00
|
|
|
readonly _session: FFSession;
|
|
|
|
|
readonly _page: Page;
|
2019-12-30 14:05:28 -08:00
|
|
|
readonly _networkManager: FFNetworkManager;
|
2020-03-09 12:32:42 -07:00
|
|
|
readonly _browserContext: FFBrowserContext;
|
2024-12-14 20:15:58 +00:00
|
|
|
private _reportedAsNew = false;
|
2020-04-29 18:36:24 -07:00
|
|
|
readonly _opener: FFPage | null;
|
2019-12-19 16:53:24 -08:00
|
|
|
private readonly _contextIdToContext: Map<string, dom.FrameExecutionContext>;
|
|
|
|
|
private _eventListeners: RegisteredListener[];
|
2020-01-17 17:51:02 -08:00
|
|
|
private _workers = new Map<string, { frameId: string, session: FFSession }>();
|
2021-05-08 11:35:36 -07:00
|
|
|
private _screencastId: string | undefined;
|
2024-06-27 09:29:20 -07:00
|
|
|
private _initScripts: { initScript: InitScript, worldName?: string }[] = [];
|
2019-12-19 16:53:24 -08:00
|
|
|
|
2020-03-09 12:32:42 -07:00
|
|
|
constructor(session: FFSession, browserContext: FFBrowserContext, opener: FFPage | null) {
|
2019-12-19 16:53:24 -08:00
|
|
|
this._session = session;
|
2020-03-09 12:32:42 -07:00
|
|
|
this._opener = opener;
|
2019-12-19 16:53:24 -08:00
|
|
|
this.rawKeyboard = new RawKeyboardImpl(session);
|
|
|
|
|
this.rawMouse = new RawMouseImpl(session);
|
2020-10-19 10:07:33 -07:00
|
|
|
this.rawTouchscreen = new RawTouchscreenImpl(session);
|
2019-12-19 16:53:24 -08:00
|
|
|
this._contextIdToContext = new Map();
|
2020-03-09 12:32:42 -07:00
|
|
|
this._browserContext = browserContext;
|
2019-12-19 16:53:24 -08:00
|
|
|
this._page = new Page(this, browserContext);
|
2021-09-14 15:22:52 -04:00
|
|
|
this.rawMouse.setPage(this._page);
|
2019-12-19 16:53:24 -08:00
|
|
|
this._networkManager = new FFNetworkManager(session, this._page);
|
2020-08-21 16:26:33 -07:00
|
|
|
this._page.on(Page.Events.FrameDetached, frame => this._removeContextsForFrame(frame));
|
2020-05-13 17:20:33 -07:00
|
|
|
// TODO: remove Page.willOpenNewWindowAsynchronously from the protocol.
|
2019-12-19 16:53:24 -08:00
|
|
|
this._eventListeners = [
|
2021-07-07 21:14:16 +02:00
|
|
|
eventsHelper.addEventListener(this._session, 'Page.eventFired', this._onEventFired.bind(this)),
|
|
|
|
|
eventsHelper.addEventListener(this._session, 'Page.frameAttached', this._onFrameAttached.bind(this)),
|
|
|
|
|
eventsHelper.addEventListener(this._session, 'Page.frameDetached', this._onFrameDetached.bind(this)),
|
|
|
|
|
eventsHelper.addEventListener(this._session, 'Page.navigationAborted', this._onNavigationAborted.bind(this)),
|
|
|
|
|
eventsHelper.addEventListener(this._session, 'Page.navigationCommitted', this._onNavigationCommitted.bind(this)),
|
|
|
|
|
eventsHelper.addEventListener(this._session, 'Page.navigationStarted', this._onNavigationStarted.bind(this)),
|
|
|
|
|
eventsHelper.addEventListener(this._session, 'Page.sameDocumentNavigation', this._onSameDocumentNavigation.bind(this)),
|
|
|
|
|
eventsHelper.addEventListener(this._session, 'Runtime.executionContextCreated', this._onExecutionContextCreated.bind(this)),
|
|
|
|
|
eventsHelper.addEventListener(this._session, 'Runtime.executionContextDestroyed', this._onExecutionContextDestroyed.bind(this)),
|
2022-10-24 16:03:56 -07:00
|
|
|
eventsHelper.addEventListener(this._session, 'Runtime.executionContextsCleared', this._onExecutionContextsCleared.bind(this)),
|
2021-07-07 21:14:16 +02:00
|
|
|
eventsHelper.addEventListener(this._session, 'Page.linkClicked', event => this._onLinkClicked(event.phase)),
|
|
|
|
|
eventsHelper.addEventListener(this._session, 'Page.uncaughtError', this._onUncaughtError.bind(this)),
|
|
|
|
|
eventsHelper.addEventListener(this._session, 'Runtime.console', this._onConsole.bind(this)),
|
|
|
|
|
eventsHelper.addEventListener(this._session, 'Page.dialogOpened', this._onDialogOpened.bind(this)),
|
|
|
|
|
eventsHelper.addEventListener(this._session, 'Page.bindingCalled', this._onBindingCalled.bind(this)),
|
|
|
|
|
eventsHelper.addEventListener(this._session, 'Page.fileChooserOpened', this._onFileChooserOpened.bind(this)),
|
|
|
|
|
eventsHelper.addEventListener(this._session, 'Page.workerCreated', this._onWorkerCreated.bind(this)),
|
|
|
|
|
eventsHelper.addEventListener(this._session, 'Page.workerDestroyed', this._onWorkerDestroyed.bind(this)),
|
|
|
|
|
eventsHelper.addEventListener(this._session, 'Page.dispatchMessageFromWorker', this._onDispatchMessageFromWorker.bind(this)),
|
|
|
|
|
eventsHelper.addEventListener(this._session, 'Page.crashed', this._onCrashed.bind(this)),
|
|
|
|
|
eventsHelper.addEventListener(this._session, 'Page.videoRecordingStarted', this._onVideoRecordingStarted.bind(this)),
|
|
|
|
|
|
|
|
|
|
eventsHelper.addEventListener(this._session, 'Page.webSocketCreated', this._onWebSocketCreated.bind(this)),
|
|
|
|
|
eventsHelper.addEventListener(this._session, 'Page.webSocketClosed', this._onWebSocketClosed.bind(this)),
|
|
|
|
|
eventsHelper.addEventListener(this._session, 'Page.webSocketFrameReceived', this._onWebSocketFrameReceived.bind(this)),
|
|
|
|
|
eventsHelper.addEventListener(this._session, 'Page.webSocketFrameSent', this._onWebSocketFrameSent.bind(this)),
|
|
|
|
|
eventsHelper.addEventListener(this._session, 'Page.screencastFrame', this._onScreencastFrame.bind(this)),
|
2021-05-08 11:35:36 -07:00
|
|
|
|
2019-12-19 16:53:24 -08:00
|
|
|
];
|
2024-12-14 20:15:58 +00:00
|
|
|
this._session.once('Page.ready', () => {
|
|
|
|
|
if (this._reportedAsNew)
|
2021-06-08 17:28:24 -07:00
|
|
|
return;
|
2024-12-14 20:15:58 +00:00
|
|
|
this._reportedAsNew = true;
|
|
|
|
|
this._page.reportAsNew(this._opener?._page);
|
2020-03-26 16:13:11 -07:00
|
|
|
});
|
|
|
|
|
// Ideally, we somehow ensure that utility world is created before Page.ready arrives, but currently it is racy.
|
|
|
|
|
// Therefore, we can end up with an initialized page without utility world, although very unlikely.
|
2024-08-07 06:20:12 -07:00
|
|
|
this.addInitScript(new InitScript('', true), UTILITY_WORLD_NAME).catch(e => this._markAsError(e));
|
2021-03-30 17:35:42 -07:00
|
|
|
}
|
|
|
|
|
|
2021-04-02 11:15:07 -07:00
|
|
|
async _markAsError(error: Error) {
|
2024-12-14 20:15:58 +00:00
|
|
|
// Same error may be reported twice: channel disconnected and session.send fails.
|
|
|
|
|
if (this._reportedAsNew)
|
2021-04-02 11:15:07 -07:00
|
|
|
return;
|
2024-12-14 20:15:58 +00:00
|
|
|
this._reportedAsNew = true;
|
|
|
|
|
this._page.reportAsNew(this._opener?._page, error);
|
2020-01-16 12:57:28 -08:00
|
|
|
}
|
|
|
|
|
|
2020-10-30 10:34:24 -07:00
|
|
|
_onWebSocketCreated(event: Protocol.Page.webSocketCreatedPayload) {
|
|
|
|
|
this._page._frameManager.onWebSocketCreated(webSocketId(event.frameId, event.wsid), event.requestURL);
|
|
|
|
|
this._page._frameManager.onWebSocketRequest(webSocketId(event.frameId, event.wsid));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_onWebSocketClosed(event: Protocol.Page.webSocketClosedPayload) {
|
|
|
|
|
if (event.error)
|
|
|
|
|
this._page._frameManager.webSocketError(webSocketId(event.frameId, event.wsid), event.error);
|
|
|
|
|
this._page._frameManager.webSocketClosed(webSocketId(event.frameId, event.wsid));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_onWebSocketFrameReceived(event: Protocol.Page.webSocketFrameReceivedPayload) {
|
|
|
|
|
this._page._frameManager.webSocketFrameReceived(webSocketId(event.frameId, event.wsid), event.opcode, event.data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_onWebSocketFrameSent(event: Protocol.Page.webSocketFrameSentPayload) {
|
|
|
|
|
this._page._frameManager.onWebSocketFrameSent(webSocketId(event.frameId, event.wsid), event.opcode, event.data);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-13 22:08:35 -08:00
|
|
|
_onExecutionContextCreated(payload: Protocol.Runtime.executionContextCreatedPayload) {
|
2021-09-27 18:58:08 +02:00
|
|
|
const { executionContextId, auxData } = payload;
|
2021-05-12 16:03:15 +00:00
|
|
|
const frame = this._page._frameManager.frame(auxData.frameId!);
|
2019-12-19 16:53:24 -08:00
|
|
|
if (!frame)
|
|
|
|
|
return;
|
|
|
|
|
const delegate = new FFExecutionContext(this._session, executionContextId);
|
2024-09-23 15:48:11 -07:00
|
|
|
let worldName: types.World|null = null;
|
2020-01-16 12:57:28 -08:00
|
|
|
if (auxData.name === UTILITY_WORLD_NAME)
|
2020-12-02 13:43:16 -08:00
|
|
|
worldName = 'utility';
|
2020-01-16 12:57:28 -08:00
|
|
|
else if (!auxData.name)
|
2020-12-02 13:43:16 -08:00
|
|
|
worldName = 'main';
|
|
|
|
|
const context = new dom.FrameExecutionContext(delegate, frame, worldName);
|
2021-11-03 10:44:50 -07:00
|
|
|
(context as any)[contextDelegateSymbol] = delegate;
|
2024-09-23 15:48:11 -07:00
|
|
|
if (worldName)
|
|
|
|
|
frame._contextCreated(worldName, context);
|
2019-12-19 16:53:24 -08:00
|
|
|
this._contextIdToContext.set(executionContextId, context);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-13 22:08:35 -08:00
|
|
|
_onExecutionContextDestroyed(payload: Protocol.Runtime.executionContextDestroyedPayload) {
|
2021-09-27 18:58:08 +02:00
|
|
|
const { executionContextId } = payload;
|
2019-12-19 16:53:24 -08:00
|
|
|
const context = this._contextIdToContext.get(executionContextId);
|
|
|
|
|
if (!context)
|
|
|
|
|
return;
|
|
|
|
|
this._contextIdToContext.delete(executionContextId);
|
2020-02-07 13:38:50 -08:00
|
|
|
context.frame._contextDestroyed(context);
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
2022-10-24 16:03:56 -07:00
|
|
|
_onExecutionContextsCleared() {
|
|
|
|
|
for (const executionContextId of Array.from(this._contextIdToContext.keys()))
|
|
|
|
|
this._onExecutionContextDestroyed({ executionContextId });
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-23 14:58:30 -08:00
|
|
|
private _removeContextsForFrame(frame: frames.Frame) {
|
|
|
|
|
for (const [contextId, context] of this._contextIdToContext) {
|
|
|
|
|
if (context.frame === frame)
|
|
|
|
|
this._contextIdToContext.delete(contextId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-05 14:47:04 -08:00
|
|
|
_onLinkClicked(phase: 'before' | 'after') {
|
|
|
|
|
if (phase === 'before')
|
|
|
|
|
this._page._frameManager.frameWillPotentiallyRequestNavigation();
|
|
|
|
|
else
|
|
|
|
|
this._page._frameManager.frameDidPotentiallyRequestNavigation();
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-09 19:03:06 -07:00
|
|
|
_onNavigationStarted(params: Protocol.Page.navigationStartedPayload) {
|
|
|
|
|
this._page._frameManager.frameRequestedNavigation(params.frameId, params.navigationId);
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_onNavigationAborted(params: Protocol.Page.navigationAbortedPayload) {
|
2020-07-06 15:58:27 -07:00
|
|
|
this._page._frameManager.frameAbortedNavigation(params.frameId, params.errorText, params.navigationId);
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_onNavigationCommitted(params: Protocol.Page.navigationCommittedPayload) {
|
2020-01-17 17:51:02 -08:00
|
|
|
for (const [workerId, worker] of this._workers) {
|
|
|
|
|
if (worker.frameId === params.frameId)
|
|
|
|
|
this._onWorkerDestroyed({ workerId });
|
|
|
|
|
}
|
2019-12-19 16:53:24 -08:00
|
|
|
this._page._frameManager.frameCommittedNewDocumentNavigation(params.frameId, params.url, params.name || '', params.navigationId || '', false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_onSameDocumentNavigation(params: Protocol.Page.sameDocumentNavigationPayload) {
|
|
|
|
|
this._page._frameManager.frameCommittedSameDocumentNavigation(params.frameId, params.url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_onFrameAttached(params: Protocol.Page.frameAttachedPayload) {
|
|
|
|
|
this._page._frameManager.frameAttached(params.frameId, params.parentFrameId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_onFrameDetached(params: Protocol.Page.frameDetachedPayload) {
|
|
|
|
|
this._page._frameManager.frameDetached(params.frameId);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-13 22:08:35 -08:00
|
|
|
_onEventFired(payload: Protocol.Page.eventFiredPayload) {
|
2021-09-27 18:58:08 +02:00
|
|
|
const { frameId, name } = payload;
|
2019-12-19 16:53:24 -08:00
|
|
|
if (name === 'load')
|
|
|
|
|
this._page._frameManager.frameLifecycleEvent(frameId, 'load');
|
|
|
|
|
if (name === 'DOMContentLoaded')
|
|
|
|
|
this._page._frameManager.frameLifecycleEvent(frameId, 'domcontentloaded');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_onUncaughtError(params: Protocol.Page.uncaughtErrorPayload) {
|
2021-07-01 14:31:20 -07:00
|
|
|
const { name, message } = splitErrorMessage(params.message);
|
2020-04-20 11:37:02 -07:00
|
|
|
const error = new Error(message);
|
2021-07-01 14:31:20 -07:00
|
|
|
error.stack = params.message + '\n' + params.stack.split('\n').filter(Boolean).map(a => a.replace(/([^@]*)@(.*)/, ' at $1 ($2)')).join('\n');
|
2021-04-19 22:37:38 +02:00
|
|
|
error.name = name;
|
2023-08-17 09:10:03 -07:00
|
|
|
this._page.emitOnContextOnceInitialized(BrowserContext.Events.PageError, error, this._page);
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
2020-01-13 22:08:35 -08:00
|
|
|
_onConsole(payload: Protocol.Runtime.consolePayload) {
|
2021-09-27 18:58:08 +02:00
|
|
|
const { type, args, executionContextId, location } = payload;
|
2021-10-27 13:28:52 -07:00
|
|
|
const context = this._contextIdToContext.get(executionContextId);
|
|
|
|
|
if (!context)
|
|
|
|
|
return;
|
2024-04-23 15:07:29 -07:00
|
|
|
// Juggler reports 'warn' for some internal messages generated by the browser.
|
|
|
|
|
this._page._addConsoleMessage(type === 'warn' ? 'warning' : type, args.map(arg => context.createHandle(arg)), location);
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_onDialogOpened(params: Protocol.Page.dialogOpenedPayload) {
|
2023-05-04 15:11:46 -07:00
|
|
|
this._page.emitOnContext(BrowserContext.Events.Dialog, new dialog.Dialog(
|
2021-01-15 18:30:55 -08:00
|
|
|
this._page,
|
2020-02-07 13:38:50 -08:00
|
|
|
params.type,
|
|
|
|
|
params.message,
|
|
|
|
|
async (accept: boolean, promptText?: string) => {
|
2020-06-05 07:50:26 -07:00
|
|
|
await this._session.sendMayFail('Page.handleDialog', { dialogId: params.dialogId, accept, promptText });
|
2020-02-07 13:38:50 -08:00
|
|
|
},
|
|
|
|
|
params.defaultValue));
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
2020-07-14 13:34:49 -07:00
|
|
|
async _onBindingCalled(event: Protocol.Page.bindingCalledPayload) {
|
2024-12-14 20:15:58 +00:00
|
|
|
const pageOrError = await this._page.waitForInitializedOrError();
|
2021-10-27 13:28:52 -07:00
|
|
|
if (!(pageOrError instanceof Error)) {
|
|
|
|
|
const context = this._contextIdToContext.get(event.executionContextId);
|
|
|
|
|
if (context)
|
|
|
|
|
await this._page._onBindingCalled(event.payload, context);
|
|
|
|
|
}
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
2020-01-13 22:08:35 -08:00
|
|
|
async _onFileChooserOpened(payload: Protocol.Page.fileChooserOpenedPayload) {
|
2021-09-27 18:58:08 +02:00
|
|
|
const { executionContextId, element } = payload;
|
2021-10-27 13:28:52 -07:00
|
|
|
const context = this._contextIdToContext.get(executionContextId);
|
|
|
|
|
if (!context)
|
|
|
|
|
return;
|
2020-05-15 15:21:49 -07:00
|
|
|
const handle = context.createHandle(element).asElement()!;
|
2021-03-22 09:59:39 -07:00
|
|
|
await this._page._onFileChooserOpened(handle);
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
2020-01-17 17:51:02 -08:00
|
|
|
async _onWorkerCreated(event: Protocol.Page.workerCreatedPayload) {
|
|
|
|
|
const workerId = event.workerId;
|
2021-02-09 09:00:00 -08:00
|
|
|
const worker = new Worker(this._page, event.url);
|
2021-08-26 18:44:49 -07:00
|
|
|
const workerSession = new FFSession(this._session._connection, workerId, (message: any) => {
|
2020-01-17 17:51:02 -08:00
|
|
|
this._session.send('Page.sendMessageToWorker', {
|
|
|
|
|
frameId: event.frameId,
|
|
|
|
|
workerId: workerId,
|
|
|
|
|
message: JSON.stringify(message)
|
|
|
|
|
}).catch(e => {
|
|
|
|
|
workerSession.dispatchMessage({ id: message.id, method: '', params: {}, error: { message: e.message, data: undefined } });
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
this._workers.set(workerId, { session: workerSession, frameId: event.frameId });
|
|
|
|
|
this._page._addWorker(workerId, worker);
|
|
|
|
|
workerSession.once('Runtime.executionContextCreated', event => {
|
|
|
|
|
worker._createExecutionContext(new FFExecutionContext(workerSession, event.executionContextId));
|
|
|
|
|
});
|
|
|
|
|
workerSession.on('Runtime.console', event => {
|
2021-09-27 18:58:08 +02:00
|
|
|
const { type, args, location } = event;
|
2020-01-17 17:51:02 -08:00
|
|
|
const context = worker._existingExecutionContext!;
|
2020-05-15 15:21:49 -07:00
|
|
|
this._page._addConsoleMessage(type, args.map(arg => context.createHandle(arg)), location);
|
2020-01-17 17:51:02 -08:00
|
|
|
});
|
|
|
|
|
// Note: we receive worker exceptions directly from the page.
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-22 09:59:39 -07:00
|
|
|
_onWorkerDestroyed(event: Protocol.Page.workerDestroyedPayload) {
|
2020-01-17 17:51:02 -08:00
|
|
|
const workerId = event.workerId;
|
|
|
|
|
const worker = this._workers.get(workerId);
|
|
|
|
|
if (!worker)
|
|
|
|
|
return;
|
2020-03-06 16:49:48 -08:00
|
|
|
worker.session.dispose();
|
2020-01-17 17:51:02 -08:00
|
|
|
this._workers.delete(workerId);
|
|
|
|
|
this._page._removeWorker(workerId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async _onDispatchMessageFromWorker(event: Protocol.Page.dispatchMessageFromWorkerPayload) {
|
|
|
|
|
const worker = this._workers.get(event.workerId);
|
|
|
|
|
if (!worker)
|
|
|
|
|
return;
|
|
|
|
|
worker.session.dispatchMessage(JSON.parse(event.message));
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-13 13:19:25 -08:00
|
|
|
async _onCrashed(event: Protocol.Page.crashedPayload) {
|
2020-04-15 00:04:35 -07:00
|
|
|
this._session.markAsCrashed();
|
2020-02-13 13:19:25 -08:00
|
|
|
this._page._didCrash();
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-08 11:35:36 -07:00
|
|
|
_onVideoRecordingStarted(event: Protocol.Page.videoRecordingStartedPayload) {
|
2024-12-14 20:15:58 +00:00
|
|
|
this._browserContext._browser._videoStarted(this._browserContext, event.screencastId, event.file, this._page.waitForInitializedOrError());
|
2020-08-20 19:49:30 -07:00
|
|
|
}
|
|
|
|
|
|
2019-12-19 16:53:24 -08:00
|
|
|
didClose() {
|
2023-10-12 11:05:34 -07:00
|
|
|
this._markAsError(new TargetClosedError());
|
2020-03-09 12:32:42 -07:00
|
|
|
this._session.dispose();
|
2021-07-07 21:14:16 +02:00
|
|
|
eventsHelper.removeEventListeners(this._eventListeners);
|
2019-12-19 16:53:24 -08:00
|
|
|
this._networkManager.dispose();
|
|
|
|
|
this._page._didClose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async navigateFrame(frame: frames.Frame, url: string, referer: string | undefined): Promise<frames.GotoResult> {
|
|
|
|
|
const response = await this._session.send('Page.navigate', { url, referer, frameId: frame._id });
|
2020-02-10 18:35:47 -08:00
|
|
|
return { newDocumentId: response.navigationId || undefined };
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
2020-02-26 12:42:20 -08:00
|
|
|
async updateExtraHTTPHeaders(): Promise<void> {
|
2022-07-07 15:28:20 -08:00
|
|
|
await this._session.send('Network.setExtraHTTPHeaders', { headers: this._page.extraHTTPHeaders() || [] });
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
2022-07-07 15:28:20 -08:00
|
|
|
async updateEmulatedViewportSize(): Promise<void> {
|
|
|
|
|
const viewportSize = this._page.viewportSize();
|
|
|
|
|
await this._session.send('Page.setViewportSize', { viewportSize });
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
2020-07-21 09:36:54 -07:00
|
|
|
async bringToFront(): Promise<void> {
|
|
|
|
|
await this._session.send('Page.bringToFront', {});
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-06 19:49:33 -07:00
|
|
|
async updateEmulateMedia(): Promise<void> {
|
2022-07-07 15:28:20 -08:00
|
|
|
const emulatedMedia = this._page.emulatedMedia();
|
2022-10-31 09:09:52 -07:00
|
|
|
const colorScheme = emulatedMedia.colorScheme === 'no-override' ? undefined : emulatedMedia.colorScheme;
|
|
|
|
|
const reducedMotion = emulatedMedia.reducedMotion === 'no-override' ? undefined : emulatedMedia.reducedMotion;
|
|
|
|
|
const forcedColors = emulatedMedia.forcedColors === 'no-override' ? undefined : emulatedMedia.forcedColors;
|
2019-12-19 16:53:24 -08:00
|
|
|
await this._session.send('Page.setEmulatedMedia', {
|
2020-07-21 18:56:41 -07:00
|
|
|
// Empty string means reset.
|
2022-10-31 09:09:52 -07:00
|
|
|
type: emulatedMedia.media === 'no-override' ? '' : emulatedMedia.media,
|
2021-05-22 01:56:09 +02:00
|
|
|
colorScheme,
|
|
|
|
|
reducedMotion,
|
2021-09-03 21:48:06 +02:00
|
|
|
forcedColors,
|
2019-12-19 16:53:24 -08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-09 21:02:54 -07:00
|
|
|
async updateRequestInterception(): Promise<void> {
|
2022-07-01 12:49:43 -07:00
|
|
|
await this._networkManager.setRequestInterception(this._page.needsRequestInterception());
|
2019-12-30 14:05:28 -08:00
|
|
|
}
|
|
|
|
|
|
2022-07-11 12:10:08 -08:00
|
|
|
async updateFileChooserInterception() {
|
|
|
|
|
const enabled = this._page.fileChooserIntercepted();
|
|
|
|
|
await this._session.send('Page.setInterceptFileChooserDialog', { enabled }).catch(() => {}); // target can be closed.
|
2020-01-30 17:43:06 -08:00
|
|
|
}
|
|
|
|
|
|
2019-12-19 16:53:24 -08:00
|
|
|
async reload(): Promise<void> {
|
2023-03-10 09:25:54 -08:00
|
|
|
await this._session.send('Page.reload');
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async goBack(): Promise<boolean> {
|
2020-07-01 19:17:27 -07:00
|
|
|
const { success } = await this._session.send('Page.goBack', { frameId: this._page.mainFrame()._id });
|
|
|
|
|
return success;
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async goForward(): Promise<boolean> {
|
2020-07-01 19:17:27 -07:00
|
|
|
const { success } = await this._session.send('Page.goForward', { frameId: this._page.mainFrame()._id });
|
|
|
|
|
return success;
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
2024-09-26 05:08:33 -07:00
|
|
|
async requestGC(): Promise<void> {
|
2024-09-13 14:09:36 -07:00
|
|
|
await this._session.send('Heap.collectGarbage');
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-27 09:29:20 -07:00
|
|
|
async addInitScript(initScript: InitScript, worldName?: string): Promise<void> {
|
|
|
|
|
this._initScripts.push({ initScript, worldName });
|
|
|
|
|
await this._session.send('Page.setInitScripts', { scripts: this._initScripts.map(s => ({ script: s.initScript.source, worldName: s.worldName })) });
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
2024-08-07 06:20:12 -07:00
|
|
|
async removeNonInternalInitScripts() {
|
|
|
|
|
this._initScripts = this._initScripts.filter(s => s.initScript.internal);
|
|
|
|
|
await this._session.send('Page.setInitScripts', { scripts: this._initScripts.map(s => ({ script: s.initScript.source, worldName: s.worldName })) });
|
2022-04-04 11:39:43 -08:00
|
|
|
}
|
|
|
|
|
|
2019-12-19 16:53:24 -08:00
|
|
|
async closePage(runBeforeUnload: boolean): Promise<void> {
|
|
|
|
|
await this._session.send('Page.close', { runBeforeUnload });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async setBackgroundColor(color?: { r: number; g: number; b: number; a: number; }): Promise<void> {
|
|
|
|
|
if (color)
|
|
|
|
|
throw new Error('Not implemented');
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 12:28:40 -07:00
|
|
|
async takeScreenshot(progress: Progress, format: 'png' | 'jpeg', documentRect: types.Rect | undefined, viewportRect: types.Rect | undefined, quality: number | undefined, fitsViewport: boolean, scale: 'css' | 'device'): Promise<Buffer> {
|
2020-03-03 16:09:32 -08:00
|
|
|
if (!documentRect) {
|
2021-04-29 14:53:53 -07:00
|
|
|
const scrollOffset = await this._page.mainFrame().waitForFunctionValueInUtility(progress, () => ({ x: window.scrollX, y: window.scrollY }));
|
2020-03-03 16:09:32 -08:00
|
|
|
documentRect = {
|
|
|
|
|
x: viewportRect!.x + scrollOffset.x,
|
|
|
|
|
y: viewportRect!.y + scrollOffset.y,
|
|
|
|
|
width: viewportRect!.width,
|
|
|
|
|
height: viewportRect!.height,
|
|
|
|
|
};
|
|
|
|
|
}
|
2021-04-07 07:01:38 +08:00
|
|
|
progress.throwIfAborted();
|
2019-12-19 16:53:24 -08:00
|
|
|
const { data } = await this._session.send('Page.screenshot', {
|
|
|
|
|
mimeType: ('image/' + format) as ('image/png' | 'image/jpeg'),
|
2020-03-03 16:09:32 -08:00
|
|
|
clip: documentRect,
|
2023-05-11 18:09:19 +00:00
|
|
|
quality,
|
2022-04-01 12:28:40 -07:00
|
|
|
omitDeviceScaleFactor: scale === 'css',
|
2019-12-19 16:53:24 -08:00
|
|
|
});
|
2020-04-01 14:42:47 -07:00
|
|
|
return Buffer.from(data, 'base64');
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getContentFrame(handle: dom.ElementHandle): Promise<frames.Frame | null> {
|
2020-01-16 15:24:37 -08:00
|
|
|
const { contentFrameId } = await this._session.send('Page.describeNode', {
|
2019-12-19 16:53:24 -08:00
|
|
|
frameId: handle._context.frame._id,
|
2020-05-27 22:19:05 -07:00
|
|
|
objectId: handle._objectId,
|
2019-12-19 16:53:24 -08:00
|
|
|
});
|
2020-01-16 15:24:37 -08:00
|
|
|
if (!contentFrameId)
|
2019-12-19 16:53:24 -08:00
|
|
|
return null;
|
2020-01-16 15:24:37 -08:00
|
|
|
return this._page._frameManager.frame(contentFrameId);
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
2020-01-27 11:43:43 -08:00
|
|
|
async getOwnerFrame(handle: dom.ElementHandle): Promise<string | null> {
|
2020-01-16 15:24:37 -08:00
|
|
|
const { ownerFrameId } = await this._session.send('Page.describeNode', {
|
|
|
|
|
frameId: handle._context.frame._id,
|
2020-05-27 22:19:05 -07:00
|
|
|
objectId: handle._objectId
|
2020-01-16 15:24:37 -08:00
|
|
|
});
|
2020-01-27 11:43:43 -08:00
|
|
|
return ownerFrameId || null;
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isElementHandle(remoteObject: any): boolean {
|
|
|
|
|
return remoteObject.subtype === 'node';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getBoundingBox(handle: dom.ElementHandle): Promise<types.Rect | null> {
|
|
|
|
|
const quads = await this.getContentQuads(handle);
|
|
|
|
|
if (!quads || !quads.length)
|
|
|
|
|
return null;
|
|
|
|
|
let minX = Infinity;
|
|
|
|
|
let maxX = -Infinity;
|
|
|
|
|
let minY = Infinity;
|
|
|
|
|
let maxY = -Infinity;
|
|
|
|
|
for (const quad of quads) {
|
|
|
|
|
for (const point of quad) {
|
|
|
|
|
minX = Math.min(minX, point.x);
|
|
|
|
|
maxX = Math.max(maxX, point.x);
|
|
|
|
|
minY = Math.min(minY, point.y);
|
|
|
|
|
maxY = Math.max(maxY, point.y);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return { x: minX, y: minY, width: maxX - minX, height: maxY - minY };
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-24 15:12:17 -07:00
|
|
|
async scrollRectIntoViewIfNeeded(handle: dom.ElementHandle, rect?: types.Rect): Promise<'error:notvisible' | 'error:notconnected' | 'done'> {
|
2020-05-22 11:15:57 -07:00
|
|
|
return await this._session.send('Page.scrollIntoViewIfNeeded', {
|
2020-02-11 10:30:09 -08:00
|
|
|
frameId: handle._context.frame._id,
|
2020-05-27 22:19:05 -07:00
|
|
|
objectId: handle._objectId,
|
2020-02-11 10:30:09 -08:00
|
|
|
rect,
|
2020-06-12 14:59:26 -07:00
|
|
|
}).then(() => 'done' as const).catch(e => {
|
2020-04-18 18:29:31 -07:00
|
|
|
if (e instanceof Error && e.message.includes('Node is detached from document'))
|
2020-06-24 15:12:17 -07:00
|
|
|
return 'error:notconnected';
|
2020-06-17 10:48:07 -07:00
|
|
|
if (e instanceof Error && e.message.includes('Node does not have a layout object'))
|
2020-06-24 15:12:17 -07:00
|
|
|
return 'error:notvisible';
|
2020-04-18 18:29:31 -07:00
|
|
|
throw e;
|
2020-02-11 10:30:09 -08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-11 13:21:01 -07:00
|
|
|
async setScreencastOptions(options: { width: number, height: number, quality: number } | null): Promise<void> {
|
|
|
|
|
if (options) {
|
|
|
|
|
const { screencastId } = await this._session.send('Page.startScreencast', options);
|
2021-05-08 11:35:36 -07:00
|
|
|
this._screencastId = screencastId;
|
|
|
|
|
} else {
|
|
|
|
|
await this._session.send('Page.stopScreencast');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private _onScreencastFrame(event: Protocol.Page.screencastFramePayload) {
|
|
|
|
|
if (!this._screencastId)
|
|
|
|
|
return;
|
2021-10-29 17:20:17 -08:00
|
|
|
const screencastId = this._screencastId;
|
|
|
|
|
this._page.throttleScreencastFrameAck(() => {
|
|
|
|
|
this._session.send('Page.screencastFrameAck', { screencastId }).catch(e => debugLogger.log('error', e));
|
|
|
|
|
});
|
2021-05-12 08:35:19 -07:00
|
|
|
|
2021-05-08 11:35:36 -07:00
|
|
|
const buffer = Buffer.from(event.data, 'base64');
|
|
|
|
|
this._page.emit(Page.Events.ScreencastFrame, {
|
|
|
|
|
buffer,
|
|
|
|
|
width: event.deviceWidth,
|
|
|
|
|
height: event.deviceHeight,
|
|
|
|
|
});
|
2021-04-08 05:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
2020-05-04 16:30:19 -07:00
|
|
|
rafCountForStablePosition(): number {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-19 16:53:24 -08:00
|
|
|
async getContentQuads(handle: dom.ElementHandle): Promise<types.Quad[] | null> {
|
2020-06-05 07:50:26 -07:00
|
|
|
const result = await this._session.sendMayFail('Page.getContentQuads', {
|
2019-12-19 16:53:24 -08:00
|
|
|
frameId: handle._context.frame._id,
|
2020-05-27 22:19:05 -07:00
|
|
|
objectId: handle._objectId,
|
2020-06-05 07:50:26 -07:00
|
|
|
});
|
2019-12-19 16:53:24 -08:00
|
|
|
if (!result)
|
|
|
|
|
return null;
|
2022-08-18 20:12:33 +02:00
|
|
|
return result.quads.map(quad => [quad.p1, quad.p2, quad.p3, quad.p4]);
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
2022-03-24 07:46:37 -07:00
|
|
|
async setInputFiles(handle: dom.ElementHandle<HTMLInputElement>, files: types.FilePayload[]): Promise<void> {
|
|
|
|
|
await handle.evaluateInUtility(([injected, node, files]) =>
|
|
|
|
|
injected.setInputFiles(node, files), files);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-29 14:44:45 -08:00
|
|
|
async setInputFilePaths(handle: dom.ElementHandle<HTMLInputElement>, files: string[]): Promise<void> {
|
2024-02-27 11:50:24 -08:00
|
|
|
await this._session.send('Page.setFileInputFiles', {
|
|
|
|
|
frameId: handle._context.frame._id,
|
|
|
|
|
objectId: handle._objectId,
|
|
|
|
|
files
|
|
|
|
|
});
|
2022-03-18 09:00:52 -07:00
|
|
|
}
|
|
|
|
|
|
2019-12-19 16:53:24 -08:00
|
|
|
async adoptElementHandle<T extends Node>(handle: dom.ElementHandle<T>, to: dom.FrameExecutionContext): Promise<dom.ElementHandle<T>> {
|
2020-01-16 12:57:28 -08:00
|
|
|
const result = await this._session.send('Page.adoptNode', {
|
|
|
|
|
frameId: handle._context.frame._id,
|
2020-05-27 22:19:05 -07:00
|
|
|
objectId: handle._objectId,
|
2021-11-03 10:44:50 -07:00
|
|
|
executionContextId: ((to as any)[contextDelegateSymbol] as FFExecutionContext)._executionContextId
|
2020-01-16 12:57:28 -08:00
|
|
|
});
|
2020-02-13 13:19:25 -08:00
|
|
|
if (!result.remoteObject)
|
2021-06-02 20:17:24 -07:00
|
|
|
throw new Error(dom.kUnableToAdoptErrorMessage);
|
2020-05-15 15:21:49 -07:00
|
|
|
return to.createHandle(result.remoteObject) as dom.ElementHandle<T>;
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
2020-01-03 11:15:43 -08:00
|
|
|
|
2020-01-14 16:54:50 -08:00
|
|
|
async getAccessibilityTree(needle?: dom.ElementHandle) {
|
|
|
|
|
return getAccessibilityTree(this._session, needle);
|
2020-01-03 11:15:43 -08:00
|
|
|
}
|
2020-01-07 13:57:37 -08:00
|
|
|
|
2020-03-07 08:19:31 -08:00
|
|
|
async inputActionEpilogue(): Promise<void> {
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-12 13:21:49 -07:00
|
|
|
async resetForReuse(): Promise<void> {
|
|
|
|
|
// Firefox sometimes keeps the last mouse position in the page,
|
|
|
|
|
// which affects things like hovered state.
|
|
|
|
|
// See https://github.com/microsoft/playwright/issues/22432.
|
|
|
|
|
// Move mouse to (-1, -1) to avoid anything being hovered.
|
|
|
|
|
await this.rawMouse.move(-1, -1, 'none', new Set(), new Set(), false);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-05 17:20:23 -08:00
|
|
|
async getFrameElement(frame: frames.Frame): Promise<dom.ElementHandle> {
|
|
|
|
|
const parent = frame.parentFrame();
|
|
|
|
|
if (!parent)
|
|
|
|
|
throw new Error('Frame has been detached.');
|
2022-09-06 08:52:25 -07:00
|
|
|
const context = await parent._mainContext();
|
|
|
|
|
const result = await this._session.send('Page.adoptNode', {
|
|
|
|
|
frameId: frame._id,
|
|
|
|
|
executionContextId: ((context as any)[contextDelegateSymbol] as FFExecutionContext)._executionContextId
|
|
|
|
|
});
|
|
|
|
|
if (!result.remoteObject)
|
2020-02-05 17:20:23 -08:00
|
|
|
throw new Error('Frame has been detached.');
|
2022-09-06 08:52:25 -07:00
|
|
|
return context.createHandle(result.remoteObject) as dom.ElementHandle;
|
2020-02-05 17:20:23 -08:00
|
|
|
}
|
2023-12-11 18:20:24 -08:00
|
|
|
|
|
|
|
|
shouldToggleStyleSheetToSyncAnimations(): boolean {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
2020-10-30 10:34:24 -07:00
|
|
|
|
|
|
|
|
function webSocketId(frameId: string, wsid: string): string {
|
|
|
|
|
return `${frameId}---${wsid}`;
|
|
|
|
|
}
|
2021-11-03 10:44:50 -07:00
|
|
|
|
|
|
|
|
const contextDelegateSymbol = Symbol('delegate');
|