2019-12-19 16:53:24 -08:00
|
|
|
/**
|
|
|
|
* Copyright 2017 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import * as frames from '../frames';
|
2020-01-22 14:17:44 -08:00
|
|
|
import { debugError, helper, RegisteredListener, assert } from '../helper';
|
2019-12-19 16:53:24 -08:00
|
|
|
import * as dom from '../dom';
|
|
|
|
import * as network from '../network';
|
2020-01-09 15:14:35 -08:00
|
|
|
import { WKSession } from './wkConnection';
|
2019-12-19 16:53:24 -08:00
|
|
|
import { Events } from '../events';
|
2020-01-29 19:13:44 -08:00
|
|
|
import { WKExecutionContext } from './wkExecutionContext';
|
2020-01-21 14:58:12 -08:00
|
|
|
import { WKInterceptableRequest } from './wkInterceptableRequest';
|
2020-01-07 12:59:01 -08:00
|
|
|
import { WKWorkers } from './wkWorkers';
|
2020-03-03 16:46:06 -08:00
|
|
|
import { Page, PageDelegate, PageBinding } from '../page';
|
2019-12-19 16:53:24 -08:00
|
|
|
import { Protocol } from './protocol';
|
|
|
|
import * as dialog from '../dialog';
|
|
|
|
import { RawMouseImpl, RawKeyboardImpl } from './wkInput';
|
|
|
|
import * as types from '../types';
|
2020-01-03 11:15:43 -08:00
|
|
|
import * as accessibility from '../accessibility';
|
2020-01-07 11:55:24 -08:00
|
|
|
import * as platform from '../platform';
|
2020-01-03 11:15:43 -08:00
|
|
|
import { getAccessibilityTree } from './wkAccessibility';
|
2020-01-22 14:17:44 -08:00
|
|
|
import { WKProvisionalPage } from './wkProvisionalPage';
|
2020-02-27 16:18:33 -08:00
|
|
|
import { WKBrowserContext } from './wkBrowser';
|
2019-12-19 16:53:24 -08:00
|
|
|
|
|
|
|
const UTILITY_WORLD_NAME = '__playwright_utility_world__';
|
|
|
|
const BINDING_CALL_MESSAGE = '__playwright_binding_call__';
|
2020-03-06 11:41:46 -08:00
|
|
|
const isPovisionalSymbol = Symbol('isPovisional');
|
2019-12-19 16:53:24 -08:00
|
|
|
|
2019-12-23 11:39:57 -08:00
|
|
|
export class WKPage implements PageDelegate {
|
2019-12-19 16:53:24 -08:00
|
|
|
readonly rawMouse: RawMouseImpl;
|
|
|
|
readonly rawKeyboard: RawKeyboardImpl;
|
2020-01-09 11:02:55 -08:00
|
|
|
_session: WKSession;
|
2020-01-22 14:17:44 -08:00
|
|
|
private _provisionalPage: WKProvisionalPage | null = null;
|
2019-12-19 16:53:24 -08:00
|
|
|
readonly _page: Page;
|
2020-03-06 11:41:46 -08:00
|
|
|
private readonly _pagePromise: Promise<Page | Error>;
|
|
|
|
private _pagePromiseCallback: (page: Page | Error) => void = () => {};
|
2020-01-09 15:14:35 -08:00
|
|
|
private readonly _pageProxySession: WKSession;
|
2020-03-06 11:41:46 -08:00
|
|
|
private readonly _opener: WKPage | null;
|
2020-01-21 14:58:12 -08:00
|
|
|
private readonly _requestIdToRequest = new Map<string, WKInterceptableRequest>();
|
2020-01-07 17:16:27 -08:00
|
|
|
private readonly _workers: WKWorkers;
|
|
|
|
private readonly _contextIdToContext: Map<number, dom.FrameExecutionContext>;
|
2020-01-27 16:51:52 -08:00
|
|
|
private _mainFrameContextId?: number;
|
2019-12-19 16:53:24 -08:00
|
|
|
private _sessionListeners: RegisteredListener[] = [];
|
2020-03-06 11:41:46 -08:00
|
|
|
private _eventListeners: RegisteredListener[];
|
2020-03-03 16:46:06 -08:00
|
|
|
private readonly _evaluateOnNewDocumentSources: string[] = [];
|
2020-03-06 11:41:46 -08:00
|
|
|
readonly _browserContext: WKBrowserContext;
|
|
|
|
private _initialized = false;
|
2019-12-19 16:53:24 -08:00
|
|
|
|
2020-03-06 11:41:46 -08:00
|
|
|
// TODO: we should be able to just use |this._session| and |this._provisionalPage|.
|
|
|
|
private readonly _sessions = new Map<string, WKSession>();
|
|
|
|
|
|
|
|
constructor(browserContext: WKBrowserContext, pageProxySession: WKSession, opener: WKPage | null) {
|
2020-01-07 17:16:27 -08:00
|
|
|
this._pageProxySession = pageProxySession;
|
2020-02-27 08:49:09 -08:00
|
|
|
this._opener = opener;
|
2020-01-07 17:16:27 -08:00
|
|
|
this.rawKeyboard = new RawKeyboardImpl(pageProxySession);
|
|
|
|
this.rawMouse = new RawMouseImpl(pageProxySession);
|
2019-12-19 16:53:24 -08:00
|
|
|
this._contextIdToContext = new Map();
|
|
|
|
this._page = new Page(this, browserContext);
|
2020-01-07 12:59:01 -08:00
|
|
|
this._workers = new WKWorkers(this._page);
|
2020-01-13 13:33:25 -08:00
|
|
|
this._session = undefined as any as WKSession;
|
2020-02-27 16:18:33 -08:00
|
|
|
this._browserContext = browserContext;
|
2020-01-23 14:58:30 -08:00
|
|
|
this._page.on(Events.Page.FrameDetached, frame => this._removeContextsForFrame(frame, false));
|
2020-03-06 11:41:46 -08:00
|
|
|
this._eventListeners = [
|
|
|
|
helper.addEventListener(this._pageProxySession, 'Target.targetCreated', this._onTargetCreated.bind(this)),
|
|
|
|
helper.addEventListener(this._pageProxySession, 'Target.targetDestroyed', this._onTargetDestroyed.bind(this)),
|
|
|
|
helper.addEventListener(this._pageProxySession, 'Target.dispatchMessageFromTarget', this._onDispatchMessageFromTarget.bind(this)),
|
|
|
|
helper.addEventListener(this._pageProxySession, 'Target.didCommitProvisionalTarget', this._onDidCommitProvisionalTarget.bind(this)),
|
|
|
|
];
|
|
|
|
this._pagePromise = new Promise(f => this._pagePromiseCallback = f);
|
|
|
|
}
|
|
|
|
|
|
|
|
_initializedPage(): Page | undefined {
|
|
|
|
return this._initialized ? this._page : undefined;
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
2020-01-17 15:33:55 -08:00
|
|
|
private async _initializePageProxySession() {
|
2020-02-07 13:36:49 -08:00
|
|
|
const promises: Promise<any>[] = [
|
2020-01-07 17:16:27 -08:00
|
|
|
this._pageProxySession.send('Dialog.enable'),
|
2020-02-11 19:10:57 -08:00
|
|
|
this._pageProxySession.send('Emulation.setActiveAndFocused', { active: true }),
|
2020-01-07 17:16:27 -08:00
|
|
|
];
|
2020-03-05 17:22:57 -08:00
|
|
|
const contextOptions = this._browserContext._options;
|
2020-01-07 17:16:27 -08:00
|
|
|
if (contextOptions.javaScriptEnabled === false)
|
|
|
|
promises.push(this._pageProxySession.send('Emulation.setJavaScriptEnabled', { enabled: false }));
|
2020-02-06 19:02:55 -08:00
|
|
|
if (this._page._state.viewportSize || contextOptions.viewport)
|
|
|
|
promises.push(this._updateViewport(true /* updateTouch */));
|
2020-03-06 13:50:42 -08:00
|
|
|
promises.push(this.updateHttpCredentials());
|
2020-01-07 17:16:27 -08:00
|
|
|
await Promise.all(promises);
|
|
|
|
}
|
|
|
|
|
2020-01-22 14:17:44 -08:00
|
|
|
private _setSession(session: WKSession) {
|
2019-12-19 16:53:24 -08:00
|
|
|
helper.removeEventListeners(this._sessionListeners);
|
|
|
|
this._session = session;
|
|
|
|
this.rawKeyboard.setSession(session);
|
|
|
|
this._addSessionListeners();
|
2020-01-07 12:59:01 -08:00
|
|
|
this._workers.setSession(session);
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// This method is called for provisional targets as well. The session passed as the parameter
|
|
|
|
// may be different from the current session and may be destroyed without becoming current.
|
2020-01-17 15:33:55 -08:00
|
|
|
async _initializeSession(session: WKSession, resourceTreeHandler: (r: Protocol.Page.getResourceTreeReturnValue) => void) {
|
2020-01-30 17:30:47 -08:00
|
|
|
await this._initializeSessionMayThrow(session, resourceTreeHandler).catch(e => {
|
|
|
|
if (session.isDisposed())
|
|
|
|
return;
|
|
|
|
// Swallow initialization errors due to newer target swap in,
|
|
|
|
// since we will reinitialize again.
|
|
|
|
if (this._session === session)
|
|
|
|
throw e;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private async _initializeSessionMayThrow(session: WKSession, resourceTreeHandler: (r: Protocol.Page.getResourceTreeReturnValue) => void) {
|
|
|
|
const [, frameTree] = await Promise.all([
|
2019-12-19 16:53:24 -08:00
|
|
|
// Page agent must be enabled before Runtime.
|
|
|
|
session.send('Page.enable'),
|
2020-01-30 17:30:47 -08:00
|
|
|
session.send('Page.getResourceTree'),
|
2020-02-05 16:53:36 -08:00
|
|
|
] as const);
|
2020-01-30 17:30:47 -08:00
|
|
|
resourceTreeHandler(frameTree);
|
2020-02-07 13:36:49 -08:00
|
|
|
const promises: Promise<any>[] = [
|
2019-12-19 16:53:24 -08:00
|
|
|
// Resource tree should be received before first execution context.
|
2020-01-17 14:02:57 -08:00
|
|
|
session.send('Runtime.enable'),
|
2020-01-29 19:13:44 -08:00
|
|
|
session.send('Page.createUserWorld', { name: UTILITY_WORLD_NAME }).catch(_ => {}), // Worlds are per-process
|
2019-12-19 16:53:24 -08:00
|
|
|
session.send('Console.enable'),
|
2020-01-21 14:58:12 -08:00
|
|
|
session.send('Network.enable'),
|
2020-01-17 15:33:55 -08:00
|
|
|
this._workers.initializeSession(session)
|
2019-12-19 16:53:24 -08:00
|
|
|
];
|
2020-01-21 14:58:12 -08:00
|
|
|
|
2020-01-31 16:23:15 -08:00
|
|
|
if (this._page._state.interceptNetwork)
|
2020-01-21 14:58:12 -08:00
|
|
|
promises.push(session.send('Network.setInterceptionEnabled', { enabled: true, interceptRequests: true }));
|
2020-01-31 16:23:15 -08:00
|
|
|
|
2020-03-05 17:22:57 -08:00
|
|
|
const contextOptions = this._browserContext._options;
|
2019-12-19 16:53:24 -08:00
|
|
|
if (contextOptions.userAgent)
|
|
|
|
promises.push(session.send('Page.overrideUserAgent', { value: contextOptions.userAgent }));
|
|
|
|
if (this._page._state.mediaType || this._page._state.colorScheme)
|
2020-01-17 14:02:57 -08:00
|
|
|
promises.push(WKPage._setEmulateMedia(session, this._page._state.mediaType, this._page._state.colorScheme));
|
2020-02-27 16:18:33 -08:00
|
|
|
promises.push(session.send('Page.setBootstrapScript', { source: this._calculateBootstrapScript() }));
|
2020-03-03 16:46:06 -08:00
|
|
|
for (const binding of this._browserContext._pageBindings.values())
|
|
|
|
promises.push(this._evaluateBindingScript(binding));
|
2019-12-19 16:53:24 -08:00
|
|
|
if (contextOptions.bypassCSP)
|
|
|
|
promises.push(session.send('Page.setBypassCSP', { enabled: true }));
|
2020-02-26 12:42:20 -08:00
|
|
|
promises.push(session.send('Network.setExtraHTTPHeaders', { headers: this._calculateExtraHTTPHeaders() }));
|
2020-03-04 17:58:12 -08:00
|
|
|
if (contextOptions.offline)
|
|
|
|
promises.push(session.send('Network.setEmulateOfflineState', { offline: true }));
|
2020-01-09 17:06:06 -08:00
|
|
|
if (this._page._state.hasTouch)
|
|
|
|
promises.push(session.send('Page.setTouchEmulationEnabled', { enabled: true }));
|
2020-02-12 17:02:59 -08:00
|
|
|
if (contextOptions.timezoneId) {
|
|
|
|
promises.push(session.send('Page.setTimeZone', { timeZone: contextOptions.timezoneId }).
|
|
|
|
catch(e => { throw new Error(`Invalid timezone ID: ${contextOptions.timezoneId}`); }));
|
|
|
|
}
|
2020-01-30 17:30:47 -08:00
|
|
|
await Promise.all(promises);
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
2020-03-06 11:41:46 -08:00
|
|
|
private _onDidCommitProvisionalTarget(event: Protocol.Target.didCommitProvisionalTargetPayload) {
|
|
|
|
const { oldTargetId, newTargetId } = event;
|
|
|
|
const newSession = this._sessions.get(newTargetId);
|
|
|
|
assert(newSession, 'Unknown new target: ' + newTargetId);
|
|
|
|
const oldSession = this._sessions.get(oldTargetId);
|
|
|
|
assert(oldSession, 'Unknown old target: ' + oldTargetId);
|
|
|
|
oldSession.errorText = 'Target was swapped out.';
|
|
|
|
(newSession as any)[isPovisionalSymbol] = undefined;
|
2020-01-22 14:17:44 -08:00
|
|
|
assert(this._provisionalPage);
|
2020-03-06 11:41:46 -08:00
|
|
|
assert(this._provisionalPage._session === newSession);
|
2020-02-07 13:38:50 -08:00
|
|
|
this._provisionalPage.commit();
|
|
|
|
this._provisionalPage.dispose();
|
2020-01-22 14:17:44 -08:00
|
|
|
this._provisionalPage = null;
|
2020-03-06 11:41:46 -08:00
|
|
|
this._setSession(newSession);
|
2020-01-22 14:17:44 -08:00
|
|
|
}
|
|
|
|
|
2020-03-06 11:41:46 -08:00
|
|
|
private _onTargetDestroyed(event: Protocol.Target.targetDestroyedPayload) {
|
|
|
|
const { targetId, crashed } = event;
|
|
|
|
const session = this._sessions.get(targetId);
|
|
|
|
assert(session, 'Unknown target destroyed: ' + targetId);
|
|
|
|
session.dispose();
|
|
|
|
this._sessions.delete(targetId);
|
2020-01-22 14:17:44 -08:00
|
|
|
if (this._provisionalPage && this._provisionalPage._session === session) {
|
|
|
|
this._provisionalPage.dispose();
|
|
|
|
this._provisionalPage = null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this._session === session && crashed)
|
|
|
|
this.didClose(crashed);
|
|
|
|
}
|
|
|
|
|
2020-01-03 11:10:10 -08:00
|
|
|
didClose(crashed: boolean) {
|
2019-12-19 16:53:24 -08:00
|
|
|
helper.removeEventListeners(this._sessionListeners);
|
2020-01-03 11:10:10 -08:00
|
|
|
if (crashed)
|
|
|
|
this._page._didCrash();
|
|
|
|
else
|
|
|
|
this._page._didClose();
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
2020-01-22 14:17:44 -08:00
|
|
|
dispose() {
|
2020-03-06 11:41:46 -08:00
|
|
|
this._pageProxySession.dispose();
|
|
|
|
helper.removeEventListeners(this._eventListeners);
|
|
|
|
for (const session of this._sessions.values())
|
|
|
|
session.dispose();
|
|
|
|
this._sessions.clear();
|
2020-01-22 14:17:44 -08:00
|
|
|
if (this._provisionalPage) {
|
|
|
|
this._provisionalPage.dispose();
|
|
|
|
this._provisionalPage = null;
|
|
|
|
}
|
2020-01-09 15:14:35 -08:00
|
|
|
this._page._didDisconnect();
|
|
|
|
}
|
|
|
|
|
2020-03-06 11:41:46 -08:00
|
|
|
dispatchMessageToSession(message: any) {
|
|
|
|
this._pageProxySession.dispatchMessage(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleProvisionalLoadFailed(event: Protocol.Browser.provisionalLoadFailedPayload) {
|
|
|
|
if (!this._initialized || !this._provisionalPage)
|
|
|
|
return;
|
|
|
|
let errorText = event.error;
|
|
|
|
if (errorText.includes('cancelled'))
|
|
|
|
errorText += '; maybe frame was detached?';
|
|
|
|
this._page._frameManager.provisionalLoadFailed(this._page.mainFrame(), event.loaderId, errorText);
|
|
|
|
}
|
|
|
|
|
|
|
|
async pageOrError(): Promise<Page | Error> {
|
|
|
|
return this._pagePromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async _onTargetCreated(event: Protocol.Target.targetCreatedPayload) {
|
|
|
|
const { targetInfo } = event;
|
|
|
|
const session = new WKSession(this._pageProxySession.connection, targetInfo.targetId, `The ${targetInfo.type} has been closed.`, (message: any) => {
|
|
|
|
this._pageProxySession.send('Target.sendMessageToTarget', {
|
|
|
|
message: JSON.stringify(message), targetId: targetInfo.targetId
|
|
|
|
}).catch(e => {
|
|
|
|
session.dispatchMessage({ id: message.id, error: { message: e.message } });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
assert(targetInfo.type === 'page', 'Only page targets are expected in WebKit, received: ' + targetInfo.type);
|
|
|
|
this._sessions.set(targetInfo.targetId, session);
|
|
|
|
|
|
|
|
if (!this._initialized) {
|
|
|
|
assert(!targetInfo.isProvisional);
|
|
|
|
let pageOrError: Page | Error;
|
|
|
|
try {
|
|
|
|
this._setSession(session);
|
|
|
|
await Promise.all([
|
|
|
|
this._initializePageProxySession(),
|
|
|
|
this._initializeSession(session, ({frameTree}) => this._handleFrameTree(frameTree)),
|
|
|
|
]);
|
|
|
|
pageOrError = this._page;
|
|
|
|
} catch (e) {
|
|
|
|
pageOrError = e;
|
|
|
|
}
|
|
|
|
if (targetInfo.isPaused)
|
|
|
|
this._pageProxySession.send('Target.resume', { targetId: targetInfo.targetId }).catch(debugError);
|
|
|
|
this._initialized = true;
|
|
|
|
this._pagePromiseCallback(pageOrError);
|
|
|
|
} else {
|
|
|
|
assert(targetInfo.isProvisional);
|
|
|
|
(session as any)[isPovisionalSymbol] = true;
|
|
|
|
assert(!this._provisionalPage);
|
|
|
|
this._provisionalPage = new WKProvisionalPage(session, this);
|
|
|
|
if (targetInfo.isPaused) {
|
|
|
|
this._provisionalPage.initializationPromise.then(() => {
|
|
|
|
this._pageProxySession.send('Target.resume', { targetId: targetInfo.targetId }).catch(debugError);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private _onDispatchMessageFromTarget(event: Protocol.Target.dispatchMessageFromTargetPayload) {
|
|
|
|
const { targetId, message } = event;
|
|
|
|
const session = this._sessions.get(targetId);
|
|
|
|
assert(session, 'Unknown target: ' + targetId);
|
|
|
|
session.dispatchMessage(JSON.parse(message));
|
|
|
|
}
|
|
|
|
|
2020-01-22 14:17:44 -08:00
|
|
|
private _addSessionListeners() {
|
2019-12-19 16:53:24 -08:00
|
|
|
this._sessionListeners = [
|
|
|
|
helper.addEventListener(this._session, 'Page.frameNavigated', event => this._onFrameNavigated(event.frame, false)),
|
|
|
|
helper.addEventListener(this._session, 'Page.navigatedWithinDocument', event => this._onFrameNavigatedWithinDocument(event.frameId, event.url)),
|
|
|
|
helper.addEventListener(this._session, 'Page.frameAttached', event => this._onFrameAttached(event.frameId, event.parentFrameId)),
|
|
|
|
helper.addEventListener(this._session, 'Page.frameDetached', event => this._onFrameDetached(event.frameId)),
|
2020-03-04 19:15:01 -08:00
|
|
|
helper.addEventListener(this._session, 'Page.frameScheduledNavigation', event => this._onFrameScheduledNavigation(event.frameId)),
|
2019-12-19 16:53:24 -08:00
|
|
|
helper.addEventListener(this._session, 'Page.frameStoppedLoading', event => this._onFrameStoppedLoading(event.frameId)),
|
|
|
|
helper.addEventListener(this._session, 'Page.loadEventFired', event => this._onLifecycleEvent(event.frameId, 'load')),
|
|
|
|
helper.addEventListener(this._session, 'Page.domContentEventFired', event => this._onLifecycleEvent(event.frameId, 'domcontentloaded')),
|
|
|
|
helper.addEventListener(this._session, 'Runtime.executionContextCreated', event => this._onExecutionContextCreated(event.context)),
|
|
|
|
helper.addEventListener(this._session, 'Console.messageAdded', event => this._onConsoleMessage(event)),
|
2020-01-07 17:16:27 -08:00
|
|
|
helper.addEventListener(this._pageProxySession, 'Dialog.javascriptDialogOpening', event => this._onDialog(event)),
|
2020-01-06 13:49:39 -08:00
|
|
|
helper.addEventListener(this._session, 'Page.fileChooserOpened', event => this._onFileChooserOpened(event)),
|
2020-01-21 14:58:12 -08:00
|
|
|
helper.addEventListener(this._session, 'Network.requestWillBeSent', e => this._onRequestWillBeSent(this._session, e)),
|
|
|
|
helper.addEventListener(this._session, 'Network.requestIntercepted', e => this._onRequestIntercepted(e)),
|
|
|
|
helper.addEventListener(this._session, 'Network.responseReceived', e => this._onResponseReceived(e)),
|
|
|
|
helper.addEventListener(this._session, 'Network.loadingFinished', e => this._onLoadingFinished(e)),
|
|
|
|
helper.addEventListener(this._session, 'Network.loadingFailed', e => this._onLoadingFailed(e)),
|
2019-12-19 16:53:24 -08:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-01-22 14:17:44 -08:00
|
|
|
private async _updateState<T extends keyof Protocol.CommandParameters>(
|
|
|
|
method: T,
|
|
|
|
params?: Protocol.CommandParameters[T]
|
|
|
|
): Promise<void> {
|
|
|
|
await this._forAllSessions(session => session.send(method, params).then());
|
|
|
|
}
|
|
|
|
|
|
|
|
private async _forAllSessions(callback: ((session: WKSession) => Promise<void>)): Promise<void> {
|
|
|
|
const sessions = [
|
|
|
|
this._session
|
|
|
|
];
|
|
|
|
// If the state changes during provisional load, push it to the provisional page
|
|
|
|
// as well to always be in sync with the backend.
|
|
|
|
if (this._provisionalPage)
|
|
|
|
sessions.push(this._provisionalPage._session);
|
|
|
|
await Promise.all(sessions.map(session => callback(session).catch(debugError)));
|
|
|
|
}
|
|
|
|
|
2020-03-04 19:15:01 -08:00
|
|
|
private _onFrameScheduledNavigation(frameId: string) {
|
|
|
|
this._page._frameManager.frameRequestedNavigation(frameId);
|
|
|
|
}
|
|
|
|
|
2020-01-22 14:17:44 -08:00
|
|
|
private _onFrameStoppedLoading(frameId: string) {
|
2019-12-19 16:53:24 -08:00
|
|
|
this._page._frameManager.frameStoppedLoading(frameId);
|
|
|
|
}
|
|
|
|
|
2020-03-06 08:24:32 -08:00
|
|
|
private _onLifecycleEvent(frameId: string, event: types.LifecycleEvent) {
|
2019-12-19 16:53:24 -08:00
|
|
|
this._page._frameManager.frameLifecycleEvent(frameId, event);
|
|
|
|
}
|
|
|
|
|
2020-01-31 10:08:45 -08:00
|
|
|
private _handleFrameTree(frameTree: Protocol.Page.FrameResourceTree) {
|
|
|
|
this._onFrameAttached(frameTree.frame.id, frameTree.frame.parentId || null);
|
2019-12-19 16:53:24 -08:00
|
|
|
this._onFrameNavigated(frameTree.frame, true);
|
2020-01-28 13:05:38 -08:00
|
|
|
|
2019-12-19 16:53:24 -08:00
|
|
|
if (!frameTree.childFrames)
|
|
|
|
return;
|
|
|
|
for (const child of frameTree.childFrames)
|
2020-01-31 10:08:45 -08:00
|
|
|
this._handleFrameTree(child);
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
2020-01-28 13:05:38 -08:00
|
|
|
_onFrameAttached(frameId: string, parentFrameId: string | null): frames.Frame {
|
|
|
|
return this._page._frameManager.frameAttached(frameId, parentFrameId);
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
2020-01-22 14:17:44 -08:00
|
|
|
private _onFrameNavigated(framePayload: Protocol.Page.Frame, initial: boolean) {
|
2019-12-19 16:53:24 -08:00
|
|
|
const frame = this._page._frameManager.frame(framePayload.id);
|
2020-01-23 14:58:30 -08:00
|
|
|
assert(frame);
|
2020-02-07 13:38:50 -08:00
|
|
|
this._removeContextsForFrame(frame, true);
|
2020-01-17 18:46:45 -08:00
|
|
|
if (!framePayload.parentId)
|
|
|
|
this._workers.clear();
|
2020-01-10 15:16:06 -08:00
|
|
|
this._page._frameManager.frameCommittedNewDocumentNavigation(framePayload.id, framePayload.url, framePayload.name || '', framePayload.loaderId, initial);
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
2020-01-22 14:17:44 -08:00
|
|
|
private _onFrameNavigatedWithinDocument(frameId: string, url: string) {
|
2019-12-19 16:53:24 -08:00
|
|
|
this._page._frameManager.frameCommittedSameDocumentNavigation(frameId, url);
|
|
|
|
}
|
|
|
|
|
2020-01-22 14:17:44 -08:00
|
|
|
private _onFrameDetached(frameId: string) {
|
2019-12-19 16:53:24 -08:00
|
|
|
this._page._frameManager.frameDetached(frameId);
|
|
|
|
}
|
|
|
|
|
2020-01-23 14:58:30 -08:00
|
|
|
private _removeContextsForFrame(frame: frames.Frame, notifyFrame: boolean) {
|
|
|
|
for (const [contextId, context] of this._contextIdToContext) {
|
|
|
|
if (context.frame === frame) {
|
|
|
|
(context._delegate as WKExecutionContext)._dispose();
|
|
|
|
this._contextIdToContext.delete(contextId);
|
|
|
|
if (notifyFrame)
|
|
|
|
frame._contextDestroyed(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 13:36:49 -08:00
|
|
|
private _onExecutionContextCreated(contextPayload: Protocol.Runtime.ExecutionContextDescription) {
|
2019-12-19 16:53:24 -08:00
|
|
|
if (this._contextIdToContext.has(contextPayload.id))
|
|
|
|
return;
|
|
|
|
const frame = this._page._frameManager.frame(contextPayload.frameId);
|
|
|
|
if (!frame)
|
|
|
|
return;
|
2020-01-07 12:59:01 -08:00
|
|
|
const delegate = new WKExecutionContext(this._session, contextPayload.id);
|
2019-12-19 16:53:24 -08:00
|
|
|
const context = new dom.FrameExecutionContext(delegate, frame);
|
2020-01-29 19:13:44 -08:00
|
|
|
if (contextPayload.type === 'normal')
|
2019-12-19 16:53:24 -08:00
|
|
|
frame._contextCreated('main', context);
|
2020-01-29 19:13:44 -08:00
|
|
|
else if (contextPayload.type === 'user' && contextPayload.name === UTILITY_WORLD_NAME)
|
2019-12-19 16:53:24 -08:00
|
|
|
frame._contextCreated('utility', context);
|
2020-01-29 19:13:44 -08:00
|
|
|
if (contextPayload.type === 'normal' && frame === this._page.mainFrame())
|
2020-01-27 16:51:52 -08:00
|
|
|
this._mainFrameContextId = contextPayload.id;
|
2019-12-19 16:53:24 -08:00
|
|
|
this._contextIdToContext.set(contextPayload.id, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
async navigateFrame(frame: frames.Frame, url: string, referrer: string | undefined): Promise<frames.GotoResult> {
|
2020-01-14 11:46:08 -08:00
|
|
|
if (this._pageProxySession.isDisposed())
|
|
|
|
throw new Error('Target closed');
|
|
|
|
const pageProxyId = this._pageProxySession.sessionId;
|
2020-02-07 13:38:50 -08:00
|
|
|
const result = await this._pageProxySession.connection.browserSession.send('Browser.navigate', { url, pageProxyId, frameId: frame._id, referrer });
|
2020-02-10 18:35:47 -08:00
|
|
|
return { newDocumentId: result.loaderId };
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
2020-01-27 16:51:52 -08:00
|
|
|
private _onConsoleMessage(event: Protocol.Console.messageAddedPayload) {
|
|
|
|
// Note: do no introduce await in this function, otherwise we lose the ordering.
|
|
|
|
// For example, frame.setContent relies on this.
|
2020-01-03 10:07:49 -08:00
|
|
|
const { type, level, text, parameters, url, line: lineNumber, column: columnNumber, source } = event.message;
|
2019-12-19 16:53:24 -08:00
|
|
|
if (level === 'debug' && parameters && parameters[0].value === BINDING_CALL_MESSAGE) {
|
2020-01-13 13:33:25 -08:00
|
|
|
const parsedObjectId = JSON.parse(parameters[1].objectId!);
|
|
|
|
const context = this._contextIdToContext.get(parsedObjectId.injectedScriptId)!;
|
2019-12-19 16:53:24 -08:00
|
|
|
this._page._onBindingCalled(parameters[2].value, context);
|
|
|
|
return;
|
|
|
|
}
|
2020-01-03 10:07:49 -08:00
|
|
|
if (level === 'error' && source === 'javascript') {
|
|
|
|
const error = new Error(text);
|
2020-02-04 19:31:57 -08:00
|
|
|
error.stack = 'Error: ' + error.message; // Nullify stack. Stack is supposed to contain error message as the first line.
|
2020-01-03 10:07:49 -08:00
|
|
|
this._page.emit(Events.Page.PageError, error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-13 13:33:25 -08:00
|
|
|
let derivedType: string = type || '';
|
2019-12-19 16:53:24 -08:00
|
|
|
if (type === 'log')
|
|
|
|
derivedType = level;
|
|
|
|
else if (type === 'timing')
|
|
|
|
derivedType = 'timeEnd';
|
|
|
|
|
|
|
|
const handles = (parameters || []).map(p => {
|
|
|
|
let context: dom.FrameExecutionContext | null = null;
|
|
|
|
if (p.objectId) {
|
|
|
|
const objectId = JSON.parse(p.objectId);
|
2020-01-13 13:33:25 -08:00
|
|
|
context = this._contextIdToContext.get(objectId.injectedScriptId)!;
|
2019-12-19 16:53:24 -08:00
|
|
|
} else {
|
2020-01-27 16:51:52 -08:00
|
|
|
context = this._contextIdToContext.get(this._mainFrameContextId!)!;
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
return context._createHandle(p);
|
|
|
|
});
|
2020-01-13 13:33:25 -08:00
|
|
|
this._page._addConsoleMessage(derivedType, handles, { url, lineNumber: (lineNumber || 1) - 1, columnNumber: (columnNumber || 1) - 1 }, handles.length ? undefined : text);
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
_onDialog(event: Protocol.Dialog.javascriptDialogOpeningPayload) {
|
|
|
|
this._page.emit(Events.Page.Dialog, new dialog.Dialog(
|
|
|
|
event.type as dialog.DialogType,
|
|
|
|
event.message,
|
|
|
|
async (accept: boolean, promptText?: string) => {
|
2020-01-07 17:16:27 -08:00
|
|
|
await this._pageProxySession.send('Dialog.handleJavaScriptDialog', { accept, promptText });
|
2019-12-19 16:53:24 -08:00
|
|
|
},
|
|
|
|
event.defaultPrompt));
|
|
|
|
}
|
|
|
|
|
2020-01-22 14:17:44 -08:00
|
|
|
private async _onFileChooserOpened(event: {frameId: Protocol.Network.FrameId, element: Protocol.Runtime.RemoteObject}) {
|
2020-01-13 13:33:25 -08:00
|
|
|
const context = await this._page._frameManager.frame(event.frameId)!._mainContext();
|
2019-12-19 16:53:24 -08:00
|
|
|
const handle = context._createHandle(event.element).asElement()!;
|
|
|
|
this._page._onFileChooserOpened(handle);
|
|
|
|
}
|
|
|
|
|
2020-01-17 14:02:57 -08:00
|
|
|
private static async _setEmulateMedia(session: WKSession, mediaType: types.MediaType | null, colorScheme: types.ColorScheme | null): Promise<void> {
|
2019-12-19 16:53:24 -08:00
|
|
|
const promises = [];
|
|
|
|
promises.push(session.send('Page.setEmulatedMedia', { media: mediaType || '' }));
|
2020-01-03 12:59:06 -08:00
|
|
|
if (colorScheme !== null) {
|
2019-12-19 16:53:24 -08:00
|
|
|
let appearance: any = '';
|
2020-01-03 12:59:06 -08:00
|
|
|
switch (colorScheme) {
|
2019-12-19 16:53:24 -08:00
|
|
|
case 'light': appearance = 'Light'; break;
|
|
|
|
case 'dark': appearance = 'Dark'; break;
|
|
|
|
}
|
|
|
|
promises.push(session.send('Page.setForcedAppearance', { appearance }));
|
|
|
|
}
|
|
|
|
await Promise.all(promises);
|
|
|
|
}
|
|
|
|
|
2020-02-26 12:42:20 -08:00
|
|
|
async updateExtraHTTPHeaders(): Promise<void> {
|
|
|
|
await this._updateState('Network.setExtraHTTPHeaders', { headers: this._calculateExtraHTTPHeaders() });
|
|
|
|
}
|
|
|
|
|
|
|
|
_calculateExtraHTTPHeaders(): network.Headers {
|
|
|
|
const headers = network.mergeHeaders([
|
2020-03-05 17:22:57 -08:00
|
|
|
this._browserContext._options.extraHTTPHeaders,
|
2020-02-26 12:42:20 -08:00
|
|
|
this._page._state.extraHTTPHeaders
|
|
|
|
]);
|
2020-03-05 17:22:57 -08:00
|
|
|
const locale = this._browserContext._options.locale;
|
2020-02-13 13:37:59 -08:00
|
|
|
if (locale)
|
2020-02-26 12:42:20 -08:00
|
|
|
headers['Accept-Language'] = locale;
|
|
|
|
return headers;
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
2020-01-03 12:59:06 -08:00
|
|
|
async setEmulateMedia(mediaType: types.MediaType | null, colorScheme: types.ColorScheme | null): Promise<void> {
|
2020-01-22 14:17:44 -08:00
|
|
|
await this._forAllSessions(session => WKPage._setEmulateMedia(session, mediaType, colorScheme));
|
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);
|
|
|
|
await this._updateViewport(false /* updateTouch */);
|
|
|
|
}
|
|
|
|
|
|
|
|
async _updateViewport(updateTouch: boolean): Promise<void> {
|
2020-03-05 17:22:57 -08:00
|
|
|
let viewport = this._browserContext._options.viewport || { width: 0, height: 0 };
|
2020-02-06 19:02:55 -08:00
|
|
|
const viewportSize = this._page._state.viewportSize;
|
|
|
|
if (viewportSize)
|
|
|
|
viewport = { ...viewport, ...viewportSize };
|
|
|
|
const promises: Promise<any>[] = [
|
|
|
|
this._pageProxySession.send('Emulation.setDeviceMetricsOverride', {
|
|
|
|
width: viewport.width,
|
|
|
|
height: viewport.height,
|
|
|
|
fixedLayout: !!viewport.isMobile,
|
|
|
|
deviceScaleFactor: viewport.deviceScaleFactor || 1
|
|
|
|
}),
|
|
|
|
];
|
|
|
|
if (updateTouch)
|
|
|
|
promises.push(this._updateState('Page.setTouchEmulationEnabled', { enabled: !!viewport.isMobile }));
|
|
|
|
await Promise.all(promises);
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
2020-01-21 14:58:12 -08:00
|
|
|
async setRequestInterception(enabled: boolean): Promise<void> {
|
2020-01-22 14:17:44 -08:00
|
|
|
await this._updateState('Network.setInterceptionEnabled', { enabled, interceptRequests: enabled });
|
2019-12-30 14:05:28 -08:00
|
|
|
}
|
|
|
|
|
2020-03-04 17:58:12 -08:00
|
|
|
async updateOffline() {
|
2020-03-05 17:22:57 -08:00
|
|
|
await this._updateState('Network.setEmulateOfflineState', { offline: !!this._browserContext._options.offline });
|
2019-12-30 14:09:54 -08:00
|
|
|
}
|
|
|
|
|
2020-03-06 13:50:42 -08:00
|
|
|
async updateHttpCredentials() {
|
|
|
|
const credentials = this._browserContext._options.httpCredentials || { username: '', password: '' };
|
|
|
|
await this._pageProxySession.send('Emulation.setAuthCredentials', { username: credentials.username, password: credentials.password });
|
2019-12-30 14:09:54 -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-27 08:49:09 -08:00
|
|
|
async opener(): Promise<Page | null> {
|
2020-03-05 15:18:27 -08:00
|
|
|
if (!this._opener)
|
|
|
|
return null;
|
|
|
|
const openerPage = await this._opener.pageOrError();
|
|
|
|
if (openerPage instanceof Page && !openerPage.isClosed())
|
|
|
|
return openerPage;
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
|
|
|
goBack(): Promise<boolean> {
|
|
|
|
return this._session.send('Page.goBack').then(() => true).catch(error => {
|
|
|
|
if (error instanceof Error && error.message.includes(`Protocol error (Page.goBack): Failed to go`))
|
|
|
|
return false;
|
|
|
|
throw error;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
goForward(): Promise<boolean> {
|
|
|
|
return this._session.send('Page.goForward').then(() => true).catch(error => {
|
|
|
|
if (error instanceof Error && error.message.includes(`Protocol error (Page.goForward): Failed to go`))
|
|
|
|
return false;
|
|
|
|
throw error;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-03 16:46:06 -08:00
|
|
|
async exposeBinding(binding: PageBinding): Promise<void> {
|
2020-02-27 16:18:33 -08:00
|
|
|
await this._updateBootstrapScript();
|
2020-03-03 16:46:06 -08:00
|
|
|
await this._evaluateBindingScript(binding);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async _evaluateBindingScript(binding: PageBinding): Promise<void> {
|
|
|
|
const script = this._bindingToScript(binding);
|
2019-12-19 16:53:24 -08:00
|
|
|
await Promise.all(this._page.frames().map(frame => frame.evaluate(script).catch(debugError)));
|
|
|
|
}
|
|
|
|
|
|
|
|
async evaluateOnNewDocument(script: string): Promise<void> {
|
2020-03-03 16:46:06 -08:00
|
|
|
this._evaluateOnNewDocumentSources.push(script);
|
2020-02-27 16:18:33 -08:00
|
|
|
await this._updateBootstrapScript();
|
|
|
|
}
|
|
|
|
|
2020-03-03 16:46:06 -08:00
|
|
|
private _bindingToScript(binding: PageBinding): string {
|
|
|
|
return `self.${binding.name} = (param) => console.debug('${BINDING_CALL_MESSAGE}', {}, param); ${binding.source}`;
|
|
|
|
}
|
|
|
|
|
2020-02-27 16:18:33 -08:00
|
|
|
private _calculateBootstrapScript(): string {
|
2020-03-03 16:46:06 -08:00
|
|
|
const scripts: string[] = [];
|
|
|
|
for (const binding of this._browserContext._pageBindings.values())
|
|
|
|
scripts.push(this._bindingToScript(binding));
|
|
|
|
for (const binding of this._page._pageBindings.values())
|
|
|
|
scripts.push(this._bindingToScript(binding));
|
|
|
|
scripts.push(...this._browserContext._evaluateOnNewDocumentSources);
|
|
|
|
scripts.push(...this._evaluateOnNewDocumentSources);
|
|
|
|
return scripts.join(';');
|
2019-12-20 17:16:32 -07:00
|
|
|
}
|
|
|
|
|
2020-02-27 16:18:33 -08:00
|
|
|
async _updateBootstrapScript(): Promise<void> {
|
|
|
|
await this._updateState('Page.setBootstrapScript', { source: this._calculateBootstrapScript() });
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async closePage(runBeforeUnload: boolean): Promise<void> {
|
2020-01-08 16:34:45 -08:00
|
|
|
this._pageProxySession.send('Target.close', {
|
|
|
|
targetId: this._session.sessionId,
|
2020-01-07 10:39:01 -08:00
|
|
|
runBeforeUnload
|
|
|
|
}).catch(debugError);
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
canScreenshotOutsideViewport(): boolean {
|
2020-03-03 16:09:32 -08:00
|
|
|
return true;
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async setBackgroundColor(color?: { r: number; g: number; b: number; a: number; }): Promise<void> {
|
|
|
|
// TODO: line below crashes, sort it out.
|
2020-01-09 15:14:35 -08:00
|
|
|
await this._session.send('Page.setDefaultBackgroundColorOverride', { color });
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
2020-03-03 16:09:32 -08:00
|
|
|
async takeScreenshot(format: string, documentRect: types.Rect | undefined, viewportRect: types.Rect | undefined, quality: number | undefined): Promise<platform.BufferType> {
|
|
|
|
// TODO: documentRect does not include pageScale, while backend considers it does.
|
|
|
|
// This brakes mobile screenshots of elements or full page.
|
|
|
|
const rect = (documentRect || viewportRect)!;
|
|
|
|
const result = await this._session.send('Page.snapshotRect', { ...rect, coordinateSystem: documentRect ? 'Page' : 'Viewport' });
|
2019-12-19 16:53:24 -08:00
|
|
|
const prefix = 'data:image/png;base64,';
|
2020-01-07 11:55:24 -08:00
|
|
|
let buffer = platform.Buffer.from(result.dataURL.substr(prefix.length), 'base64');
|
2019-12-19 16:53:24 -08:00
|
|
|
if (format === 'jpeg')
|
2020-03-03 16:09:32 -08:00
|
|
|
buffer = platform.pngToJpeg(buffer, quality);
|
2019-12-19 16:53:24 -08:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2020-03-03 16:09:32 -08:00
|
|
|
async resetViewport(): Promise<void> {
|
|
|
|
assert(false, 'Should not be called');
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async getContentFrame(handle: dom.ElementHandle): Promise<frames.Frame | null> {
|
|
|
|
const nodeInfo = await this._session.send('DOM.describeNode', {
|
2020-01-13 13:33:25 -08:00
|
|
|
objectId: toRemoteObject(handle).objectId!
|
2019-12-19 16:53:24 -08:00
|
|
|
});
|
|
|
|
if (!nodeInfo.contentFrameId)
|
|
|
|
return null;
|
|
|
|
return this._page._frameManager.frame(nodeInfo.contentFrameId);
|
|
|
|
}
|
|
|
|
|
2020-01-27 11:43:43 -08:00
|
|
|
async getOwnerFrame(handle: dom.ElementHandle): Promise<string | null> {
|
2019-12-26 14:05:46 -08:00
|
|
|
const remoteObject = toRemoteObject(handle);
|
|
|
|
if (!remoteObject.objectId)
|
|
|
|
return null;
|
|
|
|
const nodeInfo = await this._session.send('DOM.describeNode', {
|
|
|
|
objectId: remoteObject.objectId
|
|
|
|
});
|
2020-01-27 11:43:43 -08:00
|
|
|
return nodeInfo.ownerFrameId || null;
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
isElementHandle(remoteObject: any): boolean {
|
|
|
|
return (remoteObject as Protocol.Runtime.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-02-11 10:30:09 -08:00
|
|
|
async scrollRectIntoViewIfNeeded(handle: dom.ElementHandle, rect?: types.Rect): Promise<void> {
|
|
|
|
await this._session.send('DOM.scrollIntoViewIfNeeded', {
|
|
|
|
objectId: toRemoteObject(handle).objectId!,
|
|
|
|
rect,
|
|
|
|
}).catch(e => {
|
|
|
|
if (e instanceof Error && e.message.includes('Node does not have a layout object'))
|
|
|
|
e.message = 'Node is either not visible or not an HTMLElement';
|
|
|
|
throw e;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-12-19 16:53:24 -08:00
|
|
|
async getContentQuads(handle: dom.ElementHandle): Promise<types.Quad[] | null> {
|
|
|
|
const result = await this._session.send('DOM.getContentQuads', {
|
2020-01-13 13:33:25 -08:00
|
|
|
objectId: toRemoteObject(handle).objectId!
|
2019-12-19 16:53:24 -08:00
|
|
|
}).catch(debugError);
|
|
|
|
if (!result)
|
|
|
|
return null;
|
|
|
|
return result.quads.map(quad => [
|
|
|
|
{ x: quad[0], y: quad[1] },
|
|
|
|
{ x: quad[2], y: quad[3] },
|
|
|
|
{ x: quad[4], y: quad[5] },
|
|
|
|
{ x: quad[6], y: quad[7] }
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
async layoutViewport(): Promise<{ width: number, height: number }> {
|
|
|
|
return this._page.evaluate(() => ({ width: innerWidth, height: innerHeight }));
|
|
|
|
}
|
|
|
|
|
2020-01-13 13:33:25 -08:00
|
|
|
async setInputFiles(handle: dom.ElementHandle<HTMLInputElement>, files: types.FilePayload[]): Promise<void> {
|
|
|
|
const objectId = toRemoteObject(handle).objectId!;
|
2019-12-19 16:53:24 -08:00
|
|
|
await this._session.send('DOM.setInputFiles', { objectId, files });
|
|
|
|
}
|
|
|
|
|
|
|
|
async adoptElementHandle<T extends Node>(handle: dom.ElementHandle<T>, to: dom.FrameExecutionContext): Promise<dom.ElementHandle<T>> {
|
|
|
|
const result = await this._session.send('DOM.resolveNode', {
|
|
|
|
objectId: toRemoteObject(handle).objectId,
|
|
|
|
executionContextId: (to._delegate as WKExecutionContext)._contextId
|
|
|
|
}).catch(debugError);
|
|
|
|
if (!result || result.object.subtype === 'null')
|
|
|
|
throw new Error('Unable to adopt element handle from a different document');
|
|
|
|
return to._createHandle(result.object) as dom.ElementHandle<T>;
|
|
|
|
}
|
2020-01-03 11:15:43 -08:00
|
|
|
|
2020-02-07 13:36:49 -08:00
|
|
|
async getAccessibilityTree(needle?: dom.ElementHandle): Promise<{tree: accessibility.AXNode, needle: accessibility.AXNode | null}> {
|
2020-01-14 16:54:50 -08:00
|
|
|
return getAccessibilityTree(this._session, needle);
|
2020-01-03 11:15:43 -08:00
|
|
|
}
|
2020-01-07 13:57:37 -08:00
|
|
|
|
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.');
|
|
|
|
const context = await parent._utilityContext();
|
|
|
|
const handles = await context._$$('iframe');
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-01-21 14:58:12 -08:00
|
|
|
_onRequestWillBeSent(session: WKSession, event: Protocol.Network.requestWillBeSentPayload) {
|
|
|
|
if (event.request.url.startsWith('data:'))
|
|
|
|
return;
|
|
|
|
let redirectChain: network.Request[] = [];
|
|
|
|
if (event.redirectResponse) {
|
|
|
|
const request = this._requestIdToRequest.get(event.requestId);
|
|
|
|
// If we connect late to the target, we could have missed the requestWillBeSent event.
|
|
|
|
if (request) {
|
|
|
|
this._handleRequestRedirect(request, event.redirectResponse);
|
|
|
|
redirectChain = request.request._redirectChain;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const frame = this._page._frameManager.frame(event.frameId);
|
|
|
|
// TODO(einbinder) this will fail if we are an XHR document request
|
|
|
|
const isNavigationRequest = event.type === 'Document';
|
|
|
|
const documentId = isNavigationRequest ? event.loaderId : undefined;
|
2020-01-31 16:23:15 -08:00
|
|
|
const request = new WKInterceptableRequest(session, !!this._page._state.interceptNetwork, frame, event, redirectChain, documentId);
|
2020-01-21 14:58:12 -08:00
|
|
|
this._requestIdToRequest.set(event.requestId, request);
|
|
|
|
this._page._frameManager.requestStarted(request.request);
|
|
|
|
}
|
|
|
|
|
|
|
|
private _handleRequestRedirect(request: WKInterceptableRequest, responsePayload: Protocol.Network.Response) {
|
|
|
|
const response = request.createResponse(responsePayload);
|
|
|
|
request.request._redirectChain.push(request.request);
|
|
|
|
response._requestFinished(new Error('Response body is unavailable for redirect responses'));
|
|
|
|
this._requestIdToRequest.delete(request._requestId);
|
|
|
|
this._page._frameManager.requestReceivedResponse(response);
|
|
|
|
this._page._frameManager.requestFinished(request.request);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onRequestIntercepted(event: Protocol.Network.requestInterceptedPayload) {
|
|
|
|
const request = this._requestIdToRequest.get(event.requestId);
|
|
|
|
if (request)
|
|
|
|
request._interceptedCallback();
|
|
|
|
}
|
|
|
|
|
|
|
|
_onResponseReceived(event: Protocol.Network.responseReceivedPayload) {
|
|
|
|
const request = this._requestIdToRequest.get(event.requestId);
|
|
|
|
// FileUpload sends a response without a matching request.
|
|
|
|
if (!request)
|
|
|
|
return;
|
|
|
|
const response = request.createResponse(event.response);
|
|
|
|
this._page._frameManager.requestReceivedResponse(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onLoadingFinished(event: Protocol.Network.loadingFinishedPayload) {
|
|
|
|
const request = this._requestIdToRequest.get(event.requestId);
|
|
|
|
// For certain requestIds we never receive requestWillBeSent event.
|
|
|
|
// @see https://crbug.com/750469
|
|
|
|
if (!request)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Under certain conditions we never get the Network.responseReceived
|
|
|
|
// event from protocol. @see https://crbug.com/883475
|
|
|
|
const response = request.request.response();
|
|
|
|
if (response)
|
|
|
|
response._requestFinished();
|
|
|
|
this._requestIdToRequest.delete(request._requestId);
|
|
|
|
this._page._frameManager.requestFinished(request.request);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onLoadingFailed(event: Protocol.Network.loadingFailedPayload) {
|
|
|
|
const request = this._requestIdToRequest.get(event.requestId);
|
|
|
|
// For certain requestIds we never receive requestWillBeSent event.
|
|
|
|
// @see https://crbug.com/750469
|
|
|
|
if (!request)
|
|
|
|
return;
|
|
|
|
const response = request.request.response();
|
|
|
|
if (response)
|
|
|
|
response._requestFinished();
|
|
|
|
this._requestIdToRequest.delete(request._requestId);
|
|
|
|
request.request._setFailureText(event.errorText);
|
|
|
|
this._page._frameManager.requestFailed(request.request, event.errorText.includes('cancelled'));
|
|
|
|
}
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
function toRemoteObject(handle: dom.ElementHandle): Protocol.Runtime.RemoteObject {
|
|
|
|
return handle._remoteObject as Protocol.Runtime.RemoteObject;
|
|
|
|
}
|