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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { EventEmitter } from 'events';
|
2019-12-06 11:33:24 -08:00
|
|
|
import * as console from '../console';
|
|
|
|
import * as dom from '../dom';
|
|
|
|
import * as frames from '../frames';
|
2019-11-19 16:29:28 -08:00
|
|
|
import { assert, debugError, helper } from '../helper';
|
2019-12-06 11:33:24 -08:00
|
|
|
import * as input from '../input';
|
|
|
|
import { ClickOptions, mediaColorSchemes, mediaTypes, MultiClickOptions, PointerActionOptions, SelectOption } from '../input';
|
|
|
|
import * as js from '../javascript';
|
|
|
|
import * as network from '../network';
|
|
|
|
import { Screenshotter } from '../screenshotter';
|
2019-11-19 16:29:28 -08:00
|
|
|
import { TimeoutSettings } from '../TimeoutSettings';
|
2019-12-06 11:33:24 -08:00
|
|
|
import * as types from '../types';
|
2019-11-19 16:29:28 -08:00
|
|
|
import { Browser } from './Browser';
|
|
|
|
import { BrowserContext } from './BrowserContext';
|
2019-12-06 13:36:47 -08:00
|
|
|
import { CDPSession } from './Connection';
|
2019-11-22 14:46:34 -08:00
|
|
|
import { Events } from './events';
|
|
|
|
import { Accessibility } from './features/accessibility';
|
|
|
|
import { Coverage } from './features/coverage';
|
|
|
|
import { Interception } from './features/interception';
|
2019-12-06 11:33:24 -08:00
|
|
|
import { Overrides } from './features/overrides';
|
2019-11-19 16:29:28 -08:00
|
|
|
import { PDF } from './features/pdf';
|
2019-11-22 14:46:34 -08:00
|
|
|
import { Workers } from './features/workers';
|
2019-11-18 18:18:28 -08:00
|
|
|
import { FrameManager, FrameManagerEvents } from './FrameManager';
|
2019-12-06 11:33:24 -08:00
|
|
|
import { RawKeyboardImpl, RawMouseImpl } from './Input';
|
2019-11-27 16:03:51 -08:00
|
|
|
import { NetworkManagerEvents } from './NetworkManager';
|
2019-12-06 11:33:24 -08:00
|
|
|
import { CRScreenshotDelegate } from './Screenshotter';
|
2019-12-06 16:34:27 -08:00
|
|
|
import { Protocol } from './protocol';
|
2019-11-18 18:18:28 -08:00
|
|
|
|
|
|
|
export class Page extends EventEmitter {
|
|
|
|
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;
|
|
|
|
private _disconnectedPromise: Promise<Error>;
|
2019-11-21 16:54:10 -08:00
|
|
|
_client: CDPSession;
|
2019-12-05 14:11:48 -08:00
|
|
|
private _browserContext: BrowserContext;
|
2019-12-06 13:36:47 -08:00
|
|
|
readonly keyboard: input.Keyboard;
|
|
|
|
readonly mouse: input.Mouse;
|
2019-11-18 18:18:28 -08:00
|
|
|
private _timeoutSettings: TimeoutSettings;
|
2019-12-09 10:16:30 -08:00
|
|
|
_frameManager: FrameManager;
|
2019-11-19 16:29:28 -08:00
|
|
|
readonly accessibility: Accessibility;
|
|
|
|
readonly coverage: Coverage;
|
2019-11-26 15:13:49 -08:00
|
|
|
readonly overrides: Overrides;
|
2019-11-21 14:41:38 -08:00
|
|
|
readonly interception: Interception;
|
2019-11-19 16:29:28 -08:00
|
|
|
readonly pdf: PDF;
|
2019-11-19 17:32:43 -08:00
|
|
|
readonly workers: Workers;
|
2019-11-18 18:18:28 -08:00
|
|
|
private _pageBindings = new Map<string, Function>();
|
|
|
|
_javascriptEnabled = true;
|
2019-12-06 11:33:24 -08:00
|
|
|
private _viewport: types.Viewport | null = null;
|
2019-12-02 10:53:58 -08:00
|
|
|
_screenshotter: Screenshotter;
|
2019-11-18 18:18:28 -08:00
|
|
|
private _fileChooserInterceptors = new Set<(chooser: FileChooser) => void>();
|
2019-11-25 15:00:04 -08:00
|
|
|
private _emulatedMediaType: string | undefined;
|
2019-11-18 18:18:28 -08:00
|
|
|
|
2019-12-06 11:33:24 -08:00
|
|
|
constructor(client: CDPSession, browserContext: BrowserContext, ignoreHTTPSErrors: boolean) {
|
2019-11-18 18:18:28 -08:00
|
|
|
super();
|
|
|
|
this._client = client;
|
2019-12-05 14:11:48 -08:00
|
|
|
this._closedPromise = new Promise(f => this._closedCallback = f);
|
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-06 13:36:47 -08:00
|
|
|
this.keyboard = new input.Keyboard(new RawKeyboardImpl(client));
|
|
|
|
this.mouse = new input.Mouse(new RawMouseImpl(client), this.keyboard);
|
2019-11-18 18:18:28 -08:00
|
|
|
this._timeoutSettings = new TimeoutSettings();
|
2019-11-19 16:29:28 -08:00
|
|
|
this.accessibility = new Accessibility(client);
|
2019-11-18 18:18:28 -08:00
|
|
|
this._frameManager = new FrameManager(client, this, ignoreHTTPSErrors, this._timeoutSettings);
|
2019-11-19 16:29:28 -08:00
|
|
|
this.coverage = new Coverage(client);
|
|
|
|
this.pdf = new PDF(client);
|
2019-12-06 13:36:47 -08:00
|
|
|
this.workers = new Workers(client, this._addConsoleMessage.bind(this), error => this.emit(Events.Page.PageError, error));
|
2019-11-26 15:13:49 -08:00
|
|
|
this.overrides = new Overrides(client);
|
2019-11-21 14:41:38 -08:00
|
|
|
this.interception = new Interception(this._frameManager.networkManager());
|
2019-12-06 11:33:24 -08:00
|
|
|
this._screenshotter = new Screenshotter(this, new CRScreenshotDelegate(this._client), browserContext.browser());
|
2019-11-18 18:18:28 -08:00
|
|
|
|
|
|
|
this._frameManager.on(FrameManagerEvents.FrameAttached, event => this.emit(Events.Page.FrameAttached, event));
|
|
|
|
this._frameManager.on(FrameManagerEvents.FrameDetached, event => this.emit(Events.Page.FrameDetached, event));
|
|
|
|
this._frameManager.on(FrameManagerEvents.FrameNavigated, event => this.emit(Events.Page.FrameNavigated, event));
|
|
|
|
|
|
|
|
const networkManager = this._frameManager.networkManager();
|
|
|
|
networkManager.on(NetworkManagerEvents.Request, event => this.emit(Events.Page.Request, event));
|
|
|
|
networkManager.on(NetworkManagerEvents.Response, event => this.emit(Events.Page.Response, event));
|
|
|
|
networkManager.on(NetworkManagerEvents.RequestFailed, event => this.emit(Events.Page.RequestFailed, event));
|
|
|
|
networkManager.on(NetworkManagerEvents.RequestFinished, event => this.emit(Events.Page.RequestFinished, event));
|
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
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
if (!this._fileChooserInterceptors.size) {
|
|
|
|
await handle.dispose();
|
2019-11-18 18:18:28 -08:00
|
|
|
return;
|
2019-12-06 13:36:47 -08:00
|
|
|
}
|
2019-11-18 18:18:28 -08:00
|
|
|
const interceptors = Array.from(this._fileChooserInterceptors);
|
|
|
|
this._fileChooserInterceptors.clear();
|
2019-11-26 22:53:34 -08:00
|
|
|
const multiple = await handle.evaluate((element: HTMLInputElement) => !!element.multiple);
|
2019-11-27 14:26:30 -08:00
|
|
|
const fileChooser = { element: handle, multiple };
|
2019-11-18 18:18:28 -08:00
|
|
|
for (const interceptor of interceptors)
|
|
|
|
interceptor.call(null, fileChooser);
|
2019-11-27 14:26:30 -08:00
|
|
|
this.emit(Events.Page.FileChooser, fileChooser);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async waitForFileChooser(options: { timeout?: number; } = {}): Promise<FileChooser> {
|
|
|
|
const {
|
|
|
|
timeout = this._timeoutSettings.timeout(),
|
|
|
|
} = options;
|
|
|
|
let callback;
|
|
|
|
const promise = new Promise<FileChooser>(x => callback = x);
|
|
|
|
this._fileChooserInterceptors.add(callback);
|
|
|
|
return helper.waitWithTimeout<FileChooser>(promise, 'waiting for file chooser', timeout).catch(e => {
|
|
|
|
this._fileChooserInterceptors.delete(callback);
|
|
|
|
throw e;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
browser(): Browser {
|
2019-12-05 14:11:48 -08:00
|
|
|
return this._browserContext.browser();
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
browserContext(): BrowserContext {
|
2019-12-05 14:11:48 -08:00
|
|
|
return this._browserContext;
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-11-27 16:03:51 -08:00
|
|
|
mainFrame(): frames.Frame {
|
2019-11-18 18:18:28 -08:00
|
|
|
return this._frameManager.mainFrame();
|
|
|
|
}
|
|
|
|
|
2019-11-27 16:03:51 -08:00
|
|
|
frames(): frames.Frame[] {
|
2019-11-18 18:18:28 -08:00
|
|
|
return this._frameManager.frames();
|
|
|
|
}
|
|
|
|
|
|
|
|
setDefaultNavigationTimeout(timeout: number) {
|
|
|
|
this._timeoutSettings.setDefaultNavigationTimeout(timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
setDefaultTimeout(timeout: number) {
|
|
|
|
this._timeoutSettings.setDefaultTimeout(timeout);
|
|
|
|
}
|
|
|
|
|
2019-12-05 16:26:09 -08:00
|
|
|
async $(selector: string | types.Selector): Promise<dom.ElementHandle<Element> | null> {
|
2019-11-18 18:18:28 -08:00
|
|
|
return this.mainFrame().$(selector);
|
|
|
|
}
|
|
|
|
|
2019-11-27 16:03:51 -08:00
|
|
|
evaluateHandle: types.EvaluateHandle = async (pageFunction, ...args) => {
|
2019-11-18 18:18:28 -08:00
|
|
|
const context = await this.mainFrame().executionContext();
|
2019-11-25 15:06:52 -08:00
|
|
|
return context.evaluateHandle(pageFunction, ...args as any);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-11-27 16:03:51 -08:00
|
|
|
$eval: types.$Eval = (selector, pageFunction, ...args) => {
|
2019-11-25 15:06:52 -08:00
|
|
|
return this.mainFrame().$eval(selector, pageFunction, ...args as any);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-11-27 16:03:51 -08:00
|
|
|
$$eval: types.$$Eval = (selector, pageFunction, ...args) => {
|
2019-11-25 15:06:52 -08:00
|
|
|
return this.mainFrame().$$eval(selector, pageFunction, ...args as any);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-12-05 16:26:09 -08:00
|
|
|
async $$(selector: string | types.Selector): Promise<dom.ElementHandle<Element>[]> {
|
2019-11-18 18:18:28 -08:00
|
|
|
return this.mainFrame().$$(selector);
|
|
|
|
}
|
|
|
|
|
2019-12-05 16:26:09 -08:00
|
|
|
async $x(expression: string): Promise<dom.ElementHandle<Element>[]> {
|
2019-11-18 18:18:28 -08:00
|
|
|
return this.mainFrame().$x(expression);
|
|
|
|
}
|
|
|
|
|
2019-11-27 16:02:31 -08:00
|
|
|
async addScriptTag(options: { url?: string; path?: string; content?: string; type?: string; }): Promise<dom.ElementHandle> {
|
2019-11-18 18:18:28 -08:00
|
|
|
return this.mainFrame().addScriptTag(options);
|
|
|
|
}
|
|
|
|
|
2019-11-27 16:02:31 -08:00
|
|
|
async addStyleTag(options: { url?: string; path?: string; content?: string; }): Promise<dom.ElementHandle> {
|
2019-11-18 18:18:28 -08:00
|
|
|
return this.mainFrame().addStyleTag(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
async exposeFunction(name: string, playwrightFunction: Function) {
|
|
|
|
if (this._pageBindings.has(name))
|
|
|
|
throw new Error(`Failed to add page binding with name ${name}: window['${name}'] already exists!`);
|
|
|
|
this._pageBindings.set(name, playwrightFunction);
|
2019-12-06 13:36:47 -08:00
|
|
|
await this._frameManager._exposeBinding(name, helper.evaluationString(addPageBinding, name));
|
2019-11-18 18:18:28 -08:00
|
|
|
|
|
|
|
function addPageBinding(bindingName: string) {
|
|
|
|
const binding = window[bindingName];
|
|
|
|
window[bindingName] = (...args) => {
|
|
|
|
const me = window[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;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async setExtraHTTPHeaders(headers: { [s: string]: string; }) {
|
|
|
|
return this._frameManager.networkManager().setExtraHTTPHeaders(headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
async setUserAgent(userAgent: string) {
|
|
|
|
return this._frameManager.networkManager().setUserAgent(userAgent);
|
|
|
|
}
|
|
|
|
|
2019-12-06 13:36:47 -08:00
|
|
|
async _onBindingCalled(payload: string, context: js.ExecutionContext) {
|
|
|
|
const {name, seq, args} = JSON.parse(payload);
|
2019-11-18 18:18:28 -08:00
|
|
|
let expression = null;
|
|
|
|
try {
|
|
|
|
const result = await this._pageBindings.get(name)(...args);
|
|
|
|
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);
|
|
|
|
}
|
2019-12-06 13:36:47 -08:00
|
|
|
context.evaluate(expression).catch(debugError);
|
2019-11-18 18:18:28 -08:00
|
|
|
|
|
|
|
function deliverResult(name: string, seq: number, result: any) {
|
|
|
|
window[name]['callbacks'].get(seq).resolve(result);
|
|
|
|
window[name]['callbacks'].delete(seq);
|
|
|
|
}
|
|
|
|
|
|
|
|
function deliverError(name: string, seq: number, message: string, stack: string) {
|
|
|
|
const error = new Error(message);
|
|
|
|
error.stack = stack;
|
|
|
|
window[name]['callbacks'].get(seq).reject(error);
|
|
|
|
window[name]['callbacks'].delete(seq);
|
|
|
|
}
|
|
|
|
|
|
|
|
function deliverErrorValue(name: string, seq: number, value: any) {
|
|
|
|
window[name]['callbacks'].get(seq).reject(value);
|
|
|
|
window[name]['callbacks'].delete(seq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-06 13:36:47 -08:00
|
|
|
_addConsoleMessage(type: string, args: js.JSHandle[], location: console.ConsoleMessageLocation) {
|
2019-11-18 18:18:28 -08:00
|
|
|
if (!this.listenerCount(Events.Page.Console)) {
|
|
|
|
args.forEach(arg => arg.dispose());
|
|
|
|
return;
|
|
|
|
}
|
2019-12-02 13:01:01 -08:00
|
|
|
this.emit(Events.Page.Console, new console.ConsoleMessage(type, undefined, args, location));
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
url(): string {
|
|
|
|
return this.mainFrame().url();
|
|
|
|
}
|
|
|
|
|
|
|
|
async content(): Promise<string> {
|
2019-12-06 13:36:47 -08:00
|
|
|
return await this.mainFrame().content();
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async setContent(html: string, options: { timeout?: number; waitUntil?: string | string[]; } | undefined) {
|
2019-12-06 13:36:47 -08:00
|
|
|
await this.mainFrame().setContent(html, options);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-11-27 16:03:51 -08:00
|
|
|
async goto(url: string, options: { referer?: string; timeout?: number; waitUntil?: string | string[]; } | undefined): Promise<network.Response | null> {
|
2019-12-06 13:36:47 -08:00
|
|
|
return await this.mainFrame().goto(url, options);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-11-27 16:03:51 -08:00
|
|
|
async reload(options: { timeout?: number; waitUntil?: string | string[]; } = {}): Promise<network.Response | null> {
|
2019-11-18 18:18:28 -08:00
|
|
|
const [response] = await Promise.all([
|
|
|
|
this.waitForNavigation(options),
|
|
|
|
this._client.send('Page.reload')
|
|
|
|
]);
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2019-11-27 16:03:51 -08:00
|
|
|
async waitForNavigation(options: { timeout?: number; waitUntil?: string | string[]; } = {}): Promise<network.Response | null> {
|
2019-12-06 13:36:47 -08:00
|
|
|
return await this.mainFrame().waitForNavigation(options);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async waitForRequest(urlOrPredicate: (string | Function), options: { timeout?: number; } = {}): Promise<Request> {
|
|
|
|
const {
|
|
|
|
timeout = this._timeoutSettings.timeout(),
|
|
|
|
} = options;
|
2019-12-06 13:36:47 -08:00
|
|
|
return helper.waitForEvent(this, Events.Page.Request, (request: network.Request) => {
|
2019-11-18 18:18:28 -08:00
|
|
|
if (helper.isString(urlOrPredicate))
|
|
|
|
return (urlOrPredicate === request.url());
|
|
|
|
if (typeof urlOrPredicate === 'function')
|
|
|
|
return !!(urlOrPredicate(request));
|
|
|
|
return false;
|
2019-12-06 13:36:47 -08:00
|
|
|
}, timeout, this._disconnectedPromise);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-11-27 16:03:51 -08:00
|
|
|
async waitForResponse(urlOrPredicate: (string | Function), options: { timeout?: number; } = {}): Promise<network.Response> {
|
2019-11-18 18:18:28 -08:00
|
|
|
const {
|
|
|
|
timeout = this._timeoutSettings.timeout(),
|
|
|
|
} = options;
|
2019-12-06 13:36:47 -08:00
|
|
|
return helper.waitForEvent(this, Events.Page.Response, (response: network.Response) => {
|
2019-11-18 18:18:28 -08:00
|
|
|
if (helper.isString(urlOrPredicate))
|
|
|
|
return (urlOrPredicate === response.url());
|
|
|
|
if (typeof urlOrPredicate === 'function')
|
|
|
|
return !!(urlOrPredicate(response));
|
|
|
|
return false;
|
2019-12-06 13:36:47 -08:00
|
|
|
}, timeout, this._disconnectedPromise);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-11-27 16:03:51 -08:00
|
|
|
async goBack(options: { timeout?: number; waitUntil?: string | string[]; } | undefined): Promise<network.Response | null> {
|
2019-11-18 18:18:28 -08:00
|
|
|
return this._go(-1, options);
|
|
|
|
}
|
|
|
|
|
2019-11-27 16:03:51 -08:00
|
|
|
async goForward(options: { timeout?: number; waitUntil?: string | string[]; } | undefined): Promise<network.Response | null> {
|
2019-11-18 18:18:28 -08:00
|
|
|
return this._go(+1, options);
|
|
|
|
}
|
|
|
|
|
2019-11-27 16:03:51 -08:00
|
|
|
async _go(delta, options: { timeout?: number; waitUntil?: string | string[]; } | undefined): Promise<network.Response | null> {
|
2019-11-18 18:18:28 -08:00
|
|
|
const history = await this._client.send('Page.getNavigationHistory');
|
|
|
|
const entry = history.entries[history.currentIndex + delta];
|
|
|
|
if (!entry)
|
|
|
|
return null;
|
|
|
|
const [response] = await Promise.all([
|
|
|
|
this.waitForNavigation(options),
|
|
|
|
this._client.send('Page.navigateToHistoryEntry', {entryId: entry.id}),
|
|
|
|
]);
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2019-12-06 11:33:24 -08:00
|
|
|
async emulate(options: { viewport: types.Viewport; userAgent: string; }) {
|
2019-11-18 18:18:28 -08:00
|
|
|
await Promise.all([
|
|
|
|
this.setViewport(options.viewport),
|
|
|
|
this.setUserAgent(options.userAgent)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
async setJavaScriptEnabled(enabled: boolean) {
|
|
|
|
if (this._javascriptEnabled === enabled)
|
|
|
|
return;
|
|
|
|
this._javascriptEnabled = enabled;
|
|
|
|
await this._client.send('Emulation.setScriptExecutionDisabled', { value: !enabled });
|
|
|
|
}
|
|
|
|
|
|
|
|
async setBypassCSP(enabled: boolean) {
|
|
|
|
await this._client.send('Page.setBypassCSP', { enabled });
|
|
|
|
}
|
|
|
|
|
2019-11-25 15:00:04 -08:00
|
|
|
async emulateMedia(options: {
|
|
|
|
type?: string,
|
|
|
|
colorScheme?: 'dark' | 'light' | 'no-preference' }) {
|
|
|
|
assert(!options.type || mediaTypes.has(options.type), 'Unsupported media type: ' + options.type);
|
|
|
|
assert(!options.colorScheme || mediaColorSchemes.has(options.colorScheme), 'Unsupported color scheme: ' + options.colorScheme);
|
|
|
|
const media = typeof options.type === 'undefined' ? this._emulatedMediaType : options.type;
|
|
|
|
const features = typeof options.colorScheme === 'undefined' ? [] : [{ name: 'prefers-color-scheme', value: options.colorScheme }];
|
|
|
|
await this._client.send('Emulation.setEmulatedMedia', { media: media || '', features });
|
|
|
|
this._emulatedMediaType = options.type;
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-12-06 11:33:24 -08:00
|
|
|
async setViewport(viewport: types.Viewport) {
|
2019-12-06 16:34:27 -08:00
|
|
|
const {
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
isMobile = false,
|
|
|
|
deviceScaleFactor = 1,
|
|
|
|
hasTouch = false,
|
|
|
|
isLandscape = false,
|
|
|
|
} = viewport;
|
|
|
|
const screenOrientation: Protocol.Emulation.ScreenOrientation = isLandscape ? { angle: 90, type: 'landscapePrimary' } : { angle: 0, type: 'portraitPrimary' };
|
|
|
|
await Promise.all([
|
|
|
|
this._client.send('Emulation.setDeviceMetricsOverride', { mobile: isMobile, width, height, deviceScaleFactor, screenOrientation }),
|
|
|
|
this._client.send('Emulation.setTouchEmulationEnabled', {
|
|
|
|
enabled: hasTouch
|
|
|
|
})
|
|
|
|
]);
|
|
|
|
const oldIsMobile = this._viewport ? !!this._viewport.isMobile : false;
|
|
|
|
const oldHasTouch = this._viewport ? !!this._viewport.hasTouch : false;
|
2019-11-18 18:18:28 -08:00
|
|
|
this._viewport = viewport;
|
2019-12-06 16:34:27 -08:00
|
|
|
if (oldIsMobile !== isMobile || oldHasTouch !== hasTouch)
|
2019-11-18 18:18:28 -08:00
|
|
|
await this.reload();
|
|
|
|
}
|
|
|
|
|
2019-12-06 11:33:24 -08:00
|
|
|
viewport(): types.Viewport | null {
|
2019-11-18 18:18:28 -08:00
|
|
|
return this._viewport;
|
|
|
|
}
|
|
|
|
|
2019-11-27 16:03:51 -08:00
|
|
|
evaluate: types.Evaluate = (pageFunction, ...args) => {
|
2019-12-06 13:36:47 -08:00
|
|
|
return this.mainFrame().evaluate(pageFunction, ...args as any);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async evaluateOnNewDocument(pageFunction: Function | string, ...args: any[]) {
|
|
|
|
const source = helper.evaluationString(pageFunction, ...args);
|
|
|
|
await this._client.send('Page.addScriptToEvaluateOnNewDocument', { source });
|
|
|
|
}
|
|
|
|
|
|
|
|
async setCacheEnabled(enabled: boolean = true) {
|
|
|
|
await this._frameManager.networkManager().setCacheEnabled(enabled);
|
|
|
|
}
|
|
|
|
|
2019-12-06 11:33:24 -08:00
|
|
|
screenshot(options?: types.ScreenshotOptions): Promise<Buffer> {
|
|
|
|
return this._screenshotter.screenshotPage(options);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async title(): Promise<string> {
|
|
|
|
return this.mainFrame().title();
|
|
|
|
}
|
|
|
|
|
|
|
|
async close(options: { runBeforeUnload: (boolean | undefined); } = {runBeforeUnload: undefined}) {
|
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;
|
|
|
|
if (runBeforeUnload) {
|
|
|
|
await this._client.send('Page.close');
|
|
|
|
} else {
|
2019-12-05 14:11:48 -08:00
|
|
|
await this.browser()._closePage(this);
|
|
|
|
await this._closedPromise;
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
isClosed(): boolean {
|
|
|
|
return this._closed;
|
|
|
|
}
|
|
|
|
|
2019-12-04 13:11:10 -08:00
|
|
|
click(selector: string | types.Selector, options?: ClickOptions) {
|
2019-11-18 18:18:28 -08:00
|
|
|
return this.mainFrame().click(selector, options);
|
|
|
|
}
|
|
|
|
|
2019-12-04 13:11:10 -08:00
|
|
|
dblclick(selector: string | types.Selector, options?: MultiClickOptions) {
|
2019-11-18 18:18:28 -08:00
|
|
|
return this.mainFrame().dblclick(selector, options);
|
|
|
|
}
|
|
|
|
|
2019-12-04 13:11:10 -08:00
|
|
|
tripleclick(selector: string | types.Selector, options?: MultiClickOptions) {
|
2019-11-18 18:18:28 -08:00
|
|
|
return this.mainFrame().tripleclick(selector, options);
|
|
|
|
}
|
|
|
|
|
2019-12-04 13:11:10 -08:00
|
|
|
fill(selector: string | types.Selector, value: string) {
|
2019-11-18 18:18:28 -08:00
|
|
|
return this.mainFrame().fill(selector, value);
|
|
|
|
}
|
|
|
|
|
2019-12-04 13:11:10 -08:00
|
|
|
focus(selector: string | types.Selector) {
|
2019-11-18 18:18:28 -08:00
|
|
|
return this.mainFrame().focus(selector);
|
|
|
|
}
|
|
|
|
|
2019-12-04 13:11:10 -08:00
|
|
|
hover(selector: string | types.Selector, options?: PointerActionOptions) {
|
2019-11-18 18:18:28 -08:00
|
|
|
return this.mainFrame().hover(selector, options);
|
|
|
|
}
|
|
|
|
|
2019-12-04 13:11:10 -08:00
|
|
|
select(selector: string | types.Selector, ...values: (string | dom.ElementHandle | SelectOption)[]): Promise<string[]> {
|
2019-11-18 18:18:28 -08:00
|
|
|
return this.mainFrame().select(selector, ...values);
|
|
|
|
}
|
|
|
|
|
2019-12-04 13:11:10 -08:00
|
|
|
type(selector: string | types.Selector, text: string, options: { delay: (number | undefined); } | undefined) {
|
2019-11-18 18:18:28 -08:00
|
|
|
return this.mainFrame().type(selector, text, options);
|
|
|
|
}
|
|
|
|
|
2019-11-27 16:03:51 -08:00
|
|
|
waitFor(selectorOrFunctionOrTimeout: (string | number | Function), options: { visible?: boolean; hidden?: boolean; timeout?: number; polling?: string | number; } = {}, ...args: any[]): Promise<js.JSHandle> {
|
2019-11-18 18:18:28 -08:00
|
|
|
return this.mainFrame().waitFor(selectorOrFunctionOrTimeout, options, ...args);
|
|
|
|
}
|
|
|
|
|
2019-12-04 13:11:10 -08:00
|
|
|
waitForSelector(selector: string | types.Selector, options: types.TimeoutOptions = {}): Promise<dom.ElementHandle | null> {
|
2019-11-18 18:18:28 -08:00
|
|
|
return this.mainFrame().waitForSelector(selector, options);
|
|
|
|
}
|
|
|
|
|
2019-12-04 13:11:10 -08:00
|
|
|
waitForXPath(xpath: string, options: types.TimeoutOptions = {}): Promise<dom.ElementHandle | null> {
|
2019-11-18 18:18:28 -08:00
|
|
|
return this.mainFrame().waitForXPath(xpath, options);
|
|
|
|
}
|
|
|
|
|
2019-12-04 13:11:10 -08:00
|
|
|
waitForFunction(pageFunction: Function | string, options: types.WaitForFunctionOptions, ...args: any[]): Promise<js.JSHandle> {
|
2019-11-18 18:18:28 -08:00
|
|
|
return this.mainFrame().waitForFunction(pageFunction, options, ...args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 14:26:30 -08:00
|
|
|
type FileChooser = {
|
2019-11-27 16:02:31 -08:00
|
|
|
element: dom.ElementHandle,
|
2019-11-27 14:26:30 -08:00
|
|
|
multiple: boolean
|
|
|
|
};
|