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';
|
|
|
|
import * as frames from '../frames';
|
|
|
|
import { helper, RegisteredListener } from '../helper';
|
2020-08-23 17:05:58 -07:00
|
|
|
import { assert } from '../../utils/utils';
|
2020-08-24 06:51:51 -07:00
|
|
|
import { Page, PageBinding, PageDelegate, Worker } from '../page';
|
|
|
|
import { kScreenshotDuringNavigationError } from '../screenshotter';
|
|
|
|
import * as types from '../types';
|
2020-03-05 17:22:57 -08:00
|
|
|
import { getAccessibilityTree } from './ffAccessibility';
|
|
|
|
import { FFBrowserContext } from './ffBrowser';
|
2020-03-09 12:32:42 -07:00
|
|
|
import { FFSession, FFSessionEvents } 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';
|
2019-12-19 16:53:24 -08:00
|
|
|
import { Protocol } from './protocol';
|
2020-08-23 17:05:58 -07:00
|
|
|
import { rewriteErrorMessage } from '../../utils/stackTrace';
|
2019-12-19 16:53:24 -08:00
|
|
|
|
2020-01-16 12:57:28 -08:00
|
|
|
const UTILITY_WORLD_NAME = '__playwright_utility_world__';
|
|
|
|
|
2019-12-23 11:39:57 -08:00
|
|
|
export class FFPage implements PageDelegate {
|
2020-04-17 08:51:54 -07:00
|
|
|
readonly cspErrorsAsynchronousForInlineScipts = 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;
|
|
|
|
private _pagePromise: Promise<Page | Error>;
|
2020-04-29 18:36:24 -07:00
|
|
|
_pageCallback: (pageOrError: Page | Error) => void = () => {};
|
2020-04-16 13:09:24 -07:00
|
|
|
_initializedPage: Page | null = null;
|
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 }>();
|
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);
|
|
|
|
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 = [
|
|
|
|
helper.addEventListener(this._session, 'Page.eventFired', this._onEventFired.bind(this)),
|
|
|
|
helper.addEventListener(this._session, 'Page.frameAttached', this._onFrameAttached.bind(this)),
|
|
|
|
helper.addEventListener(this._session, 'Page.frameDetached', this._onFrameDetached.bind(this)),
|
|
|
|
helper.addEventListener(this._session, 'Page.navigationAborted', this._onNavigationAborted.bind(this)),
|
|
|
|
helper.addEventListener(this._session, 'Page.navigationCommitted', this._onNavigationCommitted.bind(this)),
|
2020-04-09 19:03:06 -07:00
|
|
|
helper.addEventListener(this._session, 'Page.navigationStarted', this._onNavigationStarted.bind(this)),
|
2019-12-19 16:53:24 -08:00
|
|
|
helper.addEventListener(this._session, 'Page.sameDocumentNavigation', this._onSameDocumentNavigation.bind(this)),
|
|
|
|
helper.addEventListener(this._session, 'Runtime.executionContextCreated', this._onExecutionContextCreated.bind(this)),
|
|
|
|
helper.addEventListener(this._session, 'Runtime.executionContextDestroyed', this._onExecutionContextDestroyed.bind(this)),
|
2020-03-05 14:47:04 -08:00
|
|
|
helper.addEventListener(this._session, 'Page.linkClicked', event => this._onLinkClicked(event.phase)),
|
2019-12-19 16:53:24 -08:00
|
|
|
helper.addEventListener(this._session, 'Page.uncaughtError', this._onUncaughtError.bind(this)),
|
|
|
|
helper.addEventListener(this._session, 'Runtime.console', this._onConsole.bind(this)),
|
|
|
|
helper.addEventListener(this._session, 'Page.dialogOpened', this._onDialogOpened.bind(this)),
|
|
|
|
helper.addEventListener(this._session, 'Page.bindingCalled', this._onBindingCalled.bind(this)),
|
|
|
|
helper.addEventListener(this._session, 'Page.fileChooserOpened', this._onFileChooserOpened.bind(this)),
|
2020-01-17 17:51:02 -08:00
|
|
|
helper.addEventListener(this._session, 'Page.workerCreated', this._onWorkerCreated.bind(this)),
|
|
|
|
helper.addEventListener(this._session, 'Page.workerDestroyed', this._onWorkerDestroyed.bind(this)),
|
|
|
|
helper.addEventListener(this._session, 'Page.dispatchMessageFromWorker', this._onDispatchMessageFromWorker.bind(this)),
|
2020-02-13 13:19:25 -08:00
|
|
|
helper.addEventListener(this._session, 'Page.crashed', this._onCrashed.bind(this)),
|
2020-08-20 19:49:30 -07:00
|
|
|
helper.addEventListener(this._session, 'Page.screencastStarted', this._onScreencastStarted.bind(this)),
|
2020-10-30 10:34:24 -07:00
|
|
|
|
|
|
|
helper.addEventListener(this._session, 'Page.webSocketCreated', this._onWebSocketCreated.bind(this)),
|
|
|
|
helper.addEventListener(this._session, 'Page.webSocketClosed', this._onWebSocketClosed.bind(this)),
|
|
|
|
helper.addEventListener(this._session, 'Page.webSocketFrameReceived', this._onWebSocketFrameReceived.bind(this)),
|
|
|
|
helper.addEventListener(this._session, 'Page.webSocketFrameSent', this._onWebSocketFrameSent.bind(this)),
|
2019-12-19 16:53:24 -08:00
|
|
|
];
|
2020-03-09 12:32:42 -07:00
|
|
|
this._pagePromise = new Promise(f => this._pageCallback = f);
|
|
|
|
session.once(FFSessionEvents.Disconnected, () => this._page._didDisconnect());
|
2020-03-26 16:13:11 -07:00
|
|
|
this._session.once('Page.ready', () => {
|
2020-03-09 12:32:42 -07:00
|
|
|
this._pageCallback(this._page);
|
2020-04-16 13:09:24 -07:00
|
|
|
this._initializedPage = this._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.
|
|
|
|
this._session.send('Page.addScriptToEvaluateOnNewDocument', { script: '', worldName: UTILITY_WORLD_NAME }).catch(this._pageCallback);
|
2020-03-09 12:32:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async pageOrError(): Promise<Page | Error> {
|
|
|
|
return this._pagePromise;
|
2020-01-16 12:57:28 -08:00
|
|
|
}
|
|
|
|
|
2020-11-12 12:41:23 -08:00
|
|
|
openerDelegate(): PageDelegate | null {
|
|
|
|
return this._opener;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
const {executionContextId, auxData} = payload;
|
2019-12-19 16:53:24 -08:00
|
|
|
const frame = this._page._frameManager.frame(auxData ? auxData.frameId : null);
|
|
|
|
if (!frame)
|
|
|
|
return;
|
|
|
|
const delegate = new FFExecutionContext(this._session, executionContextId);
|
2020-12-02 13:43:16 -08: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);
|
|
|
|
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) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
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) {
|
2020-04-20 11:37:02 -07:00
|
|
|
const message = params.message.startsWith('Error: ') ? params.message.substring(7) : params.message;
|
|
|
|
const error = new Error(message);
|
2019-12-19 16:53:24 -08:00
|
|
|
error.stack = params.stack;
|
2020-08-21 16:26:33 -07:00
|
|
|
this._page.emit(Page.Events.PageError, error);
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
2020-01-13 22:08:35 -08:00
|
|
|
_onConsole(payload: Protocol.Runtime.consolePayload) {
|
|
|
|
const {type, args, executionContextId, location} = payload;
|
|
|
|
const context = this._contextIdToContext.get(executionContextId)!;
|
2020-05-15 15:21:49 -07:00
|
|
|
this._page._addConsoleMessage(type, args.map(arg => context.createHandle(arg)), location);
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
_onDialogOpened(params: Protocol.Page.dialogOpenedPayload) {
|
2020-08-21 16:26:33 -07:00
|
|
|
this._page.emit(Page.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) {
|
2020-01-13 22:08:35 -08:00
|
|
|
const context = this._contextIdToContext.get(event.executionContextId)!;
|
2020-07-14 13:34:49 -07:00
|
|
|
const pageOrError = await this.pageOrError();
|
|
|
|
if (!(pageOrError instanceof Error))
|
|
|
|
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) {
|
|
|
|
const {executionContextId, element} = payload;
|
|
|
|
const context = this._contextIdToContext.get(executionContextId)!;
|
2020-05-15 15:21:49 -07:00
|
|
|
const handle = context.createHandle(element).asElement()!;
|
2019-12-19 16:53:24 -08:00
|
|
|
this._page._onFileChooserOpened(handle);
|
|
|
|
}
|
|
|
|
|
2020-01-17 17:51:02 -08:00
|
|
|
async _onWorkerCreated(event: Protocol.Page.workerCreatedPayload) {
|
|
|
|
const workerId = event.workerId;
|
2020-06-09 16:11:17 -07:00
|
|
|
const worker = new Worker(event.url);
|
2020-01-17 17:51:02 -08:00
|
|
|
const workerSession = new FFSession(this._session._connection, 'worker', workerId, (message: any) => {
|
|
|
|
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 => {
|
|
|
|
const {type, args, location} = event;
|
|
|
|
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.
|
|
|
|
}
|
|
|
|
|
|
|
|
async _onWorkerDestroyed(event: Protocol.Page.workerDestroyedPayload) {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2020-08-20 19:49:30 -07:00
|
|
|
_onScreencastStarted(event: Protocol.Page.screencastStartedPayload) {
|
2020-09-18 17:36:43 -07:00
|
|
|
this._browserContext._browser._videoStarted(this._browserContext, event.screencastId, event.file, this.pageOrError());
|
2020-08-20 19:49:30 -07:00
|
|
|
}
|
|
|
|
|
2020-03-03 16:46:06 -08:00
|
|
|
async exposeBinding(binding: PageBinding) {
|
2020-12-02 13:43:16 -08:00
|
|
|
if (binding.world !== 'main')
|
|
|
|
throw new Error('Only main context bindings are supported in Firefox.');
|
2020-03-22 22:45:15 -07:00
|
|
|
await this._session.send('Page.addBinding', { name: binding.name, script: binding.source });
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
didClose() {
|
2020-03-09 12:32:42 -07:00
|
|
|
this._session.dispose();
|
2019-12-19 16:53:24 -08:00
|
|
|
helper.removeEventListeners(this._eventListeners);
|
|
|
|
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> {
|
2020-08-18 15:38:29 -07:00
|
|
|
await this._session.send('Network.setExtraHTTPHeaders', { headers: this._page._state.extraHTTPHeaders || [] });
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
2020-02-06 19:02:55 -08:00
|
|
|
async setViewportSize(viewportSize: types.Size): Promise<void> {
|
|
|
|
assert(this._page._state.viewportSize === viewportSize);
|
2020-02-11 18:52:01 -08:00
|
|
|
await this._session.send('Page.setViewportSize', {
|
|
|
|
viewportSize: {
|
|
|
|
width: viewportSize.width,
|
|
|
|
height: viewportSize.height,
|
2020-02-06 19:02:55 -08:00
|
|
|
},
|
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> {
|
|
|
|
const colorScheme = this._page._state.colorScheme || this._browserContext._options.colorScheme || 'light';
|
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.
|
|
|
|
type: this._page._state.mediaType === null ? '' : this._page._state.mediaType,
|
2020-04-06 19:49:33 -07:00
|
|
|
colorScheme
|
2019-12-19 16:53:24 -08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-09 21:02:54 -07:00
|
|
|
async updateRequestInterception(): Promise<void> {
|
|
|
|
await this._networkManager.setRequestInterception(this._page._needsRequestInterception());
|
2019-12-30 14:05:28 -08:00
|
|
|
}
|
|
|
|
|
2020-01-30 17:43:06 -08:00
|
|
|
async setFileChooserIntercepted(enabled: boolean) {
|
|
|
|
await this._session.send('Page.setInterceptFileChooserDialog', { enabled }).catch(e => {}); // target can be closed.
|
|
|
|
}
|
|
|
|
|
2020-02-07 13:36:49 -08:00
|
|
|
async opener(): Promise<Page | null> {
|
2020-03-09 12:32:42 -07:00
|
|
|
if (!this._opener)
|
|
|
|
return null;
|
|
|
|
const result = await this._opener.pageOrError();
|
|
|
|
if (result instanceof Page && !result.isClosed())
|
|
|
|
return result;
|
|
|
|
return null;
|
2020-01-31 18:38:45 -08:00
|
|
|
}
|
|
|
|
|
2019-12-19 16:53:24 -08:00
|
|
|
async reload(): Promise<void> {
|
|
|
|
await this._session.send('Page.reload', { frameId: this._page.mainFrame()._id });
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
async evaluateOnNewDocument(source: string): Promise<void> {
|
|
|
|
await this._session.send('Page.addScriptToEvaluateOnNewDocument', { script: source });
|
|
|
|
}
|
|
|
|
|
|
|
|
async closePage(runBeforeUnload: boolean): Promise<void> {
|
|
|
|
await this._session.send('Page.close', { runBeforeUnload });
|
|
|
|
}
|
|
|
|
|
|
|
|
canScreenshotOutsideViewport(): boolean {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
async setBackgroundColor(color?: { r: number; g: number; b: number; a: number; }): Promise<void> {
|
|
|
|
if (color)
|
|
|
|
throw new Error('Not implemented');
|
|
|
|
}
|
|
|
|
|
2020-04-01 14:42:47 -07:00
|
|
|
async takeScreenshot(format: 'png' | 'jpeg', documentRect: types.Rect | undefined, viewportRect: types.Rect | undefined, quality: number | undefined): Promise<Buffer> {
|
2020-03-03 16:09:32 -08:00
|
|
|
if (!documentRect) {
|
|
|
|
const context = await this._page.mainFrame()._utilityContext();
|
2020-03-20 15:08:17 -07:00
|
|
|
const scrollOffset = await context.evaluateInternal(() => ({ 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,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
// TODO: remove fullPage option from Page.screenshot.
|
|
|
|
// TODO: remove Page.getBoundingBox method.
|
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,
|
2020-01-10 17:25:28 -08:00
|
|
|
}).catch(e => {
|
|
|
|
if (e instanceof Error && e.message.includes('document.documentElement is null'))
|
2020-05-28 16:33:31 -07:00
|
|
|
rewriteErrorMessage(e, kScreenshotDuringNavigationError);
|
2020-01-10 17:25:28 -08:00
|
|
|
throw e;
|
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 resetViewport(): Promise<void> {
|
2020-03-03 16:09:32 -08:00
|
|
|
assert(false, 'Should not be called');
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
return result.quads.map(quad => [ quad.p1, quad.p2, quad.p3, quad.p4 ]);
|
|
|
|
}
|
|
|
|
|
2020-01-13 22:08:35 -08:00
|
|
|
async setInputFiles(handle: dom.ElementHandle<HTMLInputElement>, files: types.FilePayload[]): Promise<void> {
|
2020-06-03 13:22:05 -07:00
|
|
|
await handle._evaluateInUtility(([injected, node, files]) =>
|
2020-08-18 17:32:11 -07:00
|
|
|
injected.setInputFiles(node, files), files);
|
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,
|
2020-01-16 12:57:28 -08:00
|
|
|
executionContextId: (to._delegate as FFExecutionContext)._executionContextId
|
|
|
|
});
|
2020-02-13 13:19:25 -08:00
|
|
|
if (!result.remoteObject)
|
|
|
|
throw new Error('Unable to adopt element handle from a different document');
|
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> {
|
|
|
|
}
|
|
|
|
|
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.');
|
2020-09-14 10:38:14 -07:00
|
|
|
const handles = await this._page.selectors._queryAll(parent, 'iframe', undefined);
|
2020-02-05 17:20:23 -08:00
|
|
|
const items = await Promise.all(handles.map(async handle => {
|
|
|
|
const frame = await handle.contentFrame().catch(e => null);
|
|
|
|
return { handle, frame };
|
|
|
|
}));
|
|
|
|
const result = items.find(item => item.frame === frame);
|
2020-03-04 17:57:35 -08:00
|
|
|
items.map(item => item === result ? Promise.resolve() : item.handle.dispose());
|
2020-02-05 17:20:23 -08:00
|
|
|
if (!result)
|
|
|
|
throw new Error('Frame has been detached.');
|
|
|
|
return result.handle;
|
|
|
|
}
|
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}`;
|
|
|
|
}
|