2019-11-18 18:18:28 -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.
|
|
|
|
*/
|
|
|
|
|
2019-12-09 13:08:21 -08:00
|
|
|
import * as dom from './dom';
|
|
|
|
import * as frames from './frames';
|
2020-04-29 18:35:04 -07:00
|
|
|
import { assert, helper, Listener, assertMaxArguments } from './helper';
|
2019-12-09 13:08:21 -08:00
|
|
|
import * as input from './input';
|
|
|
|
import * as js from './javascript';
|
|
|
|
import * as network from './network';
|
2019-12-11 12:36:42 -08:00
|
|
|
import { Screenshotter } from './screenshotter';
|
2019-12-12 20:54:40 -08:00
|
|
|
import { TimeoutSettings } from './timeoutSettings';
|
2019-12-09 13:08:21 -08:00
|
|
|
import * as types from './types';
|
2019-11-22 14:46:34 -08:00
|
|
|
import { Events } from './events';
|
2020-03-05 17:22:57 -08:00
|
|
|
import { BrowserContext, BrowserContextBase } from './browserContext';
|
2019-12-10 15:17:42 -08:00
|
|
|
import { ConsoleMessage, ConsoleMessageLocation } from './console';
|
2020-01-03 11:15:43 -08:00
|
|
|
import * as accessibility from './accessibility';
|
2020-04-01 14:42:47 -07:00
|
|
|
import { EventEmitter } from 'events';
|
2020-04-16 10:25:28 -07:00
|
|
|
import { FileChooser } from './fileChooser';
|
2020-06-16 17:11:19 -07:00
|
|
|
import { logError, Logger } from './logger';
|
|
|
|
import { ProgressController, Progress } from './progress';
|
2019-12-09 13:08:21 -08:00
|
|
|
|
|
|
|
export interface PageDelegate {
|
|
|
|
readonly rawMouse: input.RawMouse;
|
|
|
|
readonly rawKeyboard: input.RawKeyboard;
|
2019-12-11 12:36:42 -08:00
|
|
|
|
2020-01-31 18:38:45 -08:00
|
|
|
opener(): Promise<Page | null>;
|
|
|
|
|
2019-12-17 11:28:09 -08:00
|
|
|
reload(): Promise<void>;
|
|
|
|
goBack(): Promise<boolean>;
|
|
|
|
goForward(): Promise<boolean>;
|
2020-03-03 16:46:06 -08:00
|
|
|
exposeBinding(binding: PageBinding): Promise<void>;
|
2019-12-09 13:08:21 -08:00
|
|
|
evaluateOnNewDocument(source: string): Promise<void>;
|
|
|
|
closePage(runBeforeUnload: boolean): Promise<void>;
|
|
|
|
|
2019-12-16 22:02:33 -08:00
|
|
|
navigateFrame(frame: frames.Frame, url: string, referrer: string | undefined): Promise<frames.GotoResult>;
|
2019-12-11 12:36:42 -08:00
|
|
|
|
2020-02-26 12:42:20 -08:00
|
|
|
updateExtraHTTPHeaders(): Promise<void>;
|
2020-02-06 19:02:55 -08:00
|
|
|
setViewportSize(viewportSize: types.Size): Promise<void>;
|
2020-04-06 19:49:33 -07:00
|
|
|
updateEmulateMedia(): Promise<void>;
|
2020-03-09 21:02:54 -07:00
|
|
|
updateRequestInterception(): Promise<void>;
|
2020-01-30 17:43:06 -08:00
|
|
|
setFileChooserIntercepted(enabled: boolean): Promise<void>;
|
2019-12-11 12:36:42 -08:00
|
|
|
|
|
|
|
canScreenshotOutsideViewport(): boolean;
|
2020-03-03 16:09:32 -08:00
|
|
|
resetViewport(): Promise<void>; // Only called if canScreenshotOutsideViewport() returns false.
|
2019-12-11 12:36:42 -08:00
|
|
|
setBackgroundColor(color?: { r: number; g: number; b: number; a: number; }): Promise<void>;
|
2020-04-01 14:42:47 -07:00
|
|
|
takeScreenshot(format: string, documentRect: types.Rect | undefined, viewportRect: types.Rect | undefined, quality: number | undefined): Promise<Buffer>;
|
2019-12-12 17:51:05 -08:00
|
|
|
|
|
|
|
isElementHandle(remoteObject: any): boolean;
|
2019-12-12 21:11:52 -08:00
|
|
|
adoptElementHandle<T extends Node>(handle: dom.ElementHandle<T>, to: dom.FrameExecutionContext): Promise<dom.ElementHandle<T>>;
|
2019-12-19 15:19:22 -08:00
|
|
|
getContentFrame(handle: dom.ElementHandle): Promise<frames.Frame | null>; // Only called for frame owner elements.
|
2020-01-27 11:43:43 -08:00
|
|
|
getOwnerFrame(handle: dom.ElementHandle): Promise<string | null>; // Returns frameId.
|
2019-12-12 17:51:05 -08:00
|
|
|
getContentQuads(handle: dom.ElementHandle): Promise<types.Quad[] | null>;
|
2020-01-13 13:33:25 -08:00
|
|
|
setInputFiles(handle: dom.ElementHandle<HTMLInputElement>, files: types.FilePayload[]): Promise<void>;
|
2019-12-12 17:51:05 -08:00
|
|
|
getBoundingBox(handle: dom.ElementHandle): Promise<types.Rect | null>;
|
2020-02-05 17:20:23 -08:00
|
|
|
getFrameElement(frame: frames.Frame): Promise<dom.ElementHandle>;
|
2020-06-12 14:59:26 -07:00
|
|
|
scrollRectIntoViewIfNeeded(handle: dom.ElementHandle, rect?: types.Rect): Promise<'notvisible' | 'notconnected' | 'done'>;
|
2020-05-04 16:30:19 -07:00
|
|
|
rafCountForStablePosition(): number;
|
2020-01-03 11:15:43 -08:00
|
|
|
|
2020-01-14 16:54:50 -08:00
|
|
|
getAccessibilityTree(needle?: dom.ElementHandle): Promise<{tree: accessibility.AXNode, needle: accessibility.AXNode | null}>;
|
2020-04-01 14:42:47 -07:00
|
|
|
pdf?: (options?: types.PDFOptions) => Promise<Buffer>;
|
2020-02-13 17:39:14 -08:00
|
|
|
coverage?: () => any;
|
2020-03-07 08:19:31 -08:00
|
|
|
|
|
|
|
// Work around Chrome's non-associated input and protocol.
|
|
|
|
inputActionEpilogue(): Promise<void>;
|
2020-04-17 08:51:54 -07:00
|
|
|
// Work around for asynchronously dispatched CSP errors in Firefox.
|
|
|
|
readonly cspErrorsAsynchronousForInlineScipts?: boolean;
|
2019-12-09 13:08:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
type PageState = {
|
2020-02-06 19:02:55 -08:00
|
|
|
viewportSize: types.Size | null;
|
2020-01-03 12:59:06 -08:00
|
|
|
mediaType: types.MediaType | null;
|
|
|
|
colorScheme: types.ColorScheme | null;
|
2019-12-09 13:08:21 -08:00
|
|
|
extraHTTPHeaders: network.Headers | null;
|
|
|
|
};
|
|
|
|
|
2020-06-10 15:12:50 -07:00
|
|
|
export class Page extends EventEmitter {
|
2019-11-18 18:18:28 -08:00
|
|
|
private _closed = false;
|
2019-12-05 14:11:48 -08:00
|
|
|
private _closedCallback: () => void;
|
|
|
|
private _closedPromise: Promise<void>;
|
2019-12-06 13:36:47 -08:00
|
|
|
private _disconnected = false;
|
|
|
|
private _disconnectedCallback: (e: Error) => void;
|
2019-12-10 15:34:36 -07:00
|
|
|
readonly _disconnectedPromise: Promise<Error>;
|
2020-03-05 17:22:57 -08:00
|
|
|
readonly _browserContext: BrowserContextBase;
|
2019-12-06 13:36:47 -08:00
|
|
|
readonly keyboard: input.Keyboard;
|
|
|
|
readonly mouse: input.Mouse;
|
2019-12-09 13:08:21 -08:00
|
|
|
readonly _timeoutSettings: TimeoutSettings;
|
|
|
|
readonly _delegate: PageDelegate;
|
2020-06-16 17:11:19 -07:00
|
|
|
readonly _logger: Logger;
|
2019-12-09 13:08:21 -08:00
|
|
|
readonly _state: PageState;
|
2020-03-03 16:46:06 -08:00
|
|
|
readonly _pageBindings = new Map<string, PageBinding>();
|
2020-06-11 11:41:36 -07:00
|
|
|
readonly _evaluateOnNewDocumentSources: string[] = [];
|
2019-12-09 13:08:21 -08:00
|
|
|
readonly _screenshotter: Screenshotter;
|
2019-12-16 15:56:11 -08:00
|
|
|
readonly _frameManager: frames.FrameManager;
|
2020-01-03 11:15:43 -08:00
|
|
|
readonly accessibility: accessibility.Accessibility;
|
2020-01-07 12:59:01 -08:00
|
|
|
private _workers = new Map<string, Worker>();
|
2020-04-01 14:42:47 -07:00
|
|
|
readonly pdf: ((options?: types.PDFOptions) => Promise<Buffer>) | undefined;
|
2020-02-13 17:39:14 -08:00
|
|
|
readonly coverage: any;
|
2020-04-15 19:55:22 -07:00
|
|
|
_routes: { url: types.URLMatch, handler: network.RouteHandler }[] = [];
|
2020-02-11 12:06:58 -08:00
|
|
|
_ownedContext: BrowserContext | undefined;
|
2020-06-16 17:11:19 -07:00
|
|
|
_callingPageAPI = false;
|
2019-11-18 18:18:28 -08:00
|
|
|
|
2020-03-05 17:22:57 -08:00
|
|
|
constructor(delegate: PageDelegate, browserContext: BrowserContextBase) {
|
2019-11-18 18:18:28 -08:00
|
|
|
super();
|
2019-12-09 13:08:21 -08:00
|
|
|
this._delegate = delegate;
|
2020-06-16 17:11:19 -07:00
|
|
|
this._logger = browserContext._apiLogger;
|
2020-01-13 13:33:25 -08:00
|
|
|
this._closedCallback = () => {};
|
2019-12-05 14:11:48 -08:00
|
|
|
this._closedPromise = new Promise(f => this._closedCallback = f);
|
2020-01-13 13:33:25 -08:00
|
|
|
this._disconnectedCallback = () => {};
|
2019-12-06 13:36:47 -08:00
|
|
|
this._disconnectedPromise = new Promise(f => this._disconnectedCallback = f);
|
2019-12-05 14:11:48 -08:00
|
|
|
this._browserContext = browserContext;
|
2019-12-09 13:08:21 -08:00
|
|
|
this._state = {
|
2020-05-12 18:31:17 -07:00
|
|
|
viewportSize: browserContext._options.viewport ? { ...browserContext._options.viewport } : null,
|
2020-01-05 14:39:16 -08:00
|
|
|
mediaType: null,
|
|
|
|
colorScheme: null,
|
2019-12-09 13:08:21 -08:00
|
|
|
extraHTTPHeaders: null,
|
|
|
|
};
|
2020-01-03 11:15:43 -08:00
|
|
|
this.accessibility = new accessibility.Accessibility(delegate.getAccessibilityTree.bind(delegate));
|
2019-12-09 13:08:21 -08:00
|
|
|
this.keyboard = new input.Keyboard(delegate.rawKeyboard);
|
|
|
|
this.mouse = new input.Mouse(delegate.rawMouse, this.keyboard);
|
2020-02-13 14:18:18 -08:00
|
|
|
this._timeoutSettings = new TimeoutSettings(browserContext._timeoutSettings);
|
2019-12-11 12:36:42 -08:00
|
|
|
this._screenshotter = new Screenshotter(this);
|
2019-12-16 15:56:11 -08:00
|
|
|
this._frameManager = new frames.FrameManager(this);
|
2020-01-07 13:57:37 -08:00
|
|
|
if (delegate.pdf)
|
|
|
|
this.pdf = delegate.pdf.bind(delegate);
|
2020-02-13 17:39:14 -08:00
|
|
|
this.coverage = delegate.coverage ? delegate.coverage() : null;
|
2019-12-05 14:11:48 -08:00
|
|
|
}
|
|
|
|
|
2020-06-16 17:11:19 -07:00
|
|
|
private _runAbortableTask<T>(task: (progress: Progress) => Promise<T>, timeout: number, apiName: string): Promise<T> {
|
|
|
|
const controller = new ProgressController(this._logger, timeout, `page.${apiName}`);
|
|
|
|
return controller.run(task);
|
|
|
|
}
|
|
|
|
|
2019-12-05 14:11:48 -08:00
|
|
|
_didClose() {
|
|
|
|
assert(!this._closed, 'Page closed twice');
|
|
|
|
this._closed = true;
|
|
|
|
this.emit(Events.Page.Close);
|
|
|
|
this._closedCallback();
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-01-03 11:10:10 -08:00
|
|
|
_didCrash() {
|
2020-04-15 00:04:35 -07:00
|
|
|
this.emit(Events.Page.Crash);
|
2020-01-03 11:10:10 -08:00
|
|
|
}
|
|
|
|
|
2019-12-06 13:36:47 -08:00
|
|
|
_didDisconnect() {
|
|
|
|
assert(!this._disconnected, 'Page disconnected twice');
|
|
|
|
this._disconnected = true;
|
|
|
|
this._disconnectedCallback(new Error('Target closed'));
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-12-06 13:36:47 -08:00
|
|
|
async _onFileChooserOpened(handle: dom.ElementHandle) {
|
2020-01-13 13:33:25 -08:00
|
|
|
const multiple = await handle.evaluate(element => !!(element as HTMLInputElement).multiple);
|
2019-12-17 17:34:32 -08:00
|
|
|
if (!this.listenerCount(Events.Page.FileChooser)) {
|
2020-03-04 17:57:35 -08:00
|
|
|
handle.dispose();
|
2019-11-18 18:18:28 -08:00
|
|
|
return;
|
2019-12-06 13:36:47 -08:00
|
|
|
}
|
2020-04-16 10:25:28 -07:00
|
|
|
const fileChooser = new FileChooser(this, handle, multiple);
|
2019-11-27 14:26:30 -08:00
|
|
|
this.emit(Events.Page.FileChooser, fileChooser);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-02-10 10:41:45 -08:00
|
|
|
context(): BrowserContext {
|
2019-12-05 14:11:48 -08:00
|
|
|
return this._browserContext;
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-01-31 18:38:45 -08:00
|
|
|
async opener(): Promise<Page | null> {
|
|
|
|
return await this._delegate.opener();
|
|
|
|
}
|
|
|
|
|
2019-11-27 16:03:51 -08:00
|
|
|
mainFrame(): frames.Frame {
|
2019-12-16 15:56:11 -08:00
|
|
|
return this._frameManager.mainFrame();
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-03-06 09:38:08 -08:00
|
|
|
frame(options: string | { name?: string, url?: types.URLMatch }): frames.Frame | null {
|
|
|
|
const name = helper.isString(options) ? options : options.name;
|
|
|
|
const url = helper.isObject(options) ? options.url : undefined;
|
|
|
|
assert(name || url, 'Either name or url matcher should be specified');
|
2020-03-04 17:53:39 -08:00
|
|
|
return this.frames().find(f => {
|
2020-03-06 09:38:08 -08:00
|
|
|
if (name)
|
|
|
|
return f.name() === name;
|
2020-03-16 14:39:44 -07:00
|
|
|
return helper.urlMatches(f.url(), url);
|
2020-03-04 17:53:39 -08:00
|
|
|
}) || null;
|
|
|
|
}
|
|
|
|
|
2019-11-27 16:03:51 -08:00
|
|
|
frames(): frames.Frame[] {
|
2019-12-16 15:56:11 -08:00
|
|
|
return this._frameManager.frames();
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
setDefaultNavigationTimeout(timeout: number) {
|
|
|
|
this._timeoutSettings.setDefaultNavigationTimeout(timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
setDefaultTimeout(timeout: number) {
|
|
|
|
this._timeoutSettings.setDefaultTimeout(timeout);
|
|
|
|
}
|
|
|
|
|
2019-12-18 14:28:16 -08:00
|
|
|
async $(selector: string): Promise<dom.ElementHandle<Element> | null> {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._attributeToPage(() => this.mainFrame().$(selector));
|
2019-12-18 14:28:16 -08:00
|
|
|
}
|
|
|
|
|
2020-03-06 15:02:42 -08:00
|
|
|
async waitForSelector(selector: string, options?: types.WaitForElementOptions): Promise<dom.ElementHandle<Element> | null> {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._attributeToPage(() => this.mainFrame().waitForSelector(selector, options));
|
2020-02-11 14:51:09 -08:00
|
|
|
}
|
|
|
|
|
2020-04-23 14:58:37 -07:00
|
|
|
async dispatchEvent(selector: string, type: string, eventInit?: Object, options?: types.TimeoutOptions): Promise<void> {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._attributeToPage(() => this.mainFrame().dispatchEvent(selector, type, eventInit, options));
|
2020-04-23 14:58:37 -07:00
|
|
|
}
|
|
|
|
|
2020-06-23 14:51:06 -07:00
|
|
|
async evaluateHandle<R, Arg>(pageFunction: js.Func1<Arg, R>, arg: Arg): Promise<js.SmartHandle<R>>;
|
|
|
|
async evaluateHandle<R>(pageFunction: js.Func1<void, R>, arg?: any): Promise<js.SmartHandle<R>>;
|
|
|
|
async evaluateHandle<R, Arg>(pageFunction: js.Func1<Arg, R>, arg: Arg): Promise<js.SmartHandle<R>> {
|
2020-04-29 18:35:04 -07:00
|
|
|
assertMaxArguments(arguments.length, 2);
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._attributeToPage(() => this.mainFrame().evaluateHandle(pageFunction, arg));
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-06-23 14:51:06 -07:00
|
|
|
async $eval<R, Arg>(selector: string, pageFunction: js.FuncOn<Element, Arg, R>, arg: Arg): Promise<R>;
|
|
|
|
async $eval<R>(selector: string, pageFunction: js.FuncOn<Element, void, R>, arg?: any): Promise<R>;
|
|
|
|
async $eval<R, Arg>(selector: string, pageFunction: js.FuncOn<Element, Arg, R>, arg: Arg): Promise<R> {
|
2020-04-29 18:35:04 -07:00
|
|
|
assertMaxArguments(arguments.length, 3);
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._attributeToPage(() => this.mainFrame().$eval(selector, pageFunction, arg));
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-06-23 14:51:06 -07:00
|
|
|
async $$eval<R, Arg>(selector: string, pageFunction: js.FuncOn<Element[], Arg, R>, arg: Arg): Promise<R>;
|
|
|
|
async $$eval<R>(selector: string, pageFunction: js.FuncOn<Element[], void, R>, arg?: any): Promise<R>;
|
|
|
|
async $$eval<R, Arg>(selector: string, pageFunction: js.FuncOn<Element[], Arg, R>, arg: Arg): Promise<R> {
|
2020-04-29 18:35:04 -07:00
|
|
|
assertMaxArguments(arguments.length, 3);
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._attributeToPage(() => this.mainFrame().$$eval(selector, pageFunction, arg));
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-12-17 14:30:02 -08:00
|
|
|
async $$(selector: string): Promise<dom.ElementHandle<Element>[]> {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._attributeToPage(() => this.mainFrame().$$(selector));
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-11-27 16:02:31 -08:00
|
|
|
async addScriptTag(options: { url?: string; path?: string; content?: string; type?: string; }): Promise<dom.ElementHandle> {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._attributeToPage(() => this.mainFrame().addScriptTag(options));
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-11-27 16:02:31 -08:00
|
|
|
async addStyleTag(options: { url?: string; path?: string; content?: string; }): Promise<dom.ElementHandle> {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._attributeToPage(() => this.mainFrame().addStyleTag(options));
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async exposeFunction(name: string, playwrightFunction: Function) {
|
2020-05-18 14:28:06 -07:00
|
|
|
await this.exposeBinding(name, (options, ...args: any) => playwrightFunction(...args));
|
|
|
|
}
|
|
|
|
|
|
|
|
async exposeBinding(name: string, playwrightBinding: frames.FunctionWithSource) {
|
2019-11-18 18:18:28 -08:00
|
|
|
if (this._pageBindings.has(name))
|
2020-03-03 16:46:06 -08:00
|
|
|
throw new Error(`Function "${name}" has been already registered`);
|
|
|
|
if (this._browserContext._pageBindings.has(name))
|
|
|
|
throw new Error(`Function "${name}" has been already registered in the browser context`);
|
2020-05-18 14:28:06 -07:00
|
|
|
const binding = new PageBinding(name, playwrightBinding);
|
2020-03-03 16:46:06 -08:00
|
|
|
this._pageBindings.set(name, binding);
|
|
|
|
await this._delegate.exposeBinding(binding);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-12-09 13:08:21 -08:00
|
|
|
setExtraHTTPHeaders(headers: network.Headers) {
|
2020-02-26 12:42:20 -08:00
|
|
|
this._state.extraHTTPHeaders = network.verifyHeaders(headers);
|
|
|
|
return this._delegate.updateExtraHTTPHeaders();
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-05-18 14:28:06 -07:00
|
|
|
async _onBindingCalled(payload: string, context: dom.FrameExecutionContext) {
|
2020-03-03 16:46:06 -08:00
|
|
|
await PageBinding.dispatch(this, payload, context);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-12-10 15:17:42 -08:00
|
|
|
_addConsoleMessage(type: string, args: js.JSHandle[], location: ConsoleMessageLocation, text?: string) {
|
2020-01-27 16:51:52 -08:00
|
|
|
const message = new ConsoleMessage(type, text, args, location);
|
|
|
|
const intercepted = this._frameManager.interceptConsoleMessage(message);
|
|
|
|
if (intercepted || !this.listenerCount(Events.Page.Console))
|
2019-11-18 18:18:28 -08:00
|
|
|
args.forEach(arg => arg.dispose());
|
2020-01-27 16:51:52 -08:00
|
|
|
else
|
|
|
|
this.emit(Events.Page.Console, message);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
url(): string {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._attributeToPage(() => this.mainFrame().url());
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-12-20 15:31:20 -08:00
|
|
|
async content(): Promise<string> {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._attributeToPage(() => this.mainFrame().content());
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-03-06 08:24:32 -08:00
|
|
|
async setContent(html: string, options?: types.NavigateOptions): Promise<void> {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._attributeToPage(() => this.mainFrame().setContent(html, options));
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-12-20 15:31:20 -08:00
|
|
|
async goto(url: string, options?: frames.GotoOptions): Promise<network.Response | null> {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._attributeToPage(() => this.mainFrame().goto(url, options));
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-03-06 08:24:32 -08:00
|
|
|
async reload(options?: types.NavigateOptions): Promise<network.Response | null> {
|
2019-12-17 11:28:09 -08:00
|
|
|
const waitPromise = this.waitForNavigation(options);
|
|
|
|
await this._delegate.reload();
|
|
|
|
return waitPromise;
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-03-23 13:51:11 -07:00
|
|
|
async waitForLoadState(state?: types.LifecycleEvent, options?: types.TimeoutOptions): Promise<void> {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._attributeToPage(() => this.mainFrame().waitForLoadState(state, options));
|
2020-03-16 14:39:44 -07:00
|
|
|
}
|
|
|
|
|
2020-03-06 08:24:32 -08:00
|
|
|
async waitForNavigation(options?: types.WaitForNavigationOptions): Promise<network.Response | null> {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._attributeToPage(() => this.mainFrame().waitForNavigation(options));
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-12-20 14:38:54 -08:00
|
|
|
async waitForRequest(urlOrPredicate: string | RegExp | ((r: network.Request) => boolean), options: types.TimeoutOptions = {}): Promise<network.Request> {
|
2020-06-05 14:14:19 -07:00
|
|
|
const predicate = (request: network.Request) => {
|
2020-02-25 10:32:17 +08:00
|
|
|
if (helper.isString(urlOrPredicate) || helper.isRegExp(urlOrPredicate))
|
2020-03-16 14:39:44 -07:00
|
|
|
return helper.urlMatches(request.url(), urlOrPredicate);
|
2019-12-20 14:38:54 -08:00
|
|
|
return urlOrPredicate(request);
|
2020-06-05 14:14:19 -07:00
|
|
|
};
|
|
|
|
return this.waitForEvent(Events.Page.Request, { predicate, timeout: options.timeout });
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-12-20 14:38:54 -08:00
|
|
|
async waitForResponse(urlOrPredicate: string | RegExp | ((r: network.Response) => boolean), options: types.TimeoutOptions = {}): Promise<network.Response> {
|
2020-06-05 14:14:19 -07:00
|
|
|
const predicate = (response: network.Response) => {
|
2020-02-25 10:32:17 +08:00
|
|
|
if (helper.isString(urlOrPredicate) || helper.isRegExp(urlOrPredicate))
|
2020-03-16 14:39:44 -07:00
|
|
|
return helper.urlMatches(response.url(), urlOrPredicate);
|
2019-12-20 14:38:54 -08:00
|
|
|
return urlOrPredicate(response);
|
2020-06-05 14:14:19 -07:00
|
|
|
};
|
|
|
|
return this.waitForEvent(Events.Page.Response, { predicate, timeout: options.timeout });
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-06-10 15:12:50 -07:00
|
|
|
async waitForEvent(event: string, optionsOrPredicate: types.WaitForEventOptions = {}): Promise<any> {
|
|
|
|
const options = typeof optionsOrPredicate === 'function' ? { predicate: optionsOrPredicate } : optionsOrPredicate;
|
2020-06-16 17:11:19 -07:00
|
|
|
const progressController = new ProgressController(this._logger, this._timeoutSettings.timeout(options), 'page.waitForEvent');
|
2020-06-10 15:12:50 -07:00
|
|
|
this._disconnectedPromise.then(error => progressController.abort(error));
|
|
|
|
return progressController.run(progress => helper.waitForEvent(progress, this, event, options.predicate));
|
|
|
|
}
|
|
|
|
|
2020-03-06 08:24:32 -08:00
|
|
|
async goBack(options?: types.NavigateOptions): Promise<network.Response | null> {
|
2019-12-17 11:28:09 -08:00
|
|
|
const waitPromise = this.waitForNavigation(options);
|
|
|
|
const result = await this._delegate.goBack();
|
|
|
|
if (!result) {
|
|
|
|
waitPromise.catch(() => {});
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return waitPromise;
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-03-06 08:24:32 -08:00
|
|
|
async goForward(options?: types.NavigateOptions): Promise<network.Response | null> {
|
2019-12-17 11:28:09 -08:00
|
|
|
const waitPromise = this.waitForNavigation(options);
|
|
|
|
const result = await this._delegate.goForward();
|
|
|
|
if (!result) {
|
|
|
|
waitPromise.catch(() => {});
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return waitPromise;
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-01-05 14:39:16 -08:00
|
|
|
async emulateMedia(options: { media?: types.MediaType, colorScheme?: types.ColorScheme }) {
|
|
|
|
assert(!options.media || types.mediaTypes.has(options.media), 'Unsupported media: ' + options.media);
|
2020-01-03 12:59:06 -08:00
|
|
|
assert(!options.colorScheme || types.colorSchemes.has(options.colorScheme), 'Unsupported color scheme: ' + options.colorScheme);
|
2020-01-05 14:39:16 -08:00
|
|
|
if (options.media !== undefined)
|
|
|
|
this._state.mediaType = options.media;
|
2019-12-09 13:08:21 -08:00
|
|
|
if (options.colorScheme !== undefined)
|
2019-12-18 12:23:33 -08:00
|
|
|
this._state.colorScheme = options.colorScheme;
|
2020-04-06 19:49:33 -07:00
|
|
|
await this._delegate.updateEmulateMedia();
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-02-06 19:02:55 -08:00
|
|
|
async setViewportSize(viewportSize: types.Size) {
|
|
|
|
this._state.viewportSize = { ...viewportSize };
|
|
|
|
await this._delegate.setViewportSize(this._state.viewportSize);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-02-06 19:02:55 -08:00
|
|
|
viewportSize(): types.Size | null {
|
|
|
|
return this._state.viewportSize;
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-06-23 14:51:06 -07:00
|
|
|
async evaluate<R, Arg>(pageFunction: js.Func1<Arg, R>, arg: Arg): Promise<R>;
|
|
|
|
async evaluate<R>(pageFunction: js.Func1<void, R>, arg?: any): Promise<R>;
|
|
|
|
async evaluate<R, Arg>(pageFunction: js.Func1<Arg, R>, arg: Arg): Promise<R> {
|
2020-04-29 18:35:04 -07:00
|
|
|
assertMaxArguments(arguments.length, 2);
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._attributeToPage(() => this.mainFrame().evaluate(pageFunction, arg));
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-03-20 15:08:17 -07:00
|
|
|
async addInitScript(script: Function | string | { path?: string, content?: string }, arg?: any) {
|
2020-06-11 11:41:36 -07:00
|
|
|
const source = await helper.evaluationScript(script, arg);
|
|
|
|
this._evaluateOnNewDocumentSources.push(source);
|
|
|
|
await this._delegate.evaluateOnNewDocument(source);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-03-09 21:02:54 -07:00
|
|
|
_needsRequestInterception(): boolean {
|
|
|
|
return this._routes.length > 0 || this._browserContext._routes.length > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
async route(url: types.URLMatch, handler: network.RouteHandler): Promise<void> {
|
|
|
|
this._routes.push({ url, handler });
|
|
|
|
await this._delegate.updateRequestInterception();
|
2020-02-03 14:23:24 -08:00
|
|
|
}
|
|
|
|
|
2020-04-15 19:55:22 -07:00
|
|
|
async unroute(url: types.URLMatch, handler?: network.RouteHandler): Promise<void> {
|
|
|
|
this._routes = this._routes.filter(route => route.url !== url || (handler && route.handler !== handler));
|
|
|
|
await this._delegate.updateRequestInterception();
|
|
|
|
}
|
|
|
|
|
2020-02-03 14:23:24 -08:00
|
|
|
_requestStarted(request: network.Request) {
|
|
|
|
this.emit(Events.Page.Request, request);
|
2020-03-13 14:30:40 -07:00
|
|
|
const route = request._route();
|
|
|
|
if (!route)
|
2020-01-31 16:23:15 -08:00
|
|
|
return;
|
2020-03-09 21:02:54 -07:00
|
|
|
for (const { url, handler } of this._routes) {
|
2020-03-16 14:39:44 -07:00
|
|
|
if (helper.urlMatches(request.url(), url)) {
|
2020-03-13 14:30:40 -07:00
|
|
|
handler(route, request);
|
2020-03-09 21:02:54 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const { url, handler } of this._browserContext._routes) {
|
2020-03-16 14:39:44 -07:00
|
|
|
if (helper.urlMatches(request.url(), url)) {
|
2020-03-13 14:30:40 -07:00
|
|
|
handler(route, request);
|
2020-02-03 14:23:24 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-03-13 14:30:40 -07:00
|
|
|
route.continue();
|
2020-01-31 16:23:15 -08:00
|
|
|
}
|
|
|
|
|
2020-06-19 17:14:32 -07:00
|
|
|
_isRouted(requestURL: string): boolean {
|
|
|
|
for (const { url } of this._routes) {
|
|
|
|
if (helper.urlMatches(requestURL, url))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
for (const { url } of this._browserContext._routes) {
|
|
|
|
if (helper.urlMatches(requestURL, url))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-24 10:16:54 -07:00
|
|
|
async screenshot(options: types.ScreenshotOptions = {}): Promise<Buffer> {
|
|
|
|
const controller = new ProgressController(this._logger, this._timeoutSettings.timeout(options), 'page.screenshot');
|
|
|
|
return controller.run(progress => this._screenshotter.screenshotPage(progress, options));
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-12-20 15:31:20 -08:00
|
|
|
async title(): Promise<string> {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._attributeToPage(() => this.mainFrame().title());
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async close(options: { runBeforeUnload: (boolean | undefined); } = {runBeforeUnload: undefined}) {
|
2019-12-18 16:23:05 -08:00
|
|
|
if (this._closed)
|
|
|
|
return;
|
2019-12-06 13:36:47 -08:00
|
|
|
assert(!this._disconnected, 'Protocol error: Connection closed. Most likely the page has been closed.');
|
2019-11-18 18:18:28 -08:00
|
|
|
const runBeforeUnload = !!options.runBeforeUnload;
|
2019-12-09 13:08:21 -08:00
|
|
|
await this._delegate.closePage(runBeforeUnload);
|
|
|
|
if (!runBeforeUnload)
|
2019-12-05 14:11:48 -08:00
|
|
|
await this._closedPromise;
|
2020-02-11 12:06:58 -08:00
|
|
|
if (this._ownedContext)
|
|
|
|
await this._ownedContext.close();
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
isClosed(): boolean {
|
|
|
|
return this._closed;
|
|
|
|
}
|
|
|
|
|
2020-06-16 17:11:19 -07:00
|
|
|
private _attributeToPage<T>(func: () => T): T {
|
|
|
|
try {
|
|
|
|
this._callingPageAPI = true;
|
|
|
|
return func();
|
|
|
|
} finally {
|
|
|
|
this._callingPageAPI = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 14:51:06 -07:00
|
|
|
async click(selector: string, options?: types.MouseClickOptions & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions) {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._attributeToPage(() => this.mainFrame().click(selector, options));
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-06-23 14:51:06 -07:00
|
|
|
async dblclick(selector: string, options?: types.MouseMultiClickOptions & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions) {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._attributeToPage(() => this.mainFrame().dblclick(selector, options));
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-03-06 16:24:21 -08:00
|
|
|
async fill(selector: string, value: string, options?: types.NavigatingActionWaitOptions) {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._attributeToPage(() => this.mainFrame().fill(selector, value, options));
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-03-06 16:24:21 -08:00
|
|
|
async focus(selector: string, options?: types.TimeoutOptions) {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._attributeToPage(() => this.mainFrame().focus(selector, options));
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-05-18 17:58:23 -07:00
|
|
|
async textContent(selector: string, options?: types.TimeoutOptions): Promise<null|string> {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._attributeToPage(() => this.mainFrame().textContent(selector, options));
|
2020-05-18 17:58:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async innerText(selector: string, options?: types.TimeoutOptions): Promise<string> {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._attributeToPage(() => this.mainFrame().innerText(selector, options));
|
2020-05-18 17:58:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async innerHTML(selector: string, options?: types.TimeoutOptions): Promise<string> {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._attributeToPage(() => this.mainFrame().innerHTML(selector, options));
|
2020-05-18 17:58:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async getAttribute(selector: string, name: string, options?: types.TimeoutOptions): Promise<string | null> {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._attributeToPage(() => this.mainFrame().getAttribute(selector, name, options));
|
2020-05-18 17:58:23 -07:00
|
|
|
}
|
|
|
|
|
2020-06-23 14:51:06 -07:00
|
|
|
async hover(selector: string, options?: types.PointerActionOptions & types.PointerActionWaitOptions) {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._attributeToPage(() => this.mainFrame().hover(selector, options));
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-06-10 13:37:29 -03:00
|
|
|
async selectOption(selector: string, values: string | dom.ElementHandle | types.SelectOption | string[] | dom.ElementHandle[] | types.SelectOption[] | null, options?: types.NavigatingActionWaitOptions): Promise<string[]> {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._attributeToPage(() => this.mainFrame().selectOption(selector, values, options));
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-04-16 10:25:28 -07:00
|
|
|
async setInputFiles(selector: string, files: string | types.FilePayload | string[] | types.FilePayload[], options?: types.NavigatingActionWaitOptions): Promise<void> {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._attributeToPage(() => this.mainFrame().setInputFiles(selector, files, options));
|
2020-04-16 10:25:28 -07:00
|
|
|
}
|
|
|
|
|
2020-03-06 16:24:21 -08:00
|
|
|
async type(selector: string, text: string, options?: { delay?: number } & types.NavigatingActionWaitOptions) {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._attributeToPage(() => this.mainFrame().type(selector, text, options));
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-03-12 22:02:19 -07:00
|
|
|
async press(selector: string, key: string, options?: { delay?: number } & types.NavigatingActionWaitOptions) {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._attributeToPage(() => this.mainFrame().press(selector, key, options));
|
2020-03-06 09:38:08 -08:00
|
|
|
}
|
|
|
|
|
2020-03-06 16:24:21 -08:00
|
|
|
async check(selector: string, options?: types.PointerActionWaitOptions & types.NavigatingActionWaitOptions) {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._attributeToPage(() => this.mainFrame().check(selector, options));
|
2020-02-04 14:39:11 -08:00
|
|
|
}
|
|
|
|
|
2020-03-06 16:24:21 -08:00
|
|
|
async uncheck(selector: string, options?: types.PointerActionWaitOptions & types.NavigatingActionWaitOptions) {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._attributeToPage(() => this.mainFrame().uncheck(selector, options));
|
2020-02-04 14:39:11 -08:00
|
|
|
}
|
|
|
|
|
2020-04-27 12:08:29 -07:00
|
|
|
async waitForTimeout(timeout: number) {
|
|
|
|
await this.mainFrame().waitForTimeout(timeout);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-06-23 14:51:06 -07:00
|
|
|
async waitForFunction<R, Arg>(pageFunction: js.Func1<Arg, R>, arg: Arg, options?: types.WaitForFunctionOptions): Promise<js.SmartHandle<R>>;
|
|
|
|
async waitForFunction<R>(pageFunction: js.Func1<void, R>, arg?: any, options?: types.WaitForFunctionOptions): Promise<js.SmartHandle<R>>;
|
|
|
|
async waitForFunction<R, Arg>(pageFunction: js.Func1<Arg, R>, arg: Arg, options?: types.WaitForFunctionOptions): Promise<js.SmartHandle<R>> {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._attributeToPage(() => this.mainFrame().waitForFunction(pageFunction, arg, options));
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
2019-12-18 18:11:02 -08:00
|
|
|
|
2020-01-07 12:59:01 -08:00
|
|
|
workers(): Worker[] {
|
|
|
|
return [...this._workers.values()];
|
|
|
|
}
|
|
|
|
|
|
|
|
_addWorker(workerId: string, worker: Worker) {
|
|
|
|
this._workers.set(workerId, worker);
|
2020-02-27 14:02:48 -08:00
|
|
|
this.emit(Events.Page.Worker, worker);
|
2020-01-07 12:59:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
_removeWorker(workerId: string) {
|
|
|
|
const worker = this._workers.get(workerId);
|
|
|
|
if (!worker)
|
|
|
|
return;
|
2020-02-27 14:02:48 -08:00
|
|
|
worker.emit(Events.Worker.Close, worker);
|
2020-01-07 12:59:01 -08:00
|
|
|
this._workers.delete(workerId);
|
|
|
|
}
|
|
|
|
|
|
|
|
_clearWorkers() {
|
2020-01-23 17:52:06 -08:00
|
|
|
for (const [workerId, worker] of this._workers) {
|
2020-02-27 14:02:48 -08:00
|
|
|
worker.emit(Events.Worker.Close, worker);
|
2020-01-23 17:52:06 -08:00
|
|
|
this._workers.delete(workerId);
|
|
|
|
}
|
2020-01-07 12:59:01 -08:00
|
|
|
}
|
2020-01-30 17:43:06 -08:00
|
|
|
|
2020-04-01 14:42:47 -07:00
|
|
|
on(event: string | symbol, listener: Listener): this {
|
2020-01-30 17:43:06 -08:00
|
|
|
if (event === Events.Page.FileChooser) {
|
|
|
|
if (!this.listenerCount(event))
|
|
|
|
this._delegate.setFileChooserIntercepted(true);
|
|
|
|
}
|
|
|
|
super.on(event, listener);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-04-01 14:42:47 -07:00
|
|
|
removeListener(event: string | symbol, listener: Listener): this {
|
2020-01-30 17:43:06 -08:00
|
|
|
super.removeListener(event, listener);
|
|
|
|
if (event === Events.Page.FileChooser && !this.listenerCount(event))
|
|
|
|
this._delegate.setFileChooserIntercepted(false);
|
|
|
|
return this;
|
|
|
|
}
|
2020-01-07 12:59:01 -08:00
|
|
|
}
|
|
|
|
|
2020-04-01 14:42:47 -07:00
|
|
|
export class Worker extends EventEmitter {
|
2020-01-07 12:59:01 -08:00
|
|
|
private _url: string;
|
|
|
|
private _executionContextPromise: Promise<js.ExecutionContext>;
|
|
|
|
private _executionContextCallback: (value?: js.ExecutionContext) => void;
|
2020-01-13 13:33:25 -08:00
|
|
|
_existingExecutionContext: js.ExecutionContext | null = null;
|
2020-01-07 12:59:01 -08:00
|
|
|
|
2020-06-09 16:11:17 -07:00
|
|
|
constructor(url: string) {
|
2020-02-27 14:02:48 -08:00
|
|
|
super();
|
2020-01-07 12:59:01 -08:00
|
|
|
this._url = url;
|
2020-01-13 13:33:25 -08:00
|
|
|
this._executionContextCallback = () => {};
|
2020-01-07 12:59:01 -08:00
|
|
|
this._executionContextPromise = new Promise(x => this._executionContextCallback = x);
|
|
|
|
}
|
2020-01-13 09:14:28 -08:00
|
|
|
|
2020-01-07 12:59:01 -08:00
|
|
|
_createExecutionContext(delegate: js.ExecutionContextDelegate) {
|
2020-06-09 16:11:17 -07:00
|
|
|
this._existingExecutionContext = new js.ExecutionContext(delegate);
|
2020-01-07 12:59:01 -08:00
|
|
|
this._executionContextCallback(this._existingExecutionContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
url(): string {
|
|
|
|
return this._url;
|
|
|
|
}
|
|
|
|
|
2020-06-23 14:51:06 -07:00
|
|
|
async evaluate<R, Arg>(pageFunction: js.Func1<Arg, R>, arg: Arg): Promise<R>;
|
|
|
|
async evaluate<R>(pageFunction: js.Func1<void, R>, arg?: any): Promise<R>;
|
|
|
|
async evaluate<R, Arg>(pageFunction: js.Func1<Arg, R>, arg: Arg): Promise<R> {
|
2020-04-29 18:35:04 -07:00
|
|
|
assertMaxArguments(arguments.length, 2);
|
2020-06-03 17:50:16 -07:00
|
|
|
return js.evaluate(await this._executionContextPromise, true /* returnByValue */, pageFunction, arg);
|
2020-01-07 12:59:01 -08:00
|
|
|
}
|
|
|
|
|
2020-06-23 14:51:06 -07:00
|
|
|
async evaluateHandle<R, Arg>(pageFunction: js.Func1<Arg, R>, arg: Arg): Promise<js.SmartHandle<R>>;
|
|
|
|
async evaluateHandle<R>(pageFunction: js.Func1<void, R>, arg?: any): Promise<js.SmartHandle<R>>;
|
|
|
|
async evaluateHandle<R, Arg>(pageFunction: js.Func1<Arg, R>, arg: Arg): Promise<js.SmartHandle<R>> {
|
2020-04-29 18:35:04 -07:00
|
|
|
assertMaxArguments(arguments.length, 2);
|
2020-06-03 17:50:16 -07:00
|
|
|
return js.evaluate(await this._executionContextPromise, false /* returnByValue */, pageFunction, arg);
|
2020-01-07 12:59:01 -08:00
|
|
|
}
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
2020-03-03 16:46:06 -08:00
|
|
|
|
|
|
|
export class PageBinding {
|
|
|
|
readonly name: string;
|
2020-05-18 14:28:06 -07:00
|
|
|
readonly playwrightFunction: frames.FunctionWithSource;
|
2020-03-03 16:46:06 -08:00
|
|
|
readonly source: string;
|
|
|
|
|
2020-05-18 14:28:06 -07:00
|
|
|
constructor(name: string, playwrightFunction: frames.FunctionWithSource) {
|
2020-03-03 16:46:06 -08:00
|
|
|
this.name = name;
|
|
|
|
this.playwrightFunction = playwrightFunction;
|
|
|
|
this.source = helper.evaluationString(addPageBinding, name);
|
|
|
|
}
|
|
|
|
|
2020-05-18 14:28:06 -07:00
|
|
|
static async dispatch(page: Page, payload: string, context: dom.FrameExecutionContext) {
|
2020-03-03 16:46:06 -08:00
|
|
|
const {name, seq, args} = JSON.parse(payload);
|
|
|
|
let expression = null;
|
|
|
|
try {
|
|
|
|
let binding = page._pageBindings.get(name);
|
|
|
|
if (!binding)
|
2020-03-05 17:22:57 -08:00
|
|
|
binding = page._browserContext._pageBindings.get(name);
|
2020-05-18 14:28:06 -07:00
|
|
|
const result = await binding!.playwrightFunction({ frame: context.frame, page, context: page._browserContext }, ...args);
|
2020-03-03 16:46:06 -08:00
|
|
|
expression = helper.evaluationString(deliverResult, name, seq, result);
|
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof Error)
|
|
|
|
expression = helper.evaluationString(deliverError, name, seq, error.message, error.stack);
|
|
|
|
else
|
|
|
|
expression = helper.evaluationString(deliverErrorValue, name, seq, error);
|
|
|
|
}
|
2020-06-09 16:11:17 -07:00
|
|
|
context.evaluateInternal(expression).catch(logError(page._logger));
|
2020-03-03 16:46:06 -08:00
|
|
|
|
|
|
|
function deliverResult(name: string, seq: number, result: any) {
|
|
|
|
(window as any)[name]['callbacks'].get(seq).resolve(result);
|
|
|
|
(window as any)[name]['callbacks'].delete(seq);
|
|
|
|
}
|
|
|
|
|
|
|
|
function deliverError(name: string, seq: number, message: string, stack: string) {
|
|
|
|
const error = new Error(message);
|
|
|
|
error.stack = stack;
|
|
|
|
(window as any)[name]['callbacks'].get(seq).reject(error);
|
|
|
|
(window as any)[name]['callbacks'].delete(seq);
|
|
|
|
}
|
|
|
|
|
|
|
|
function deliverErrorValue(name: string, seq: number, value: any) {
|
|
|
|
(window as any)[name]['callbacks'].get(seq).reject(value);
|
|
|
|
(window as any)[name]['callbacks'].delete(seq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function addPageBinding(bindingName: string) {
|
|
|
|
const binding = (window as any)[bindingName];
|
2020-03-10 16:19:01 -07:00
|
|
|
if (binding.__installed)
|
|
|
|
return;
|
2020-03-03 16:46:06 -08:00
|
|
|
(window as any)[bindingName] = (...args: any[]) => {
|
|
|
|
const me = (window as any)[bindingName];
|
|
|
|
let callbacks = me['callbacks'];
|
|
|
|
if (!callbacks) {
|
|
|
|
callbacks = new Map();
|
|
|
|
me['callbacks'] = callbacks;
|
|
|
|
}
|
|
|
|
const seq = (me['lastSeq'] || 0) + 1;
|
|
|
|
me['lastSeq'] = seq;
|
|
|
|
const promise = new Promise((resolve, reject) => callbacks.set(seq, {resolve, reject}));
|
|
|
|
binding(JSON.stringify({name: bindingName, seq, args}));
|
|
|
|
return promise;
|
|
|
|
};
|
2020-03-10 16:19:01 -07:00
|
|
|
(window as any)[bindingName].__installed = true;
|
2020-03-03 16:46:06 -08:00
|
|
|
}
|