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-11-27 12:38:26 -08:00
|
|
|
import * as types from './types';
|
2019-11-26 08:57:53 -08:00
|
|
|
import * as fs from 'fs';
|
2019-11-27 12:39:53 -08:00
|
|
|
import * as js from './javascript';
|
2019-11-27 16:02:31 -08:00
|
|
|
import * as dom from './dom';
|
2019-11-27 12:44:12 -08:00
|
|
|
import * as network from './network';
|
2019-12-11 07:17:32 -08:00
|
|
|
import { helper, assert, RegisteredListener } from './helper';
|
2019-11-27 12:38:26 -08:00
|
|
|
import { ClickOptions, MultiClickOptions, PointerActionOptions, SelectOption } from './input';
|
2019-12-03 10:51:41 -08:00
|
|
|
import { TimeoutError } from './Errors';
|
2019-12-11 07:17:32 -08:00
|
|
|
import { Events } from './events';
|
2019-12-11 12:36:42 -08:00
|
|
|
import { Page } from './page';
|
2019-11-26 08:57:53 -08:00
|
|
|
|
|
|
|
const readFileAsync = helper.promisify(fs.readFile);
|
2019-11-18 18:18:28 -08:00
|
|
|
|
2019-11-26 15:37:25 -08:00
|
|
|
type WorldType = 'main' | 'utility';
|
2019-11-27 16:02:31 -08:00
|
|
|
type World = {
|
|
|
|
contextPromise: Promise<js.ExecutionContext>;
|
|
|
|
contextResolveCallback: (c: js.ExecutionContext) => void;
|
|
|
|
context: js.ExecutionContext | null;
|
2019-12-03 10:51:41 -08:00
|
|
|
rerunnableTasks: Set<RerunnableTask>;
|
2019-11-26 15:37:25 -08:00
|
|
|
};
|
|
|
|
|
2019-11-26 16:19:43 -08:00
|
|
|
export type NavigateOptions = {
|
|
|
|
timeout?: number,
|
2019-12-09 16:34:42 -08:00
|
|
|
waitUntil?: LifecycleEvent | LifecycleEvent[],
|
2019-11-26 16:19:43 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
export type GotoOptions = NavigateOptions & {
|
|
|
|
referer?: string,
|
|
|
|
};
|
|
|
|
|
2019-12-09 16:34:42 -08:00
|
|
|
export type LifecycleEvent = 'load' | 'domcontentloaded';
|
|
|
|
|
2019-11-27 16:02:31 -08:00
|
|
|
export class Frame {
|
2019-12-09 16:34:42 -08:00
|
|
|
readonly _firedLifecycleEvents: Set<LifecycleEvent>;
|
2019-12-11 07:17:32 -08:00
|
|
|
_lastDocumentId: string;
|
|
|
|
readonly _page: Page;
|
2019-11-27 16:02:31 -08:00
|
|
|
private _parentFrame: Frame;
|
2019-11-18 18:18:28 -08:00
|
|
|
private _url = '';
|
|
|
|
private _detached = false;
|
2019-11-27 16:02:31 -08:00
|
|
|
private _worlds = new Map<WorldType, World>();
|
|
|
|
private _childFrames = new Set<Frame>();
|
2019-11-18 18:18:28 -08:00
|
|
|
private _name: string;
|
|
|
|
|
2019-12-11 12:36:42 -08:00
|
|
|
constructor(page: Page, parentFrame: Frame | null) {
|
2019-12-09 16:34:42 -08:00
|
|
|
this._firedLifecycleEvents = new Set();
|
2019-12-11 07:17:32 -08:00
|
|
|
this._lastDocumentId = '';
|
|
|
|
this._page = page;
|
2019-11-18 18:18:28 -08:00
|
|
|
this._parentFrame = parentFrame;
|
|
|
|
|
2019-12-03 10:51:41 -08:00
|
|
|
this._worlds.set('main', { contextPromise: new Promise(() => {}), contextResolveCallback: () => {}, context: null, rerunnableTasks: new Set() });
|
|
|
|
this._worlds.set('utility', { contextPromise: new Promise(() => {}), contextResolveCallback: () => {}, context: null, rerunnableTasks: new Set() });
|
2019-11-26 15:37:25 -08:00
|
|
|
this._setContext('main', null);
|
|
|
|
this._setContext('utility', null);
|
2019-11-18 18:18:28 -08:00
|
|
|
|
|
|
|
if (this._parentFrame)
|
|
|
|
this._parentFrame._childFrames.add(this);
|
|
|
|
}
|
|
|
|
|
2019-12-09 14:53:17 -08:00
|
|
|
async goto(url: string, options?: GotoOptions): Promise<network.Response | null> {
|
2019-12-11 12:36:42 -08:00
|
|
|
return this._page._delegate.navigateFrame(this, url, options);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-12-09 14:53:17 -08:00
|
|
|
async waitForNavigation(options?: NavigateOptions): Promise<network.Response | null> {
|
2019-12-11 12:36:42 -08:00
|
|
|
return this._page._delegate.waitForFrameNavigation(this, options);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-11-27 16:02:31 -08:00
|
|
|
_mainContext(): Promise<js.ExecutionContext> {
|
2019-11-26 15:37:25 -08:00
|
|
|
if (this._detached)
|
|
|
|
throw new Error(`Execution Context is not available in detached frame "${this.url()}" (are you trying to evaluate?)`);
|
|
|
|
return this._worlds.get('main').contextPromise;
|
|
|
|
}
|
|
|
|
|
2019-11-28 12:50:52 -08:00
|
|
|
async _mainDOMWorld(): Promise<dom.DOMWorld> {
|
|
|
|
const context = await this._mainContext();
|
|
|
|
if (!context._domWorld)
|
|
|
|
throw new Error(`Execution Context does not belong to frame`);
|
|
|
|
return context._domWorld;
|
|
|
|
}
|
|
|
|
|
2019-11-27 16:02:31 -08:00
|
|
|
_utilityContext(): Promise<js.ExecutionContext> {
|
2019-11-26 15:37:25 -08:00
|
|
|
if (this._detached)
|
|
|
|
throw new Error(`Execution Context is not available in detached frame "${this.url()}" (are you trying to evaluate?)`);
|
|
|
|
return this._worlds.get('utility').contextPromise;
|
|
|
|
}
|
|
|
|
|
2019-11-28 12:50:52 -08:00
|
|
|
async _utilityDOMWorld(): Promise<dom.DOMWorld> {
|
|
|
|
const context = await this._utilityContext();
|
|
|
|
if (!context._domWorld)
|
|
|
|
throw new Error(`Execution Context does not belong to frame`);
|
|
|
|
return context._domWorld;
|
|
|
|
}
|
|
|
|
|
2019-11-27 16:02:31 -08:00
|
|
|
executionContext(): Promise<js.ExecutionContext> {
|
2019-11-26 15:37:25 -08:00
|
|
|
return this._mainContext();
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-11-27 16:03:51 -08:00
|
|
|
evaluateHandle: types.EvaluateHandle = async (pageFunction, ...args) => {
|
2019-11-26 15:37:25 -08:00
|
|
|
const context = await this._mainContext();
|
2019-11-26 08:57:53 -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
|
|
|
evaluate: types.Evaluate = async (pageFunction, ...args) => {
|
2019-11-26 15:37:25 -08:00
|
|
|
const context = await this._mainContext();
|
2019-11-26 08:57:53 -08:00
|
|
|
return context.evaluate(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> | null> {
|
2019-11-28 12:50:52 -08:00
|
|
|
const domWorld = await this._mainDOMWorld();
|
2019-12-04 13:11:10 -08:00
|
|
|
return domWorld.$(types.clearSelector(selector));
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-12-05 16:26:09 -08:00
|
|
|
async $x(expression: string): Promise<dom.ElementHandle<Element>[]> {
|
2019-11-28 12:50:52 -08:00
|
|
|
const domWorld = await this._mainDOMWorld();
|
2019-12-02 17:33:44 -08:00
|
|
|
return domWorld.$$('xpath=' + expression);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-11-27 16:03:51 -08:00
|
|
|
$eval: types.$Eval = async (selector, pageFunction, ...args) => {
|
2019-11-28 12:50:52 -08:00
|
|
|
const domWorld = await this._mainDOMWorld();
|
2019-12-02 17:33:44 -08:00
|
|
|
return domWorld.$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 = async (selector, pageFunction, ...args) => {
|
2019-11-28 12:50:52 -08:00
|
|
|
const domWorld = await this._mainDOMWorld();
|
2019-12-02 17:33:44 -08:00
|
|
|
return domWorld.$$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-28 12:50:52 -08:00
|
|
|
const domWorld = await this._mainDOMWorld();
|
2019-12-04 13:11:10 -08:00
|
|
|
return domWorld.$$(types.clearSelector(selector));
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async content(): Promise<string> {
|
2019-11-26 15:37:25 -08:00
|
|
|
const context = await this._utilityContext();
|
2019-11-26 08:57:53 -08:00
|
|
|
return context.evaluate(() => {
|
|
|
|
let retVal = '';
|
|
|
|
if (document.doctype)
|
|
|
|
retVal = new XMLSerializer().serializeToString(document.doctype);
|
|
|
|
if (document.documentElement)
|
|
|
|
retVal += document.documentElement.outerHTML;
|
|
|
|
return retVal;
|
|
|
|
});
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-12-09 14:53:17 -08:00
|
|
|
async setContent(html: string, options?: NavigateOptions): Promise<void> {
|
2019-12-11 12:36:42 -08:00
|
|
|
return this._page._delegate.setFrameContent(this, html, options);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
name(): string {
|
|
|
|
return this._name || '';
|
|
|
|
}
|
|
|
|
|
|
|
|
url(): string {
|
|
|
|
return this._url;
|
|
|
|
}
|
|
|
|
|
2019-11-27 16:02:31 -08:00
|
|
|
parentFrame(): Frame | null {
|
2019-11-18 18:18:28 -08:00
|
|
|
return this._parentFrame;
|
|
|
|
}
|
|
|
|
|
2019-11-27 16:02:31 -08:00
|
|
|
childFrames(): Frame[] {
|
2019-11-18 18:18:28 -08:00
|
|
|
return Array.from(this._childFrames);
|
|
|
|
}
|
|
|
|
|
|
|
|
isDetached(): boolean {
|
|
|
|
return this._detached;
|
|
|
|
}
|
|
|
|
|
|
|
|
async addScriptTag(options: {
|
|
|
|
url?: string; path?: string;
|
|
|
|
content?: string;
|
2019-11-26 08:57:53 -08:00
|
|
|
type?: string;
|
2019-11-27 16:02:31 -08:00
|
|
|
}): Promise<dom.ElementHandle> {
|
2019-11-26 08:57:53 -08:00
|
|
|
const {
|
|
|
|
url = null,
|
|
|
|
path = null,
|
|
|
|
content = null,
|
|
|
|
type = ''
|
|
|
|
} = options;
|
|
|
|
if (url !== null) {
|
|
|
|
try {
|
2019-11-26 15:37:25 -08:00
|
|
|
const context = await this._mainContext();
|
2019-11-26 08:57:53 -08:00
|
|
|
return (await context.evaluateHandle(addScriptUrl, url, type)).asElement();
|
|
|
|
} catch (error) {
|
|
|
|
throw new Error(`Loading script from ${url} failed`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path !== null) {
|
|
|
|
let contents = await readFileAsync(path, 'utf8');
|
|
|
|
contents += '//# sourceURL=' + path.replace(/\n/g, '');
|
2019-11-26 15:37:25 -08:00
|
|
|
const context = await this._mainContext();
|
2019-11-26 08:57:53 -08:00
|
|
|
return (await context.evaluateHandle(addScriptContent, contents, type)).asElement();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (content !== null) {
|
2019-11-26 15:37:25 -08:00
|
|
|
const context = await this._mainContext();
|
2019-11-26 08:57:53 -08:00
|
|
|
return (await context.evaluateHandle(addScriptContent, content, type)).asElement();
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error('Provide an object with a `url`, `path` or `content` property');
|
|
|
|
|
|
|
|
async function addScriptUrl(url: string, type: string): Promise<HTMLElement> {
|
|
|
|
const script = document.createElement('script');
|
|
|
|
script.src = url;
|
|
|
|
if (type)
|
|
|
|
script.type = type;
|
|
|
|
const promise = new Promise((res, rej) => {
|
|
|
|
script.onload = res;
|
|
|
|
script.onerror = rej;
|
|
|
|
});
|
|
|
|
document.head.appendChild(script);
|
|
|
|
await promise;
|
|
|
|
return script;
|
|
|
|
}
|
|
|
|
|
|
|
|
function addScriptContent(content: string, type: string = 'text/javascript'): HTMLElement {
|
|
|
|
const script = document.createElement('script');
|
|
|
|
script.type = type;
|
|
|
|
script.text = content;
|
|
|
|
let error = null;
|
|
|
|
script.onerror = e => error = e;
|
|
|
|
document.head.appendChild(script);
|
|
|
|
if (error)
|
|
|
|
throw error;
|
|
|
|
return script;
|
|
|
|
}
|
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> {
|
2019-11-26 08:57:53 -08:00
|
|
|
const {
|
|
|
|
url = null,
|
|
|
|
path = null,
|
|
|
|
content = null
|
|
|
|
} = options;
|
|
|
|
if (url !== null) {
|
|
|
|
try {
|
2019-11-26 15:37:25 -08:00
|
|
|
const context = await this._mainContext();
|
2019-11-26 08:57:53 -08:00
|
|
|
return (await context.evaluateHandle(addStyleUrl, url)).asElement();
|
|
|
|
} catch (error) {
|
|
|
|
throw new Error(`Loading style from ${url} failed`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path !== null) {
|
|
|
|
let contents = await readFileAsync(path, 'utf8');
|
|
|
|
contents += '/*# sourceURL=' + path.replace(/\n/g, '') + '*/';
|
2019-11-26 15:37:25 -08:00
|
|
|
const context = await this._mainContext();
|
2019-11-26 08:57:53 -08:00
|
|
|
return (await context.evaluateHandle(addStyleContent, contents)).asElement();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (content !== null) {
|
2019-11-26 15:37:25 -08:00
|
|
|
const context = await this._mainContext();
|
2019-11-26 08:57:53 -08:00
|
|
|
return (await context.evaluateHandle(addStyleContent, content)).asElement();
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error('Provide an object with a `url`, `path` or `content` property');
|
|
|
|
|
|
|
|
async function addStyleUrl(url: string): Promise<HTMLElement> {
|
|
|
|
const link = document.createElement('link');
|
|
|
|
link.rel = 'stylesheet';
|
|
|
|
link.href = url;
|
|
|
|
const promise = new Promise((res, rej) => {
|
|
|
|
link.onload = res;
|
|
|
|
link.onerror = rej;
|
|
|
|
});
|
|
|
|
document.head.appendChild(link);
|
|
|
|
await promise;
|
|
|
|
return link;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function addStyleContent(content: string): Promise<HTMLElement> {
|
|
|
|
const style = document.createElement('style');
|
|
|
|
style.type = 'text/css';
|
|
|
|
style.appendChild(document.createTextNode(content));
|
|
|
|
const promise = new Promise((res, rej) => {
|
|
|
|
style.onload = res;
|
|
|
|
style.onerror = rej;
|
|
|
|
});
|
|
|
|
document.head.appendChild(style);
|
|
|
|
await promise;
|
|
|
|
return style;
|
|
|
|
}
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-12-04 13:11:10 -08:00
|
|
|
async click(selector: string | types.Selector, options?: ClickOptions) {
|
2019-11-28 12:50:52 -08:00
|
|
|
const domWorld = await this._utilityDOMWorld();
|
2019-12-04 13:11:10 -08:00
|
|
|
const handle = await domWorld.$(types.clearSelector(selector));
|
|
|
|
assert(handle, 'No node found for selector: ' + types.selectorToString(selector));
|
2019-11-22 14:46:34 -08:00
|
|
|
await handle.click(options);
|
|
|
|
await handle.dispose();
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-12-04 13:11:10 -08:00
|
|
|
async dblclick(selector: string | types.Selector, options?: MultiClickOptions) {
|
2019-11-28 12:50:52 -08:00
|
|
|
const domWorld = await this._utilityDOMWorld();
|
2019-12-04 13:11:10 -08:00
|
|
|
const handle = await domWorld.$(types.clearSelector(selector));
|
|
|
|
assert(handle, 'No node found for selector: ' + types.selectorToString(selector));
|
2019-11-22 14:46:34 -08:00
|
|
|
await handle.dblclick(options);
|
|
|
|
await handle.dispose();
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-12-04 13:11:10 -08:00
|
|
|
async tripleclick(selector: string | types.Selector, options?: MultiClickOptions) {
|
2019-11-28 12:50:52 -08:00
|
|
|
const domWorld = await this._utilityDOMWorld();
|
2019-12-04 13:11:10 -08:00
|
|
|
const handle = await domWorld.$(types.clearSelector(selector));
|
|
|
|
assert(handle, 'No node found for selector: ' + types.selectorToString(selector));
|
2019-11-22 14:46:34 -08:00
|
|
|
await handle.tripleclick(options);
|
|
|
|
await handle.dispose();
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-12-04 13:11:10 -08:00
|
|
|
async fill(selector: string | types.Selector, value: string) {
|
2019-11-28 12:50:52 -08:00
|
|
|
const domWorld = await this._utilityDOMWorld();
|
2019-12-04 13:11:10 -08:00
|
|
|
const handle = await domWorld.$(types.clearSelector(selector));
|
|
|
|
assert(handle, 'No node found for selector: ' + types.selectorToString(selector));
|
2019-11-22 14:46:34 -08:00
|
|
|
await handle.fill(value);
|
|
|
|
await handle.dispose();
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-12-04 13:11:10 -08:00
|
|
|
async focus(selector: string | types.Selector) {
|
2019-11-28 12:50:52 -08:00
|
|
|
const domWorld = await this._utilityDOMWorld();
|
2019-12-04 13:11:10 -08:00
|
|
|
const handle = await domWorld.$(types.clearSelector(selector));
|
|
|
|
assert(handle, 'No node found for selector: ' + types.selectorToString(selector));
|
2019-11-22 14:46:34 -08:00
|
|
|
await handle.focus();
|
|
|
|
await handle.dispose();
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-12-04 13:11:10 -08:00
|
|
|
async hover(selector: string | types.Selector, options?: PointerActionOptions) {
|
2019-11-28 12:50:52 -08:00
|
|
|
const domWorld = await this._utilityDOMWorld();
|
2019-12-04 13:11:10 -08:00
|
|
|
const handle = await domWorld.$(types.clearSelector(selector));
|
|
|
|
assert(handle, 'No node found for selector: ' + types.selectorToString(selector));
|
2019-11-22 14:46:34 -08:00
|
|
|
await handle.hover(options);
|
|
|
|
await handle.dispose();
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-12-04 13:11:10 -08:00
|
|
|
async select(selector: string | types.Selector, ...values: (string | dom.ElementHandle | SelectOption)[]): Promise<string[]> {
|
2019-11-28 12:50:52 -08:00
|
|
|
const domWorld = await this._utilityDOMWorld();
|
2019-12-04 13:11:10 -08:00
|
|
|
const handle = await domWorld.$(types.clearSelector(selector));
|
|
|
|
assert(handle, 'No node found for selector: ' + types.selectorToString(selector));
|
2019-12-02 17:33:44 -08:00
|
|
|
const toDispose: Promise<dom.ElementHandle>[] = [];
|
2019-11-27 12:38:26 -08:00
|
|
|
const adoptedValues = await Promise.all(values.map(async value => {
|
2019-12-02 17:33:44 -08:00
|
|
|
if (value instanceof dom.ElementHandle && value.executionContext() !== domWorld.context) {
|
|
|
|
const adopted = domWorld.adoptElementHandle(value);
|
|
|
|
toDispose.push(adopted);
|
|
|
|
return adopted;
|
|
|
|
}
|
2019-11-27 12:38:26 -08:00
|
|
|
return value;
|
|
|
|
}));
|
2019-11-22 14:46:34 -08:00
|
|
|
const result = await handle.select(...adoptedValues);
|
|
|
|
await handle.dispose();
|
2019-12-02 17:33:44 -08:00
|
|
|
await Promise.all(toDispose.map(handlePromise => handlePromise.then(handle => handle.dispose())));
|
2019-11-22 14:46:34 -08:00
|
|
|
return result;
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-12-04 13:11:10 -08:00
|
|
|
async type(selector: string | types.Selector, text: string, options: { delay: (number | undefined); } | undefined) {
|
2019-11-28 12:50:52 -08:00
|
|
|
const domWorld = await this._utilityDOMWorld();
|
2019-12-04 13:11:10 -08:00
|
|
|
const handle = await domWorld.$(types.clearSelector(selector));
|
|
|
|
assert(handle, 'No node found for selector: ' + types.selectorToString(selector));
|
2019-11-22 14:46:34 -08:00
|
|
|
await handle.type(text, options);
|
|
|
|
await handle.dispose();
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-11-27 16:02:31 -08:00
|
|
|
waitFor(selectorOrFunctionOrTimeout: (string | number | Function), options: any = {}, ...args: any[]): Promise<js.JSHandle | null> {
|
2019-12-03 10:43:13 -08:00
|
|
|
if (helper.isString(selectorOrFunctionOrTimeout))
|
|
|
|
return this.waitForSelector(selectorOrFunctionOrTimeout as string, options) as any;
|
2019-11-18 18:18:28 -08:00
|
|
|
if (helper.isNumber(selectorOrFunctionOrTimeout))
|
|
|
|
return new Promise(fulfill => setTimeout(fulfill, selectorOrFunctionOrTimeout as number));
|
|
|
|
if (typeof selectorOrFunctionOrTimeout === 'function')
|
|
|
|
return this.waitForFunction(selectorOrFunctionOrTimeout, options, ...args);
|
|
|
|
return Promise.reject(new Error('Unsupported target type: ' + (typeof selectorOrFunctionOrTimeout)));
|
|
|
|
}
|
|
|
|
|
2019-12-04 13:11:10 -08:00
|
|
|
async waitForSelector(selector: string | types.Selector, options: types.TimeoutOptions = {}): Promise<dom.ElementHandle | null> {
|
2019-12-11 07:17:32 -08:00
|
|
|
const { timeout = this._page._timeoutSettings.timeout() } = options;
|
2019-12-04 13:11:10 -08:00
|
|
|
const task = dom.waitForSelectorTask(types.clearSelector(selector), timeout);
|
|
|
|
const handle = await this._scheduleRerunnableTask(task, 'utility', timeout, `selector "${types.selectorToString(selector)}"`);
|
2019-11-26 15:37:25 -08:00
|
|
|
if (!handle.asElement()) {
|
|
|
|
await handle.dispose();
|
|
|
|
return null;
|
2019-11-26 11:16:20 -08:00
|
|
|
}
|
2019-11-28 12:50:52 -08:00
|
|
|
const mainDOMWorld = await this._mainDOMWorld();
|
2019-12-02 17:33:44 -08:00
|
|
|
if (handle.executionContext() === mainDOMWorld.context)
|
|
|
|
return handle.asElement();
|
|
|
|
const adopted = await mainDOMWorld.adoptElementHandle(handle.asElement());
|
|
|
|
await handle.dispose();
|
|
|
|
return adopted;
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-12-04 13:11:10 -08:00
|
|
|
async waitForXPath(xpath: string, options: types.TimeoutOptions = {}): Promise<dom.ElementHandle | null> {
|
2019-12-03 10:43:13 -08:00
|
|
|
return this.waitForSelector('xpath=' + xpath, options);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-12-04 13:11:10 -08:00
|
|
|
waitForFunction(pageFunction: Function | string, options: types.WaitForFunctionOptions = {}, ...args: any[]): Promise<js.JSHandle> {
|
2019-12-11 07:17:32 -08:00
|
|
|
options = { timeout: this._page._timeoutSettings.timeout(), ...options };
|
2019-12-03 10:51:41 -08:00
|
|
|
const task = dom.waitForFunctionTask(pageFunction, options, ...args);
|
|
|
|
return this._scheduleRerunnableTask(task, 'main', options.timeout);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async title(): Promise<string> {
|
2019-11-26 15:37:25 -08:00
|
|
|
const context = await this._utilityContext();
|
2019-11-26 08:57:53 -08:00
|
|
|
return context.evaluate(() => document.title);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-12-11 07:17:32 -08:00
|
|
|
_onExpectedNewDocumentNavigation(documentId: string, url?: string) {
|
|
|
|
for (const watcher of this._page._lifecycleWatchers)
|
|
|
|
watcher._onExpectedNewDocumentNavigation(this, documentId, url);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onAbortedNewDocumentNavigation(documentId: string, errorText: string) {
|
|
|
|
for (const watcher of this._page._lifecycleWatchers)
|
|
|
|
watcher._onAbortedNewDocumentNavigation(this, documentId, errorText);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onCommittedNewDocumentNavigation(url: string, name: string, documentId: string) {
|
2019-11-18 18:18:28 -08:00
|
|
|
this._url = url;
|
2019-11-26 16:19:43 -08:00
|
|
|
this._name = name;
|
2019-12-11 07:17:32 -08:00
|
|
|
this._lastDocumentId = documentId;
|
|
|
|
this._firedLifecycleEvents.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
_onCommittedSameDocumentNavigation(url: string) {
|
|
|
|
this._url = url;
|
|
|
|
for (const watcher of this._page._lifecycleWatchers)
|
|
|
|
watcher._onNavigatedWithinDocument(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
_lifecycleEvent(event: LifecycleEvent) {
|
|
|
|
this._firedLifecycleEvents.add(event);
|
|
|
|
for (const watcher of this._page._lifecycleWatchers)
|
|
|
|
watcher._onLifecycleEvent(this);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-12-11 07:17:32 -08:00
|
|
|
_onDetached() {
|
2019-11-18 18:18:28 -08:00
|
|
|
this._detached = true;
|
2019-11-26 15:37:25 -08:00
|
|
|
for (const world of this._worlds.values()) {
|
2019-12-03 10:51:41 -08:00
|
|
|
for (const rerunnableTask of world.rerunnableTasks)
|
|
|
|
rerunnableTask.terminate(new Error('waitForFunction failed: frame got detached.'));
|
2019-11-26 15:37:25 -08:00
|
|
|
}
|
2019-11-18 18:18:28 -08:00
|
|
|
if (this._parentFrame)
|
|
|
|
this._parentFrame._childFrames.delete(this);
|
|
|
|
this._parentFrame = null;
|
2019-12-11 07:17:32 -08:00
|
|
|
for (const watcher of this._page._lifecycleWatchers)
|
|
|
|
watcher._onFrameDetached(this);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
2019-11-26 15:37:25 -08:00
|
|
|
|
2019-12-03 10:51:41 -08:00
|
|
|
private _scheduleRerunnableTask(task: dom.Task, worldType: WorldType, timeout?: number, title?: string): Promise<js.JSHandle> {
|
2019-12-03 10:43:13 -08:00
|
|
|
const world = this._worlds.get(worldType);
|
2019-12-03 10:51:41 -08:00
|
|
|
const rerunnableTask = new RerunnableTask(world, task, timeout, title);
|
|
|
|
world.rerunnableTasks.add(rerunnableTask);
|
2019-11-26 15:37:25 -08:00
|
|
|
if (world.context)
|
2019-12-03 10:51:41 -08:00
|
|
|
rerunnableTask.rerun(world.context._domWorld);
|
|
|
|
return rerunnableTask.promise;
|
2019-11-26 15:37:25 -08:00
|
|
|
}
|
|
|
|
|
2019-11-27 16:02:31 -08:00
|
|
|
private _setContext(worldType: WorldType, context: js.ExecutionContext | null) {
|
2019-11-26 15:37:25 -08:00
|
|
|
const world = this._worlds.get(worldType);
|
|
|
|
world.context = context;
|
|
|
|
if (context) {
|
2019-12-03 10:51:41 -08:00
|
|
|
assert(context._domWorld, 'Frame context must have a dom world');
|
2019-11-26 15:37:25 -08:00
|
|
|
world.contextResolveCallback.call(null, context);
|
2019-12-03 10:51:41 -08:00
|
|
|
for (const rerunnableTask of world.rerunnableTasks)
|
|
|
|
rerunnableTask.rerun(context._domWorld);
|
2019-11-26 15:37:25 -08:00
|
|
|
} else {
|
|
|
|
world.contextPromise = new Promise(fulfill => {
|
|
|
|
world.contextResolveCallback = fulfill;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 16:02:31 -08:00
|
|
|
_contextCreated(worldType: WorldType, context: js.ExecutionContext) {
|
2019-11-26 15:37:25 -08:00
|
|
|
const world = this._worlds.get(worldType);
|
|
|
|
// In case of multiple sessions to the same target, there's a race between
|
|
|
|
// connections so we might end up creating multiple isolated worlds.
|
|
|
|
// We can use either.
|
2019-12-03 11:47:02 -07:00
|
|
|
if (world.context)
|
|
|
|
this._setContext(worldType, null);
|
|
|
|
this._setContext(worldType, context);
|
2019-11-26 15:37:25 -08:00
|
|
|
}
|
|
|
|
|
2019-11-27 16:02:31 -08:00
|
|
|
_contextDestroyed(context: js.ExecutionContext) {
|
2019-11-26 15:37:25 -08:00
|
|
|
for (const [worldType, world] of this._worlds) {
|
|
|
|
if (world.context === context)
|
|
|
|
this._setContext(worldType, null);
|
|
|
|
}
|
|
|
|
}
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
2019-12-03 10:51:41 -08:00
|
|
|
|
|
|
|
class RerunnableTask {
|
|
|
|
readonly promise: Promise<js.JSHandle>;
|
|
|
|
private _world: World;
|
|
|
|
private _task: dom.Task;
|
|
|
|
private _runCount: number;
|
|
|
|
private _resolve: (result: js.JSHandle) => void;
|
|
|
|
private _reject: (reason: Error) => void;
|
|
|
|
private _timeoutTimer: NodeJS.Timer;
|
|
|
|
private _terminated: boolean;
|
|
|
|
|
|
|
|
constructor(world: World, task: dom.Task, timeout?: number, title?: string) {
|
|
|
|
this._world = world;
|
|
|
|
this._task = task;
|
|
|
|
this._runCount = 0;
|
|
|
|
this.promise = new Promise<js.JSHandle>((resolve, reject) => {
|
|
|
|
this._resolve = resolve;
|
|
|
|
this._reject = reject;
|
|
|
|
});
|
|
|
|
// Since page navigation requires us to re-install the pageScript, we should track
|
|
|
|
// timeout on our end.
|
|
|
|
if (timeout) {
|
|
|
|
const timeoutError = new TimeoutError(`waiting for ${title || 'function'} failed: timeout ${timeout}ms exceeded`);
|
|
|
|
this._timeoutTimer = setTimeout(() => this.terminate(timeoutError), timeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
terminate(error: Error) {
|
|
|
|
this._terminated = true;
|
|
|
|
this._reject(error);
|
|
|
|
this._doCleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
async rerun(domWorld: dom.DOMWorld) {
|
|
|
|
const runCount = ++this._runCount;
|
|
|
|
let success: js.JSHandle | null = null;
|
|
|
|
let error = null;
|
|
|
|
try {
|
|
|
|
success = await this._task(domWorld);
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._terminated || runCount !== this._runCount) {
|
|
|
|
if (success)
|
|
|
|
await success.dispose();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore timeouts in pageScript - we track timeouts ourselves.
|
|
|
|
// If execution context has been already destroyed, `context.evaluate` will
|
|
|
|
// throw an error - ignore this predicate run altogether.
|
|
|
|
if (!error && await domWorld.context.evaluate(s => !s, success).catch(e => true)) {
|
|
|
|
await success.dispose();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// When the page is navigated, the promise is rejected.
|
|
|
|
// We will try again in the new execution context.
|
|
|
|
if (error && error.message.includes('Execution context was destroyed'))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// We could have tried to evaluate in a context which was already
|
|
|
|
// destroyed.
|
|
|
|
if (error && error.message.includes('Cannot find context with specified id'))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
this._reject(error);
|
|
|
|
else
|
|
|
|
this._resolve(success);
|
|
|
|
|
|
|
|
this._doCleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
_doCleanup() {
|
|
|
|
clearTimeout(this._timeoutTimer);
|
|
|
|
this._world.rerunnableTasks.delete(this);
|
|
|
|
}
|
|
|
|
}
|
2019-12-11 07:17:32 -08:00
|
|
|
|
|
|
|
export class LifecycleWatcher {
|
|
|
|
readonly sameDocumentNavigationPromise: Promise<Error | null>;
|
|
|
|
readonly lifecyclePromise: Promise<void>;
|
|
|
|
readonly newDocumentNavigationPromise: Promise<Error | null>;
|
|
|
|
readonly timeoutOrTerminationPromise: Promise<Error | null>;
|
|
|
|
private _expectedLifecycle: LifecycleEvent[];
|
|
|
|
private _frame: Frame;
|
|
|
|
private _navigationRequest: network.Request | null = null;
|
|
|
|
private _sameDocumentNavigationCompleteCallback: () => void;
|
|
|
|
private _lifecycleCallback: () => void;
|
|
|
|
private _newDocumentNavigationCompleteCallback: () => void;
|
|
|
|
private _frameDetachedCallback: (err: Error) => void;
|
|
|
|
private _navigationAbortedCallback: (err: Error) => void;
|
|
|
|
private _maximumTimer: NodeJS.Timer;
|
|
|
|
private _hasSameDocumentNavigation: boolean;
|
|
|
|
private _listeners: RegisteredListener[];
|
|
|
|
private _targetUrl?: string;
|
|
|
|
private _expectedDocumentId?: string;
|
|
|
|
|
|
|
|
constructor(frame: Frame, waitUntil: LifecycleEvent | LifecycleEvent[], timeout: number) {
|
|
|
|
if (Array.isArray(waitUntil))
|
|
|
|
waitUntil = waitUntil.slice();
|
|
|
|
else if (typeof waitUntil === 'string')
|
|
|
|
waitUntil = [waitUntil];
|
|
|
|
if (waitUntil.some(e => e !== 'load' && e !== 'domcontentloaded'))
|
|
|
|
throw new Error('Unsupported waitUntil option');
|
|
|
|
this._expectedLifecycle = waitUntil.slice();
|
|
|
|
this._frame = frame;
|
|
|
|
this.sameDocumentNavigationPromise = new Promise(f => this._sameDocumentNavigationCompleteCallback = f);
|
|
|
|
this.lifecyclePromise = new Promise(f => this._lifecycleCallback = f);
|
|
|
|
this.newDocumentNavigationPromise = new Promise(f => this._newDocumentNavigationCompleteCallback = f);
|
|
|
|
this.timeoutOrTerminationPromise = Promise.race([
|
|
|
|
this._createTimeoutPromise(timeout),
|
|
|
|
new Promise<Error>(f => this._frameDetachedCallback = f),
|
|
|
|
new Promise<Error>(f => this._navigationAbortedCallback = f),
|
|
|
|
this._frame._page._disconnectedPromise.then(() => new Error('Navigation failed because browser has disconnected!')),
|
|
|
|
]);
|
|
|
|
frame._page._lifecycleWatchers.add(this);
|
|
|
|
this._listeners = [
|
|
|
|
helper.addEventListener(this._frame._page, Events.Page.Request, (request: network.Request) => {
|
|
|
|
if (request.frame() === this._frame && request.isNavigationRequest())
|
|
|
|
this._navigationRequest = request;
|
|
|
|
}),
|
|
|
|
];
|
|
|
|
this._checkLifecycleComplete();
|
|
|
|
}
|
|
|
|
|
|
|
|
_onFrameDetached(frame: Frame) {
|
|
|
|
if (this._frame === frame) {
|
|
|
|
this._frameDetachedCallback.call(null, new Error('Navigating frame was detached'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._checkLifecycleComplete();
|
|
|
|
}
|
|
|
|
|
|
|
|
_onNavigatedWithinDocument(frame: Frame) {
|
|
|
|
if (frame !== this._frame)
|
|
|
|
return;
|
|
|
|
this._hasSameDocumentNavigation = true;
|
|
|
|
this._checkLifecycleComplete();
|
|
|
|
}
|
|
|
|
|
|
|
|
_onExpectedNewDocumentNavigation(frame: Frame, documentId: string, url?: string) {
|
|
|
|
if (frame === this._frame && this._expectedDocumentId === undefined) {
|
|
|
|
this._expectedDocumentId = documentId;
|
|
|
|
this._targetUrl = url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onAbortedNewDocumentNavigation(frame: Frame, documentId: string, errorText: string) {
|
|
|
|
if (frame === this._frame && documentId === this._expectedDocumentId) {
|
|
|
|
if (this._targetUrl)
|
|
|
|
this._navigationAbortedCallback(new Error('Navigation to ' + this._targetUrl + ' failed: ' + errorText));
|
|
|
|
else
|
|
|
|
this._navigationAbortedCallback(new Error('Navigation failed: ' + errorText));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onLifecycleEvent(frame: Frame) {
|
|
|
|
this._checkLifecycleComplete();
|
|
|
|
}
|
|
|
|
|
|
|
|
navigationResponse(): network.Response | null {
|
|
|
|
return this._navigationRequest ? this._navigationRequest.response() : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private _createTimeoutPromise(timeout: number): Promise<Error | null> {
|
|
|
|
if (!timeout)
|
|
|
|
return new Promise(() => {});
|
|
|
|
const errorMessage = 'Navigation timeout of ' + timeout + ' ms exceeded';
|
|
|
|
return new Promise(fulfill => this._maximumTimer = setTimeout(fulfill, timeout))
|
|
|
|
.then(() => new TimeoutError(errorMessage));
|
|
|
|
}
|
|
|
|
|
|
|
|
private _checkLifecycleRecursively(frame: Frame, expectedLifecycle: LifecycleEvent[]): boolean {
|
|
|
|
for (const event of expectedLifecycle) {
|
|
|
|
if (!frame._firedLifecycleEvents.has(event))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (const child of frame.childFrames()) {
|
|
|
|
if (!this._checkLifecycleRecursively(child, expectedLifecycle))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private _checkLifecycleComplete() {
|
|
|
|
// We expect navigation to commit.
|
|
|
|
if (!this._checkLifecycleRecursively(this._frame, this._expectedLifecycle))
|
|
|
|
return;
|
|
|
|
this._lifecycleCallback();
|
|
|
|
if (this._hasSameDocumentNavigation)
|
|
|
|
this._sameDocumentNavigationCompleteCallback();
|
|
|
|
if (this._frame._lastDocumentId === this._expectedDocumentId)
|
|
|
|
this._newDocumentNavigationCompleteCallback();
|
|
|
|
}
|
|
|
|
|
|
|
|
dispose() {
|
|
|
|
this._frame._page._lifecycleWatchers.delete(this);
|
|
|
|
helper.removeEventListeners(this._listeners);
|
|
|
|
clearTimeout(this._maximumTimer);
|
|
|
|
}
|
|
|
|
}
|