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.
|
|
|
|
*/
|
|
|
|
|
2022-04-06 13:57:14 -08:00
|
|
|
import type * as dom from './dom';
|
2019-12-09 13:08:21 -08:00
|
|
|
import * as frames from './frames';
|
|
|
|
import * as input from './input';
|
|
|
|
import * as js from './javascript';
|
|
|
|
import * as network from './network';
|
2022-09-20 18:41:51 -07:00
|
|
|
import type * as channels from '@protocol/channels';
|
2022-04-06 13:57:14 -08:00
|
|
|
import type { ScreenshotOptions } from './screenshotter';
|
2022-04-19 08:43:18 -06:00
|
|
|
import { Screenshotter, validateScreenshotOptions } from './screenshotter';
|
2022-04-07 13:36:13 -08:00
|
|
|
import { TimeoutSettings } from '../common/timeoutSettings';
|
2022-04-06 13:57:14 -08:00
|
|
|
import type * as types from './types';
|
2021-03-31 10:38:05 -07:00
|
|
|
import { BrowserContext } from './browserContext';
|
2020-06-25 18:01:18 -07:00
|
|
|
import { ConsoleMessage } from './console';
|
2020-01-03 11:15:43 -08:00
|
|
|
import * as accessibility from './accessibility';
|
2020-04-16 10:25:28 -07:00
|
|
|
import { FileChooser } from './fileChooser';
|
2022-04-06 13:57:14 -08:00
|
|
|
import type { Progress } from './progress';
|
|
|
|
import { ProgressController } from './progress';
|
2022-04-07 12:55:44 -08:00
|
|
|
import { assert, isError } from '../utils';
|
2022-04-07 19:18:22 -08:00
|
|
|
import { ManualPromise } from '../utils/manualPromise';
|
2022-04-07 13:36:13 -08:00
|
|
|
import { debugLogger } from '../common/debugLogger';
|
2022-04-06 13:57:14 -08:00
|
|
|
import type { ImageComparatorOptions } from '../utils/comparators';
|
|
|
|
import { getComparator } from '../utils/comparators';
|
|
|
|
import type { SelectorInfo, Selectors } from './selectors';
|
|
|
|
import type { CallMetadata } from './instrumentation';
|
|
|
|
import { SdkObject } from './instrumentation';
|
|
|
|
import type { Artifact } from './artifact';
|
|
|
|
import type { TimeoutOptions } from '../common/types';
|
2022-04-07 20:48:41 -08:00
|
|
|
import type { ParsedSelector } from './isomorphic/selectorParser';
|
|
|
|
import { isInvalidSelectorError } from './isomorphic/selectorParser';
|
2022-05-09 14:07:04 -08:00
|
|
|
import { parseEvaluationResultValue, source } from './isomorphic/utilityScriptSerializers';
|
|
|
|
import type { SerializedValue } from './isomorphic/utilityScriptSerializers';
|
2019-12-09 13:08:21 -08:00
|
|
|
|
|
|
|
export interface PageDelegate {
|
|
|
|
readonly rawMouse: input.RawMouse;
|
|
|
|
readonly rawKeyboard: input.RawKeyboard;
|
2020-10-19 10:07:33 -07:00
|
|
|
readonly rawTouchscreen: input.RawTouchscreen;
|
2019-12-11 12:36:42 -08:00
|
|
|
|
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>;
|
2022-04-04 11:39:43 -08:00
|
|
|
removeExposedBindings(): Promise<void>;
|
2022-04-02 18:02:27 -08:00
|
|
|
addInitScript(source: string): Promise<void>;
|
2022-04-04 11:39:43 -08:00
|
|
|
removeInitScripts(): Promise<void>;
|
2019-12-09 13:08:21 -08:00
|
|
|
closePage(runBeforeUnload: boolean): Promise<void>;
|
2022-03-17 17:27:33 -08:00
|
|
|
potentiallyUninitializedPage(): Page;
|
2020-11-12 12:41:23 -08:00
|
|
|
pageOrError(): Promise<Page | Error>;
|
2019-12-09 13:08:21 -08:00
|
|
|
|
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>;
|
2022-08-03 16:14:28 -07:00
|
|
|
updateEmulatedViewportSize(preserveWindowBoundaries?: boolean): Promise<void>;
|
2020-04-06 19:49:33 -07:00
|
|
|
updateEmulateMedia(): Promise<void>;
|
2020-03-09 21:02:54 -07:00
|
|
|
updateRequestInterception(): Promise<void>;
|
2022-07-11 12:10:08 -08:00
|
|
|
updateFileChooserInterception(): Promise<void>;
|
2020-07-21 09:36:54 -07:00
|
|
|
bringToFront(): Promise<void>;
|
2019-12-11 12:36:42 -08:00
|
|
|
|
|
|
|
setBackgroundColor(color?: { r: number; g: number; b: number; a: number; }): Promise<void>;
|
2022-04-01 12:28:40 -07:00
|
|
|
takeScreenshot(progress: Progress, format: string, documentRect: types.Rect | undefined, viewportRect: types.Rect | undefined, quality: number | undefined, fitsViewport: boolean, scale: 'css' | 'device'): 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>;
|
2022-03-24 07:46:37 -07:00
|
|
|
setInputFiles(handle: dom.ElementHandle<HTMLInputElement>, files: types.FilePayload[]): Promise<void>;
|
2022-03-18 09:00:52 -07:00
|
|
|
setInputFilePaths(handle: dom.ElementHandle<HTMLInputElement>, files: string[]): 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-24 15:12:17 -07:00
|
|
|
scrollRectIntoViewIfNeeded(handle: dom.ElementHandle, rect?: types.Rect): Promise<'error:notvisible' | 'error:notconnected' | 'done'>;
|
2021-05-11 13:21:01 -07:00
|
|
|
setScreencastOptions(options: { width: number, height: number, quality: number } | null): Promise<void>;
|
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}>;
|
2022-06-14 22:02:15 -07:00
|
|
|
pdf?: (options: channels.PagePdfParams) => Promise<Buffer>;
|
2020-02-13 17:39:14 -08:00
|
|
|
coverage?: () => any;
|
2020-03-07 08:19:31 -08:00
|
|
|
|
2020-06-25 16:57:21 -07:00
|
|
|
// Work around WebKit's raf issues on Windows.
|
|
|
|
rafCountForStablePosition(): number;
|
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
|
|
|
}
|
|
|
|
|
2022-07-07 15:28:20 -08:00
|
|
|
type EmulatedSize = { screen: types.Size, viewport: types.Size };
|
|
|
|
|
|
|
|
type EmulatedMedia = {
|
|
|
|
media: types.MediaType | null;
|
2020-01-03 12:59:06 -08:00
|
|
|
colorScheme: types.ColorScheme | null;
|
2021-05-22 01:56:09 +02:00
|
|
|
reducedMotion: types.ReducedMotion | null;
|
2021-09-03 21:48:06 +02:00
|
|
|
forcedColors: types.ForcedColors | null;
|
2019-12-09 13:08:21 -08:00
|
|
|
};
|
|
|
|
|
2022-02-28 13:25:59 -07:00
|
|
|
type ExpectScreenshotOptions = {
|
|
|
|
timeout?: number,
|
|
|
|
expected?: Buffer,
|
|
|
|
isNot?: boolean,
|
|
|
|
locator?: {
|
|
|
|
frame: frames.Frame,
|
|
|
|
selector: string,
|
|
|
|
},
|
|
|
|
comparatorOptions?: ImageComparatorOptions,
|
|
|
|
screenshotOptions?: ScreenshotOptions,
|
|
|
|
};
|
|
|
|
|
2021-02-09 09:00:00 -08:00
|
|
|
export class Page extends SdkObject {
|
2020-08-21 16:26:33 -07:00
|
|
|
static Events = {
|
|
|
|
Close: 'close',
|
|
|
|
Crash: 'crash',
|
|
|
|
Console: 'console',
|
|
|
|
Dialog: 'dialog',
|
|
|
|
Download: 'download',
|
|
|
|
FileChooser: 'filechooser',
|
|
|
|
// Can't use just 'error' due to node.js special treatment of error events.
|
|
|
|
// @see https://nodejs.org/api/events.html#events_error_events
|
|
|
|
PageError: 'pageerror',
|
|
|
|
FrameAttached: 'frameattached',
|
|
|
|
FrameDetached: 'framedetached',
|
2021-01-13 14:25:42 -08:00
|
|
|
InternalFrameNavigatedToNewDocument: 'internalframenavigatedtonewdocument',
|
2021-04-08 05:32:12 +08:00
|
|
|
ScreencastFrame: 'screencastframe',
|
2021-03-31 10:38:05 -07:00
|
|
|
Video: 'video',
|
2020-10-26 22:20:43 -07:00
|
|
|
WebSocket: 'websocket',
|
2020-08-21 16:26:33 -07:00
|
|
|
Worker: 'worker',
|
|
|
|
};
|
|
|
|
|
2020-06-29 16:26:32 -07:00
|
|
|
private _closedState: 'open' | 'closing' | 'closed' = 'open';
|
2021-08-29 11:21:06 -07:00
|
|
|
private _closedPromise = new ManualPromise<void>();
|
2019-12-06 13:36:47 -08:00
|
|
|
private _disconnected = false;
|
2021-05-13 10:29:14 -07:00
|
|
|
private _initialized = false;
|
2021-08-29 11:21:06 -07:00
|
|
|
readonly _disconnectedPromise = new ManualPromise<Error>();
|
|
|
|
readonly _crashedPromise = new ManualPromise<Error>();
|
2020-08-19 10:31:59 -07:00
|
|
|
readonly _browserContext: BrowserContext;
|
2019-12-06 13:36:47 -08:00
|
|
|
readonly keyboard: input.Keyboard;
|
|
|
|
readonly mouse: input.Mouse;
|
2020-10-19 10:07:33 -07:00
|
|
|
readonly touchscreen: input.Touchscreen;
|
2019-12-09 13:08:21 -08:00
|
|
|
readonly _timeoutSettings: TimeoutSettings;
|
|
|
|
readonly _delegate: PageDelegate;
|
2022-07-07 15:28:20 -08:00
|
|
|
_emulatedSize: EmulatedSize | undefined;
|
|
|
|
private _extraHTTPHeaders: types.HeadersArray | undefined;
|
|
|
|
private _emulatedMedia: Partial<EmulatedMedia> = {};
|
|
|
|
private _interceptFileChooser = false;
|
2020-12-02 13:43:16 -08:00
|
|
|
private readonly _pageBindings = new Map<string, PageBinding>();
|
2022-04-02 18:02:27 -08:00
|
|
|
readonly initScripts: 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>();
|
2022-06-14 22:02:15 -07:00
|
|
|
readonly pdf: ((options: channels.PagePdfParams) => Promise<Buffer>) | undefined;
|
2020-02-13 17:39:14 -08:00
|
|
|
readonly coverage: any;
|
2022-07-01 12:49:43 -07:00
|
|
|
_clientRequestInterceptor: network.RouteHandler | undefined;
|
|
|
|
_serverRequestInterceptor: network.RouteHandler | undefined;
|
2020-02-11 12:06:58 -08:00
|
|
|
_ownedContext: BrowserContext | undefined;
|
2022-08-19 13:48:33 -07:00
|
|
|
selectors: Selectors;
|
2021-03-18 23:14:57 +08:00
|
|
|
_pageIsError: Error | undefined;
|
2021-03-31 10:38:05 -07:00
|
|
|
_video: Artifact | null = null;
|
2021-04-02 11:15:07 -07:00
|
|
|
_opener: Page | undefined;
|
2021-10-29 17:20:17 -08:00
|
|
|
private _frameThrottler = new FrameThrottler(10, 200);
|
2022-03-17 17:27:33 -08:00
|
|
|
private _isServerSideOnly = false;
|
2019-11-18 18:18:28 -08:00
|
|
|
|
2020-08-19 10:31:59 -07:00
|
|
|
constructor(delegate: PageDelegate, browserContext: BrowserContext) {
|
2021-04-20 23:03:56 -07:00
|
|
|
super(browserContext, 'page');
|
2021-02-09 09:00:00 -08:00
|
|
|
this.attribution.page = this;
|
2019-12-09 13:08:21 -08:00
|
|
|
this._delegate = delegate;
|
2019-12-05 14:11:48 -08:00
|
|
|
this._browserContext = browserContext;
|
2020-01-03 11:15:43 -08:00
|
|
|
this.accessibility = new accessibility.Accessibility(delegate.getAccessibilityTree.bind(delegate));
|
2020-08-18 19:13:40 -07:00
|
|
|
this.keyboard = new input.Keyboard(delegate.rawKeyboard, this);
|
|
|
|
this.mouse = new input.Mouse(delegate.rawMouse, this);
|
2020-10-19 10:07:33 -07:00
|
|
|
this.touchscreen = new input.Touchscreen(delegate.rawTouchscreen, this);
|
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;
|
2020-09-02 16:15:43 -07:00
|
|
|
this.selectors = browserContext.selectors();
|
2022-01-12 07:37:48 -08:00
|
|
|
this.instrumentation.onPageOpen(this);
|
2019-12-05 14:11:48 -08:00
|
|
|
}
|
|
|
|
|
2021-04-02 11:15:07 -07:00
|
|
|
async initOpener(opener: PageDelegate | null) {
|
|
|
|
if (!opener)
|
|
|
|
return;
|
|
|
|
const openerPage = await opener.pageOrError();
|
|
|
|
if (openerPage instanceof Page && !openerPage.isClosed())
|
|
|
|
this._opener = openerPage;
|
|
|
|
}
|
|
|
|
|
2022-06-22 17:23:51 +02:00
|
|
|
reportAsNew(error: Error | undefined = undefined, contextEvent: string = BrowserContext.Events.Page) {
|
2021-03-30 17:35:42 -07:00
|
|
|
if (error) {
|
2020-11-12 12:41:23 -08:00
|
|
|
// Initialization error could have happened because of
|
|
|
|
// context/browser closure. Just ignore the page.
|
|
|
|
if (this._browserContext.isClosingOrClosed())
|
|
|
|
return;
|
2021-03-30 17:35:42 -07:00
|
|
|
this._setIsError(error);
|
2020-11-12 12:41:23 -08:00
|
|
|
}
|
2021-05-13 10:29:14 -07:00
|
|
|
this._initialized = true;
|
2022-06-22 17:23:51 +02:00
|
|
|
this.emitOnContext(contextEvent, this);
|
2022-03-17 17:27:33 -08:00
|
|
|
// I may happen that page initialization finishes after Close event has already been sent,
|
2021-04-02 11:15:07 -07:00
|
|
|
// in that case we fire another Close event to ensure that each reported Page will have
|
|
|
|
// corresponding Close event after it is reported on the context.
|
|
|
|
if (this.isClosed())
|
|
|
|
this.emit(Page.Events.Close);
|
2020-11-12 12:41:23 -08:00
|
|
|
}
|
|
|
|
|
2021-05-13 10:29:14 -07:00
|
|
|
initializedOrUndefined() {
|
|
|
|
return this._initialized ? this : undefined;
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:27:33 -08:00
|
|
|
emitOnContext(event: string | symbol, ...args: any[]) {
|
|
|
|
if (this._isServerSideOnly)
|
|
|
|
return;
|
|
|
|
this._browserContext.emit(event, ...args);
|
|
|
|
}
|
|
|
|
|
2022-07-11 12:10:08 -08:00
|
|
|
async resetForReuse(metadata: CallMetadata) {
|
|
|
|
this.setDefaultNavigationTimeout(undefined);
|
|
|
|
this.setDefaultTimeout(undefined);
|
|
|
|
|
2022-07-12 13:30:24 -08:00
|
|
|
await this._removeExposedBindings();
|
|
|
|
await this._removeInitScripts();
|
|
|
|
await this.setClientRequestInterceptor(undefined);
|
2022-07-11 12:10:08 -08:00
|
|
|
await this._setServerRequestInterceptor(undefined);
|
|
|
|
await this.setFileChooserIntercepted(false);
|
2022-08-04 16:39:18 -07:00
|
|
|
// Re-navigate once init scripts are gone.
|
2022-07-11 12:10:08 -08:00
|
|
|
await this.mainFrame().goto(metadata, 'about:blank');
|
|
|
|
this._emulatedSize = undefined;
|
|
|
|
this._emulatedMedia = {};
|
|
|
|
this._extraHTTPHeaders = undefined;
|
|
|
|
this._interceptFileChooser = false;
|
|
|
|
|
2022-08-03 16:14:28 -07:00
|
|
|
await Promise.all([
|
|
|
|
this._delegate.updateEmulatedViewportSize(true),
|
|
|
|
this._delegate.updateEmulateMedia(),
|
|
|
|
this._delegate.updateFileChooserInterception(),
|
|
|
|
]);
|
2022-07-11 12:10:08 -08:00
|
|
|
}
|
|
|
|
|
2020-08-18 19:13:40 -07:00
|
|
|
async _doSlowMo() {
|
2021-01-29 16:00:56 -08:00
|
|
|
const slowMo = this._browserContext._browser.options.slowMo;
|
2020-08-18 19:13:40 -07:00
|
|
|
if (!slowMo)
|
|
|
|
return;
|
|
|
|
await new Promise(x => setTimeout(x, slowMo));
|
|
|
|
}
|
|
|
|
|
2019-12-05 14:11:48 -08:00
|
|
|
_didClose() {
|
2022-01-12 07:37:48 -08:00
|
|
|
this.instrumentation.onPageClose(this);
|
2020-07-10 16:38:01 -07:00
|
|
|
this._frameManager.dispose();
|
2021-10-29 17:20:17 -08:00
|
|
|
this._frameThrottler.setEnabled(false);
|
2020-06-29 16:26:32 -07:00
|
|
|
assert(this._closedState !== 'closed', 'Page closed twice');
|
|
|
|
this._closedState = 'closed';
|
2020-08-21 16:26:33 -07:00
|
|
|
this.emit(Page.Events.Close);
|
2021-08-29 11:21:06 -07:00
|
|
|
this._closedPromise.resolve();
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-01-03 11:10:10 -08:00
|
|
|
_didCrash() {
|
2022-01-12 07:37:48 -08:00
|
|
|
this.instrumentation.onPageClose(this);
|
2020-07-10 16:38:01 -07:00
|
|
|
this._frameManager.dispose();
|
2021-10-29 17:20:17 -08:00
|
|
|
this._frameThrottler.setEnabled(false);
|
2020-08-21 16:26:33 -07:00
|
|
|
this.emit(Page.Events.Crash);
|
2021-08-29 11:21:06 -07:00
|
|
|
this._crashedPromise.resolve(new Error('Page crashed'));
|
2020-01-03 11:10:10 -08:00
|
|
|
}
|
|
|
|
|
2019-12-06 13:36:47 -08:00
|
|
|
_didDisconnect() {
|
2022-01-12 07:37:48 -08:00
|
|
|
this.instrumentation.onPageClose(this);
|
2020-07-10 16:38:01 -07:00
|
|
|
this._frameManager.dispose();
|
2021-10-29 17:20:17 -08:00
|
|
|
this._frameThrottler.setEnabled(false);
|
2019-12-06 13:36:47 -08:00
|
|
|
assert(!this._disconnected, 'Page disconnected twice');
|
|
|
|
this._disconnected = true;
|
2021-08-29 11:21:06 -07:00
|
|
|
this._disconnectedPromise.resolve(new Error('Page closed'));
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-12-06 13:36:47 -08:00
|
|
|
async _onFileChooserOpened(handle: dom.ElementHandle) {
|
2021-02-05 11:30:44 -08:00
|
|
|
let multiple;
|
|
|
|
try {
|
|
|
|
multiple = await handle.evaluate(element => !!(element as HTMLInputElement).multiple);
|
|
|
|
} catch (e) {
|
|
|
|
// Frame/context may be gone during async processing. Do not throw.
|
|
|
|
return;
|
|
|
|
}
|
2020-08-21 16:26:33 -07:00
|
|
|
if (!this.listenerCount(Page.Events.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);
|
2020-08-21 16:26:33 -07:00
|
|
|
this.emit(Page.Events.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
|
|
|
}
|
|
|
|
|
2021-04-02 11:15:07 -07:00
|
|
|
opener(): Page | undefined {
|
|
|
|
return this._opener;
|
2020-01-31 18:38:45 -08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-10-28 07:31:30 -08:00
|
|
|
setDefaultNavigationTimeout(timeout: number | undefined) {
|
2019-11-18 18:18:28 -08:00
|
|
|
this._timeoutSettings.setDefaultNavigationTimeout(timeout);
|
|
|
|
}
|
|
|
|
|
2021-10-28 07:31:30 -08:00
|
|
|
setDefaultTimeout(timeout: number | undefined) {
|
2019-11-18 18:18:28 -08:00
|
|
|
this._timeoutSettings.setDefaultTimeout(timeout);
|
|
|
|
}
|
|
|
|
|
2021-09-08 14:27:05 -07:00
|
|
|
async exposeBinding(name: string, needsHandle: boolean, playwrightBinding: frames.FunctionWithSource) {
|
|
|
|
if (this._pageBindings.has(name))
|
2020-03-03 16:46:06 -08:00
|
|
|
throw new Error(`Function "${name}" has been already registered`);
|
2021-09-08 14:27:05 -07:00
|
|
|
if (this._browserContext._pageBindings.has(name))
|
2020-03-03 16:46:06 -08:00
|
|
|
throw new Error(`Function "${name}" has been already registered in the browser context`);
|
2021-09-08 14:27:05 -07:00
|
|
|
const binding = new PageBinding(name, playwrightBinding, needsHandle);
|
|
|
|
this._pageBindings.set(name, binding);
|
2020-03-03 16:46:06 -08:00
|
|
|
await this._delegate.exposeBinding(binding);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2022-07-12 13:30:24 -08:00
|
|
|
async _removeExposedBindings() {
|
2022-05-09 06:44:20 -08:00
|
|
|
for (const key of this._pageBindings.keys()) {
|
|
|
|
if (!key.startsWith('__pw'))
|
|
|
|
this._pageBindings.delete(key);
|
|
|
|
}
|
2022-04-04 11:39:43 -08:00
|
|
|
await this._delegate.removeExposedBindings();
|
|
|
|
}
|
|
|
|
|
2020-08-18 15:38:29 -07:00
|
|
|
setExtraHTTPHeaders(headers: types.HeadersArray) {
|
2022-07-07 15:28:20 -08:00
|
|
|
this._extraHTTPHeaders = headers;
|
2020-02-26 12:42:20 -08:00
|
|
|
return this._delegate.updateExtraHTTPHeaders();
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2022-07-07 15:28:20 -08:00
|
|
|
extraHTTPHeaders(): types.HeadersArray | undefined {
|
|
|
|
return this._extraHTTPHeaders;
|
|
|
|
}
|
|
|
|
|
2020-05-18 14:28:06 -07:00
|
|
|
async _onBindingCalled(payload: string, context: dom.FrameExecutionContext) {
|
2020-07-14 13:34:49 -07:00
|
|
|
if (this._disconnected || this._closedState === 'closed')
|
|
|
|
return;
|
2020-03-03 16:46:06 -08:00
|
|
|
await PageBinding.dispatch(this, payload, context);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-06-25 18:01:18 -07:00
|
|
|
_addConsoleMessage(type: string, args: js.JSHandle[], location: types.ConsoleMessageLocation, text?: string) {
|
2021-04-20 23:03:56 -07:00
|
|
|
const message = new ConsoleMessage(this, type, text, args, location);
|
2020-01-27 16:51:52 -08:00
|
|
|
const intercepted = this._frameManager.interceptConsoleMessage(message);
|
2020-08-21 16:26:33 -07:00
|
|
|
if (intercepted || !this.listenerCount(Page.Events.Console))
|
2019-11-18 18:18:28 -08:00
|
|
|
args.forEach(arg => arg.dispose());
|
2020-01-27 16:51:52 -08:00
|
|
|
else
|
2020-08-21 16:26:33 -07:00
|
|
|
this.emit(Page.Events.Console, message);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2021-02-09 14:44:48 -08:00
|
|
|
async reload(metadata: CallMetadata, options: types.NavigateOptions): Promise<network.Response | null> {
|
2021-02-25 10:00:54 -08:00
|
|
|
const controller = new ProgressController(metadata, this);
|
2022-06-17 21:17:30 -07:00
|
|
|
return controller.run(progress => this.mainFrame().raceNavigationAction(progress, options, async () => {
|
2021-01-22 15:58:53 -08:00
|
|
|
// Note: waitForNavigation may fail before we get response to reload(),
|
|
|
|
// so we should await it immediately.
|
|
|
|
const [response] = await Promise.all([
|
2022-08-12 13:48:47 -07:00
|
|
|
// Reload must be a new document, and should not be confused with a stray pushState.
|
|
|
|
this.mainFrame()._waitForNavigation(progress, true /* requiresNewDocument */, options),
|
2021-01-22 15:58:53 -08:00
|
|
|
this._delegate.reload(),
|
|
|
|
]);
|
2021-02-25 10:00:54 -08:00
|
|
|
await this._doSlowMo();
|
2021-01-22 15:58:53 -08:00
|
|
|
return response;
|
2021-02-25 10:00:54 -08:00
|
|
|
}), this._timeoutSettings.navigationTimeout(options));
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2021-02-09 14:44:48 -08:00
|
|
|
async goBack(metadata: CallMetadata, options: types.NavigateOptions): Promise<network.Response | null> {
|
2021-02-25 10:00:54 -08:00
|
|
|
const controller = new ProgressController(metadata, this);
|
2022-06-17 21:17:30 -07:00
|
|
|
return controller.run(progress => this.mainFrame().raceNavigationAction(progress, options, async () => {
|
2021-01-22 15:58:53 -08:00
|
|
|
// Note: waitForNavigation may fail before we get response to goBack,
|
|
|
|
// so we should catch it immediately.
|
|
|
|
let error: Error | undefined;
|
2022-08-12 13:48:47 -07:00
|
|
|
const waitPromise = this.mainFrame()._waitForNavigation(progress, false /* requiresNewDocument */, options).catch(e => {
|
2021-01-22 15:58:53 -08:00
|
|
|
error = e;
|
|
|
|
return null;
|
|
|
|
});
|
2020-09-14 16:43:17 -07:00
|
|
|
const result = await this._delegate.goBack();
|
2021-01-22 15:58:53 -08:00
|
|
|
if (!result)
|
2020-09-14 16:43:17 -07:00
|
|
|
return null;
|
2021-01-22 15:58:53 -08:00
|
|
|
const response = await waitPromise;
|
|
|
|
if (error)
|
|
|
|
throw error;
|
2021-02-25 10:00:54 -08:00
|
|
|
await this._doSlowMo();
|
2021-01-22 15:58:53 -08:00
|
|
|
return response;
|
2021-02-25 10:00:54 -08:00
|
|
|
}), this._timeoutSettings.navigationTimeout(options));
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2021-02-09 14:44:48 -08:00
|
|
|
async goForward(metadata: CallMetadata, options: types.NavigateOptions): Promise<network.Response | null> {
|
2021-02-25 10:00:54 -08:00
|
|
|
const controller = new ProgressController(metadata, this);
|
2022-06-17 21:17:30 -07:00
|
|
|
return controller.run(progress => this.mainFrame().raceNavigationAction(progress, options, async () => {
|
2021-01-22 15:58:53 -08:00
|
|
|
// Note: waitForNavigation may fail before we get response to goForward,
|
|
|
|
// so we should catch it immediately.
|
|
|
|
let error: Error | undefined;
|
2022-08-12 13:48:47 -07:00
|
|
|
const waitPromise = this.mainFrame()._waitForNavigation(progress, false /* requiresNewDocument */, options).catch(e => {
|
2021-01-22 15:58:53 -08:00
|
|
|
error = e;
|
|
|
|
return null;
|
|
|
|
});
|
2020-09-14 16:43:17 -07:00
|
|
|
const result = await this._delegate.goForward();
|
2021-01-22 15:58:53 -08:00
|
|
|
if (!result)
|
2020-09-14 16:43:17 -07:00
|
|
|
return null;
|
2021-01-22 15:58:53 -08:00
|
|
|
const response = await waitPromise;
|
|
|
|
if (error)
|
|
|
|
throw error;
|
2021-02-25 10:00:54 -08:00
|
|
|
await this._doSlowMo();
|
2021-01-22 15:58:53 -08:00
|
|
|
return response;
|
2021-02-25 10:00:54 -08:00
|
|
|
}), this._timeoutSettings.navigationTimeout(options));
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2022-07-07 15:28:20 -08:00
|
|
|
async emulateMedia(options: Partial<EmulatedMedia>) {
|
2020-01-05 14:39:16 -08:00
|
|
|
if (options.media !== undefined)
|
2022-07-07 15:28:20 -08:00
|
|
|
this._emulatedMedia.media = options.media;
|
2019-12-09 13:08:21 -08:00
|
|
|
if (options.colorScheme !== undefined)
|
2022-07-07 15:28:20 -08:00
|
|
|
this._emulatedMedia.colorScheme = options.colorScheme;
|
2021-05-22 01:56:09 +02:00
|
|
|
if (options.reducedMotion !== undefined)
|
2022-07-07 15:28:20 -08:00
|
|
|
this._emulatedMedia.reducedMotion = options.reducedMotion;
|
2021-09-03 21:48:06 +02:00
|
|
|
if (options.forcedColors !== undefined)
|
2022-07-07 15:28:20 -08:00
|
|
|
this._emulatedMedia.forcedColors = options.forcedColors;
|
2022-07-12 13:30:24 -08:00
|
|
|
|
2020-04-06 19:49:33 -07:00
|
|
|
await this._delegate.updateEmulateMedia();
|
2020-08-18 19:13:40 -07:00
|
|
|
await this._doSlowMo();
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2022-07-07 15:28:20 -08:00
|
|
|
emulatedMedia(): EmulatedMedia {
|
|
|
|
const contextOptions = this._browserContext._options;
|
|
|
|
return {
|
|
|
|
media: this._emulatedMedia.media || null,
|
|
|
|
colorScheme: this._emulatedMedia.colorScheme !== undefined ? this._emulatedMedia.colorScheme : contextOptions.colorScheme ?? 'light',
|
|
|
|
reducedMotion: this._emulatedMedia.reducedMotion !== undefined ? this._emulatedMedia.reducedMotion : contextOptions.reducedMotion ?? 'no-preference',
|
|
|
|
forcedColors: this._emulatedMedia.forcedColors !== undefined ? this._emulatedMedia.forcedColors : contextOptions.forcedColors ?? 'none',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-02-06 19:02:55 -08:00
|
|
|
async setViewportSize(viewportSize: types.Size) {
|
2022-07-07 15:28:20 -08:00
|
|
|
this._emulatedSize = { viewport: { ...viewportSize }, screen: { ...viewportSize } };
|
|
|
|
await this._delegate.updateEmulatedViewportSize();
|
2020-08-18 19:13:40 -07:00
|
|
|
await this._doSlowMo();
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-02-06 19:02:55 -08:00
|
|
|
viewportSize(): types.Size | null {
|
2022-07-07 15:28:20 -08:00
|
|
|
return this.emulatedSize()?.viewport || null;
|
|
|
|
}
|
|
|
|
|
|
|
|
emulatedSize(): EmulatedSize | null {
|
|
|
|
if (this._emulatedSize)
|
|
|
|
return this._emulatedSize;
|
|
|
|
const contextOptions = this._browserContext._options;
|
|
|
|
return contextOptions.viewport ? { viewport: contextOptions.viewport, screen: contextOptions.screen || contextOptions.viewport } : null;
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-07-21 09:36:54 -07:00
|
|
|
async bringToFront(): Promise<void> {
|
|
|
|
await this._delegate.bringToFront();
|
|
|
|
}
|
|
|
|
|
2022-04-02 18:02:27 -08:00
|
|
|
async addInitScript(source: string) {
|
|
|
|
this.initScripts.push(source);
|
|
|
|
await this._delegate.addInitScript(source);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2022-07-12 13:30:24 -08:00
|
|
|
async _removeInitScripts() {
|
2022-04-04 11:39:43 -08:00
|
|
|
this.initScripts.splice(0, this.initScripts.length);
|
|
|
|
await this._delegate.removeInitScripts();
|
|
|
|
}
|
|
|
|
|
2022-07-01 12:49:43 -07:00
|
|
|
needsRequestInterception(): boolean {
|
2020-11-13 14:24:53 -08:00
|
|
|
return !!this._clientRequestInterceptor || !!this._serverRequestInterceptor || !!this._browserContext._requestInterceptor;
|
2020-03-09 21:02:54 -07:00
|
|
|
}
|
|
|
|
|
2022-04-02 18:02:27 -08:00
|
|
|
async setClientRequestInterceptor(handler: network.RouteHandler | undefined): Promise<void> {
|
2020-11-13 14:24:53 -08:00
|
|
|
this._clientRequestInterceptor = handler;
|
|
|
|
await this._delegate.updateRequestInterception();
|
|
|
|
}
|
|
|
|
|
|
|
|
async _setServerRequestInterceptor(handler: network.RouteHandler | undefined): Promise<void> {
|
|
|
|
this._serverRequestInterceptor = handler;
|
2020-04-15 19:55:22 -07:00
|
|
|
await this._delegate.updateRequestInterception();
|
|
|
|
}
|
|
|
|
|
2022-02-28 13:25:59 -07:00
|
|
|
async expectScreenshot(metadata: CallMetadata, options: ExpectScreenshotOptions = {}): Promise<{ actual?: Buffer, previous?: Buffer, diff?: Buffer, errorMessage?: string, log?: string[] }> {
|
|
|
|
const locator = options.locator;
|
|
|
|
const rafrafScreenshot = locator ? async (progress: Progress, timeout: number) => {
|
|
|
|
return await locator.frame.rafrafTimeoutScreenshotElementWithProgress(progress, locator.selector, timeout, options.screenshotOptions || {});
|
|
|
|
} : async (progress: Progress, timeout: number) => {
|
|
|
|
await this.mainFrame().rafrafTimeout(timeout);
|
|
|
|
return await this._screenshotter.screenshotPage(progress, options.screenshotOptions || {});
|
|
|
|
};
|
|
|
|
|
2022-03-21 17:42:21 -06:00
|
|
|
const comparator = getComparator('image/png');
|
2022-02-28 13:25:59 -07:00
|
|
|
const controller = new ProgressController(metadata, this);
|
2022-03-21 16:10:33 -06:00
|
|
|
if (!options.expected && options.isNot)
|
2022-02-28 13:25:59 -07:00
|
|
|
return { errorMessage: '"not" matcher requires expected result' };
|
2022-04-19 08:43:18 -06:00
|
|
|
try {
|
|
|
|
const format = validateScreenshotOptions(options.screenshotOptions || {});
|
|
|
|
if (format !== 'png')
|
|
|
|
throw new Error('Only PNG screenshots are supported');
|
|
|
|
} catch (error) {
|
|
|
|
return { errorMessage: error.message };
|
|
|
|
}
|
2022-02-28 13:25:59 -07:00
|
|
|
let intermediateResult: {
|
|
|
|
actual?: Buffer,
|
|
|
|
previous?: Buffer,
|
2022-03-21 16:10:33 -06:00
|
|
|
errorMessage: string,
|
2022-02-28 13:25:59 -07:00
|
|
|
diff?: Buffer,
|
|
|
|
} | undefined = undefined;
|
2022-03-21 16:10:33 -06:00
|
|
|
const areEqualScreenshots = (actual: Buffer | undefined, expected: Buffer | undefined, previous: Buffer | undefined) => {
|
|
|
|
const comparatorResult = actual && expected ? comparator(actual, expected, options.comparatorOptions) : undefined;
|
|
|
|
if (comparatorResult !== undefined && !!comparatorResult === !!options.isNot)
|
|
|
|
return true;
|
|
|
|
if (comparatorResult)
|
|
|
|
intermediateResult = { errorMessage: comparatorResult.errorMessage, diff: comparatorResult.diff, actual, previous };
|
|
|
|
return false;
|
|
|
|
};
|
2022-03-11 23:40:28 -07:00
|
|
|
const callTimeout = this._timeoutSettings.timeout(options);
|
2022-02-28 13:25:59 -07:00
|
|
|
return controller.run(async progress => {
|
|
|
|
let actual: Buffer | undefined;
|
|
|
|
let previous: Buffer | undefined;
|
2022-03-04 19:17:57 -07:00
|
|
|
const pollIntervals = [0, 100, 250, 500];
|
2022-03-11 23:40:28 -07:00
|
|
|
progress.log(`${metadata.apiName}${callTimeout ? ` with timeout ${callTimeout}ms` : ''}`);
|
2022-03-21 16:10:33 -06:00
|
|
|
if (options.expected)
|
|
|
|
progress.log(` verifying given screenshot expectation`);
|
2022-03-11 23:40:28 -07:00
|
|
|
else
|
2022-03-21 16:10:33 -06:00
|
|
|
progress.log(` generating new stable screenshot expectation`);
|
|
|
|
let isFirstIteration = true;
|
2022-02-28 13:25:59 -07:00
|
|
|
while (true) {
|
|
|
|
progress.throwIfAborted();
|
|
|
|
if (this.isClosed())
|
|
|
|
throw new Error('The page has closed');
|
2022-03-08 10:30:14 -07:00
|
|
|
const screenshotTimeout = pollIntervals.shift() ?? 1000;
|
2022-03-11 23:40:28 -07:00
|
|
|
if (screenshotTimeout)
|
|
|
|
progress.log(`waiting ${screenshotTimeout}ms before taking screenshot`);
|
2022-03-21 16:10:33 -06:00
|
|
|
previous = actual;
|
|
|
|
actual = await rafrafScreenshot(progress, screenshotTimeout).catch(e => {
|
|
|
|
progress.log(`failed to take screenshot - ` + e.message);
|
|
|
|
return undefined;
|
|
|
|
});
|
|
|
|
if (!actual)
|
|
|
|
continue;
|
|
|
|
// Compare against expectation for the first iteration.
|
|
|
|
const expectation = options.expected && isFirstIteration ? options.expected : previous;
|
|
|
|
if (areEqualScreenshots(actual, expectation, previous))
|
2022-02-28 13:25:59 -07:00
|
|
|
break;
|
2022-03-21 16:10:33 -06:00
|
|
|
if (intermediateResult)
|
|
|
|
progress.log(intermediateResult.errorMessage);
|
|
|
|
isFirstIteration = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isFirstIteration)
|
|
|
|
progress.log(`captured a stable screenshot`);
|
|
|
|
|
|
|
|
if (!options.expected)
|
|
|
|
return { actual };
|
|
|
|
|
|
|
|
if (isFirstIteration) {
|
|
|
|
progress.log(`screenshot matched expectation`);
|
|
|
|
return {};
|
2022-02-28 13:25:59 -07:00
|
|
|
}
|
|
|
|
|
2022-03-21 16:10:33 -06:00
|
|
|
if (areEqualScreenshots(actual, options.expected, previous)) {
|
|
|
|
progress.log(`screenshot matched expectation`);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
throw new Error(intermediateResult!.errorMessage);
|
2022-03-11 23:40:28 -07:00
|
|
|
}, callTimeout).catch(e => {
|
2022-02-28 13:25:59 -07:00
|
|
|
// Q: Why not throw upon isSessionClosedError(e) as in other places?
|
|
|
|
// A: We want user to receive a friendly diff between actual and expected/previous.
|
|
|
|
if (js.isJavaScriptErrorInEvaluate(e) || isInvalidSelectorError(e))
|
|
|
|
throw e;
|
|
|
|
return {
|
2022-03-11 23:40:28 -07:00
|
|
|
log: e.message ? [...metadata.log, e.message] : metadata.log,
|
2022-02-28 13:25:59 -07:00
|
|
|
...intermediateResult,
|
2022-04-19 08:43:18 -06:00
|
|
|
errorMessage: e.message,
|
2022-02-28 13:25:59 -07:00
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async screenshot(metadata: CallMetadata, options: ScreenshotOptions & TimeoutOptions = {}): Promise<Buffer> {
|
2021-02-09 14:44:48 -08:00
|
|
|
const controller = new ProgressController(metadata, this);
|
|
|
|
return controller.run(
|
2020-06-25 16:57:21 -07:00
|
|
|
progress => this._screenshotter.screenshotPage(progress, options),
|
2020-08-14 18:25:32 -07:00
|
|
|
this._timeoutSettings.timeout(options));
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2021-02-19 09:33:24 -08:00
|
|
|
async close(metadata: CallMetadata, options?: { runBeforeUnload?: boolean }) {
|
2020-06-29 16:26:32 -07:00
|
|
|
if (this._closedState === 'closed')
|
2019-12-18 16:23:05 -08:00
|
|
|
return;
|
2020-06-25 08:30:56 -07:00
|
|
|
const runBeforeUnload = !!options && !!options.runBeforeUnload;
|
2020-06-29 16:26:32 -07:00
|
|
|
if (this._closedState !== 'closing') {
|
2020-07-01 16:05:56 -07:00
|
|
|
this._closedState = 'closing';
|
2021-08-26 18:44:49 -07:00
|
|
|
assert(!this._disconnected, 'Target closed');
|
2020-11-16 10:26:34 -08:00
|
|
|
// This might throw if the browser context containing the page closes
|
|
|
|
// while we are trying to close the page.
|
|
|
|
await this._delegate.closePage(runBeforeUnload).catch(e => debugLogger.log('error', e));
|
2020-06-29 16:26:32 -07:00
|
|
|
}
|
2019-12-09 13:08:21 -08:00
|
|
|
if (!runBeforeUnload)
|
2019-12-05 14:11:48 -08:00
|
|
|
await this._closedPromise;
|
2020-02-11 12:06:58 -08:00
|
|
|
if (this._ownedContext)
|
2021-02-19 09:33:24 -08:00
|
|
|
await this._ownedContext.close(metadata);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2021-03-18 23:14:57 +08:00
|
|
|
private _setIsError(error: Error) {
|
|
|
|
this._pageIsError = error;
|
2022-05-04 20:52:50 +01:00
|
|
|
this._frameManager.createDummyMainFrameIfNeeded();
|
2020-07-10 13:15:39 -07:00
|
|
|
}
|
|
|
|
|
2019-11-18 18:18:28 -08:00
|
|
|
isClosed(): boolean {
|
2020-06-29 16:26:32 -07:00
|
|
|
return this._closedState === 'closed';
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-01-07 12:59:01 -08:00
|
|
|
_addWorker(workerId: string, worker: Worker) {
|
|
|
|
this._workers.set(workerId, worker);
|
2020-08-21 16:26:33 -07:00
|
|
|
this.emit(Page.Events.Worker, worker);
|
2020-01-07 12:59:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
_removeWorker(workerId: string) {
|
|
|
|
const worker = this._workers.get(workerId);
|
|
|
|
if (!worker)
|
|
|
|
return;
|
2021-11-03 10:44:50 -07:00
|
|
|
worker.didClose();
|
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) {
|
2021-11-03 10:44:50 -07:00
|
|
|
worker.didClose();
|
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
|
|
|
|
2022-04-02 18:02:27 -08:00
|
|
|
async setFileChooserIntercepted(enabled: boolean): Promise<void> {
|
2022-07-07 15:28:20 -08:00
|
|
|
this._interceptFileChooser = enabled;
|
2022-07-11 12:10:08 -08:00
|
|
|
await this._delegate.updateFileChooserInterception();
|
2020-01-30 17:43:06 -08:00
|
|
|
}
|
2020-10-19 14:35:18 -07:00
|
|
|
|
2022-07-07 15:28:20 -08:00
|
|
|
fileChooserIntercepted() {
|
|
|
|
return this._interceptFileChooser;
|
|
|
|
}
|
|
|
|
|
2021-01-13 14:25:42 -08:00
|
|
|
frameNavigatedToNewDocument(frame: frames.Frame) {
|
|
|
|
this.emit(Page.Events.InternalFrameNavigatedToNewDocument, frame);
|
2020-11-13 14:24:53 -08:00
|
|
|
const url = frame.url();
|
|
|
|
if (!url.startsWith('http'))
|
|
|
|
return;
|
2021-01-25 14:49:51 -08:00
|
|
|
const purl = network.parsedURL(url);
|
|
|
|
if (purl)
|
|
|
|
this._browserContext.addVisitedOrigin(purl.origin);
|
2020-11-13 14:24:53 -08:00
|
|
|
}
|
2020-12-02 13:43:16 -08:00
|
|
|
|
|
|
|
allBindings() {
|
|
|
|
return [...this._browserContext._pageBindings.values(), ...this._pageBindings.values()];
|
|
|
|
}
|
|
|
|
|
2021-09-08 14:27:05 -07:00
|
|
|
getBinding(name: string) {
|
|
|
|
return this._pageBindings.get(name) || this._browserContext._pageBindings.get(name);
|
2020-12-02 13:43:16 -08:00
|
|
|
}
|
2021-04-08 05:32:12 +08:00
|
|
|
|
2021-05-11 13:21:01 -07:00
|
|
|
setScreencastOptions(options: { width: number, height: number, quality: number } | null) {
|
|
|
|
this._delegate.setScreencastOptions(options).catch(e => debugLogger.log('error', e));
|
2021-10-29 17:20:17 -08:00
|
|
|
this._frameThrottler.setEnabled(!!options);
|
|
|
|
}
|
|
|
|
|
|
|
|
throttleScreencastFrameAck(ack: () => void) {
|
|
|
|
// Don't ack immediately, tracing has smart throttling logic that is implemented here.
|
|
|
|
this._frameThrottler.ack(ack);
|
|
|
|
}
|
|
|
|
|
|
|
|
temporarlyDisableTracingScreencastThrottling() {
|
|
|
|
this._frameThrottler.recharge();
|
2021-04-08 05:32:12 +08:00
|
|
|
}
|
2021-07-01 15:26:55 -07:00
|
|
|
|
|
|
|
firePageError(error: Error) {
|
|
|
|
this.emit(Page.Events.PageError, error);
|
|
|
|
}
|
2021-08-18 12:51:45 -07:00
|
|
|
|
2021-11-04 12:28:35 -08:00
|
|
|
parseSelector(selector: string | ParsedSelector, options?: types.StrictOptions): SelectorInfo {
|
2021-08-18 12:51:45 -07:00
|
|
|
const strict = typeof options?.strict === 'boolean' ? options.strict : !!this.context()._options.strictSelectors;
|
|
|
|
return this.selectors.parseSelector(selector, strict);
|
|
|
|
}
|
2022-01-12 07:37:48 -08:00
|
|
|
|
|
|
|
async hideHighlight() {
|
|
|
|
await Promise.all(this.frames().map(frame => frame.hideHighlight().catch(() => {})));
|
|
|
|
}
|
2022-03-17 17:27:33 -08:00
|
|
|
|
|
|
|
markAsServerSideOnly() {
|
|
|
|
this._isServerSideOnly = true;
|
|
|
|
}
|
2020-01-07 12:59:01 -08:00
|
|
|
}
|
|
|
|
|
2021-02-09 09:00:00 -08:00
|
|
|
export class Worker extends SdkObject {
|
2020-08-21 16:26:33 -07:00
|
|
|
static Events = {
|
|
|
|
Close: 'close',
|
|
|
|
};
|
|
|
|
|
2020-01-07 12:59:01 -08:00
|
|
|
private _url: string;
|
|
|
|
private _executionContextPromise: Promise<js.ExecutionContext>;
|
2021-02-03 13:49:25 -08:00
|
|
|
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
|
|
|
|
2021-02-09 09:00:00 -08:00
|
|
|
constructor(parent: SdkObject, url: string) {
|
2021-04-20 23:03:56 -07:00
|
|
|
super(parent, 'worker');
|
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) {
|
2021-02-09 09:00:00 -08:00
|
|
|
this._existingExecutionContext = new js.ExecutionContext(this, delegate);
|
2020-01-07 12:59:01 -08:00
|
|
|
this._executionContextCallback(this._existingExecutionContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
url(): string {
|
|
|
|
return this._url;
|
|
|
|
}
|
|
|
|
|
2021-11-03 10:44:50 -07:00
|
|
|
didClose() {
|
|
|
|
if (this._existingExecutionContext)
|
|
|
|
this._existingExecutionContext.contextDestroyed(new Error('Worker was closed'));
|
|
|
|
this.emit(Worker.Events.Close, this);
|
|
|
|
}
|
|
|
|
|
2021-03-18 03:03:21 +08:00
|
|
|
async evaluateExpression(expression: string, isFunction: boolean | undefined, arg: any): Promise<any> {
|
2021-07-09 16:19:42 +02:00
|
|
|
return js.evaluateExpression(await this._executionContextPromise, true /* returnByValue */, expression, isFunction, arg);
|
2020-06-30 10:55:11 -07:00
|
|
|
}
|
|
|
|
|
2021-03-18 03:03:21 +08:00
|
|
|
async evaluateExpressionHandle(expression: string, isFunction: boolean | undefined, arg: any): Promise<any> {
|
2021-07-09 16:19:42 +02:00
|
|
|
return js.evaluateExpression(await this._executionContextPromise, false /* returnByValue */, expression, isFunction, arg);
|
2020-06-30 10:55:11 -07:00
|
|
|
}
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
2020-03-03 16:46:06 -08:00
|
|
|
|
2022-05-09 14:07:04 -08:00
|
|
|
type BindingPayload = {
|
|
|
|
name: string;
|
|
|
|
seq: number;
|
|
|
|
serializedArgs?: SerializedValue[],
|
|
|
|
};
|
|
|
|
|
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-10-01 22:47:31 -07:00
|
|
|
readonly needsHandle: boolean;
|
2020-03-03 16:46:06 -08:00
|
|
|
|
2021-09-08 14:27:05 -07:00
|
|
|
constructor(name: string, playwrightFunction: frames.FunctionWithSource, needsHandle: boolean) {
|
2020-03-03 16:46:06 -08:00
|
|
|
this.name = name;
|
|
|
|
this.playwrightFunction = playwrightFunction;
|
2022-05-09 17:51:53 -08:00
|
|
|
this.source = `(${addPageBinding.toString()})(${JSON.stringify(name)}, ${needsHandle}, (${source})())`;
|
2020-10-01 22:47:31 -07:00
|
|
|
this.needsHandle = needsHandle;
|
2020-03-03 16:46:06 -08:00
|
|
|
}
|
|
|
|
|
2020-05-18 14:28:06 -07:00
|
|
|
static async dispatch(page: Page, payload: string, context: dom.FrameExecutionContext) {
|
2022-05-09 14:07:04 -08:00
|
|
|
const { name, seq, serializedArgs } = JSON.parse(payload) as BindingPayload;
|
2020-03-03 16:46:06 -08:00
|
|
|
try {
|
2020-12-02 13:43:16 -08:00
|
|
|
assert(context.world);
|
2021-09-08 14:27:05 -07:00
|
|
|
const binding = page.getBinding(name)!;
|
2020-10-01 22:47:31 -07:00
|
|
|
let result: any;
|
2020-12-02 13:43:16 -08:00
|
|
|
if (binding.needsHandle) {
|
2021-03-18 01:47:07 +08:00
|
|
|
const handle = await context.evaluateHandle(takeHandle, { name, seq }).catch(e => null);
|
2020-12-02 13:43:16 -08:00
|
|
|
result = await binding.playwrightFunction({ frame: context.frame, page, context: page._browserContext }, handle);
|
2020-10-01 22:47:31 -07:00
|
|
|
} else {
|
2022-05-09 17:51:53 -08:00
|
|
|
const args = serializedArgs!.map(a => parseEvaluationResultValue(a));
|
2020-12-02 13:43:16 -08:00
|
|
|
result = await binding.playwrightFunction({ frame: context.frame, page, context: page._browserContext }, ...args);
|
2020-10-01 22:47:31 -07:00
|
|
|
}
|
2021-03-18 01:47:07 +08:00
|
|
|
context.evaluate(deliverResult, { name, seq, result }).catch(e => debugLogger.log('error', e));
|
2020-03-03 16:46:06 -08:00
|
|
|
} catch (error) {
|
2020-08-22 07:07:13 -07:00
|
|
|
if (isError(error))
|
2021-03-18 01:47:07 +08:00
|
|
|
context.evaluate(deliverError, { name, seq, message: error.message, stack: error.stack }).catch(e => debugLogger.log('error', e));
|
2020-03-03 16:46:06 -08:00
|
|
|
else
|
2021-03-18 01:47:07 +08:00
|
|
|
context.evaluate(deliverErrorValue, { name, seq, error }).catch(e => debugLogger.log('error', e));
|
2020-03-03 16:46:06 -08:00
|
|
|
}
|
|
|
|
|
2020-10-01 22:47:31 -07:00
|
|
|
function takeHandle(arg: { name: string, seq: number }) {
|
2021-05-12 22:19:27 +00:00
|
|
|
const handle = (globalThis as any)[arg.name]['handles'].get(arg.seq);
|
|
|
|
(globalThis as any)[arg.name]['handles'].delete(arg.seq);
|
2020-10-01 22:47:31 -07:00
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2020-07-15 20:05:11 -07:00
|
|
|
function deliverResult(arg: { name: string, seq: number, result: any }) {
|
2021-05-12 22:19:27 +00:00
|
|
|
(globalThis as any)[arg.name]['callbacks'].get(arg.seq).resolve(arg.result);
|
|
|
|
(globalThis as any)[arg.name]['callbacks'].delete(arg.seq);
|
2020-03-03 16:46:06 -08:00
|
|
|
}
|
|
|
|
|
2020-07-15 20:05:11 -07:00
|
|
|
function deliverError(arg: { name: string, seq: number, message: string, stack: string | undefined }) {
|
|
|
|
const error = new Error(arg.message);
|
|
|
|
error.stack = arg.stack;
|
2021-05-12 22:19:27 +00:00
|
|
|
(globalThis as any)[arg.name]['callbacks'].get(arg.seq).reject(error);
|
|
|
|
(globalThis as any)[arg.name]['callbacks'].delete(arg.seq);
|
2020-03-03 16:46:06 -08:00
|
|
|
}
|
|
|
|
|
2020-07-15 20:05:11 -07:00
|
|
|
function deliverErrorValue(arg: { name: string, seq: number, error: any }) {
|
2021-05-12 22:19:27 +00:00
|
|
|
(globalThis as any)[arg.name]['callbacks'].get(arg.seq).reject(arg.error);
|
|
|
|
(globalThis as any)[arg.name]['callbacks'].delete(arg.seq);
|
2020-03-03 16:46:06 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-09 14:07:04 -08:00
|
|
|
function addPageBinding(bindingName: string, needsHandle: boolean, utilityScriptSerializers: ReturnType<typeof source>) {
|
2021-05-12 22:19:27 +00:00
|
|
|
const binding = (globalThis as any)[bindingName];
|
2020-03-10 16:19:01 -07:00
|
|
|
if (binding.__installed)
|
|
|
|
return;
|
2021-05-12 22:19:27 +00:00
|
|
|
(globalThis as any)[bindingName] = (...args: any[]) => {
|
|
|
|
const me = (globalThis as any)[bindingName];
|
2020-10-01 22:47:31 -07:00
|
|
|
if (needsHandle && args.slice(1).some(arg => arg !== undefined))
|
|
|
|
throw new Error(`exposeBindingHandle supports a single argument, ${args.length} received`);
|
2020-03-03 16:46:06 -08:00
|
|
|
let callbacks = me['callbacks'];
|
|
|
|
if (!callbacks) {
|
|
|
|
callbacks = new Map();
|
|
|
|
me['callbacks'] = callbacks;
|
|
|
|
}
|
2022-05-09 14:07:04 -08:00
|
|
|
const seq: number = (me['lastSeq'] || 0) + 1;
|
2020-03-03 16:46:06 -08:00
|
|
|
me['lastSeq'] = seq;
|
2020-10-01 22:47:31 -07:00
|
|
|
let handles = me['handles'];
|
|
|
|
if (!handles) {
|
|
|
|
handles = new Map();
|
|
|
|
me['handles'] = handles;
|
|
|
|
}
|
2021-09-27 18:58:08 +02:00
|
|
|
const promise = new Promise((resolve, reject) => callbacks.set(seq, { resolve, reject }));
|
2022-05-09 14:07:04 -08:00
|
|
|
let payload: BindingPayload;
|
2020-10-01 22:47:31 -07:00
|
|
|
if (needsHandle) {
|
|
|
|
handles.set(seq, args[0]);
|
2022-05-09 14:07:04 -08:00
|
|
|
payload = { name: bindingName, seq };
|
2020-10-01 22:47:31 -07:00
|
|
|
} else {
|
2022-05-09 14:07:04 -08:00
|
|
|
const serializedArgs = args.map(a => utilityScriptSerializers.serializeAsCallArgument(a, v => {
|
|
|
|
return { fallThrough: v };
|
|
|
|
}));
|
|
|
|
payload = { name: bindingName, seq, serializedArgs };
|
2020-10-01 22:47:31 -07:00
|
|
|
}
|
2022-05-09 14:07:04 -08:00
|
|
|
binding(JSON.stringify(payload));
|
2020-03-03 16:46:06 -08:00
|
|
|
return promise;
|
|
|
|
};
|
2021-05-12 22:19:27 +00:00
|
|
|
(globalThis as any)[bindingName].__installed = true;
|
2020-03-03 16:46:06 -08:00
|
|
|
}
|
2021-10-29 17:20:17 -08:00
|
|
|
|
|
|
|
class FrameThrottler {
|
|
|
|
private _acks: (() => void)[] = [];
|
|
|
|
private _interval: number;
|
|
|
|
private _nonThrottledFrames: number;
|
|
|
|
private _budget: number;
|
|
|
|
private _intervalId: NodeJS.Timeout | undefined;
|
|
|
|
|
|
|
|
constructor(nonThrottledFrames: number, interval: number) {
|
|
|
|
this._nonThrottledFrames = nonThrottledFrames;
|
|
|
|
this._budget = nonThrottledFrames;
|
|
|
|
this._interval = interval;
|
|
|
|
}
|
|
|
|
|
|
|
|
setEnabled(enabled: boolean) {
|
|
|
|
if (enabled) {
|
|
|
|
if (this._intervalId)
|
|
|
|
clearInterval(this._intervalId);
|
|
|
|
this._intervalId = setInterval(() => this._tick(), this._interval);
|
|
|
|
} else if (this._intervalId) {
|
|
|
|
clearInterval(this._intervalId);
|
|
|
|
this._intervalId = undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
recharge() {
|
|
|
|
// Send all acks, reset budget.
|
|
|
|
for (const ack of this._acks)
|
|
|
|
ack();
|
|
|
|
this._acks = [];
|
|
|
|
this._budget = this._nonThrottledFrames;
|
|
|
|
}
|
|
|
|
|
|
|
|
ack(ack: () => void) {
|
|
|
|
// Either not engaged or video is also recording, don't throttle.
|
|
|
|
if (!this._intervalId) {
|
|
|
|
ack();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do we have enough budget to respond w/o throttling?
|
|
|
|
if (--this._budget > 0) {
|
|
|
|
ack();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Schedule.
|
|
|
|
this._acks.push(ack);
|
|
|
|
}
|
|
|
|
|
|
|
|
private _tick() {
|
|
|
|
this._acks.shift()?.();
|
|
|
|
}
|
|
|
|
}
|