2017-06-17 21:27:51 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2017-08-02 19:06:47 +00:00
|
|
|
const EventEmitter = require('events');
|
2018-05-31 23:53:51 +00:00
|
|
|
const {helper, assert} = require('./helper');
|
2019-01-15 03:57:05 +00:00
|
|
|
const {Events} = require('./Events');
|
2019-01-22 22:55:33 +00:00
|
|
|
const {ExecutionContext, EVALUATION_SCRIPT_URL} = require('./ExecutionContext');
|
2019-01-15 04:34:50 +00:00
|
|
|
const {LifecycleWatcher} = require('./LifecycleWatcher');
|
2019-01-16 01:21:23 +00:00
|
|
|
const {DOMWorld} = require('./DOMWorld');
|
2017-10-12 08:26:44 +00:00
|
|
|
|
2019-01-22 22:55:33 +00:00
|
|
|
const UTILITY_WORLD_NAME = '__puppeteer_utility_world__';
|
|
|
|
|
2017-06-17 21:27:51 +00:00
|
|
|
class FrameManager extends EventEmitter {
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2018-01-11 03:33:22 +00:00
|
|
|
* @param {!Puppeteer.CDPSession} client
|
2018-04-08 00:58:52 +00:00
|
|
|
* @param {!Protocol.Page.FrameTree} frameTree
|
2017-10-10 05:31:40 +00:00
|
|
|
* @param {!Puppeteer.Page} page
|
2018-09-19 20:12:28 +00:00
|
|
|
* @param {!Puppeteer.NetworkManager} networkManager
|
2019-01-29 01:16:12 +00:00
|
|
|
* @param {!Puppeteer.TimeoutSettings} timeoutSettings
|
2017-06-21 20:58:49 +00:00
|
|
|
*/
|
2019-01-29 01:16:12 +00:00
|
|
|
constructor(client, frameTree, page, networkManager, timeoutSettings) {
|
2017-06-21 20:51:06 +00:00
|
|
|
super();
|
|
|
|
this._client = client;
|
2017-10-07 07:28:24 +00:00
|
|
|
this._page = page;
|
2018-09-19 20:12:28 +00:00
|
|
|
this._networkManager = networkManager;
|
2019-01-29 01:16:12 +00:00
|
|
|
this._timeoutSettings = timeoutSettings;
|
2017-06-21 20:51:06 +00:00
|
|
|
/** @type {!Map<string, !Frame>} */
|
|
|
|
this._frames = new Map();
|
2018-04-08 00:58:52 +00:00
|
|
|
/** @type {!Map<number, !ExecutionContext>} */
|
2017-10-06 22:35:02 +00:00
|
|
|
this._contextIdToContext = new Map();
|
2019-01-22 22:55:33 +00:00
|
|
|
/** @type {!Set<string>} */
|
|
|
|
this._isolatedWorlds = new Set();
|
2017-06-21 20:51:06 +00:00
|
|
|
|
2017-06-27 19:40:46 +00:00
|
|
|
this._client.on('Page.frameAttached', event => this._onFrameAttached(event.frameId, event.parentFrameId));
|
|
|
|
this._client.on('Page.frameNavigated', event => this._onFrameNavigated(event.frame));
|
2018-04-10 06:38:20 +00:00
|
|
|
this._client.on('Page.navigatedWithinDocument', event => this._onFrameNavigatedWithinDocument(event.frameId, event.url));
|
2017-06-27 19:40:46 +00:00
|
|
|
this._client.on('Page.frameDetached', event => this._onFrameDetached(event.frameId));
|
2018-04-10 22:59:41 +00:00
|
|
|
this._client.on('Page.frameStoppedLoading', event => this._onFrameStoppedLoading(event.frameId));
|
2017-06-27 19:40:46 +00:00
|
|
|
this._client.on('Runtime.executionContextCreated', event => this._onExecutionContextCreated(event.context));
|
2017-11-19 00:27:52 +00:00
|
|
|
this._client.on('Runtime.executionContextDestroyed', event => this._onExecutionContextDestroyed(event.executionContextId));
|
|
|
|
this._client.on('Runtime.executionContextsCleared', event => this._onExecutionContextsCleared());
|
2017-11-10 23:33:14 +00:00
|
|
|
this._client.on('Page.lifecycleEvent', event => this._onLifecycleEvent(event));
|
2017-10-18 02:14:57 +00:00
|
|
|
this._handleFrameTree(frameTree);
|
|
|
|
}
|
|
|
|
|
2018-09-19 20:12:28 +00:00
|
|
|
/**
|
|
|
|
* @param {!Puppeteer.Frame} frame
|
|
|
|
* @param {string} url
|
2018-11-12 20:59:21 +00:00
|
|
|
* @param {!{referer?: string, timeout?: number, waitUntil?: string|!Array<string>}=} options
|
2018-09-19 20:12:28 +00:00
|
|
|
* @return {!Promise<?Puppeteer.Response>}
|
|
|
|
*/
|
|
|
|
async navigateFrame(frame, url, options = {}) {
|
2018-11-20 23:32:46 +00:00
|
|
|
assertNoLegacyNavigationOptions(options);
|
2018-11-12 20:59:21 +00:00
|
|
|
const {
|
|
|
|
referer = this._networkManager.extraHTTPHeaders()['referer'],
|
2018-11-20 23:32:46 +00:00
|
|
|
waitUntil = ['load'],
|
2019-01-29 01:16:12 +00:00
|
|
|
timeout = this._timeoutSettings.navigationTimeout(),
|
2018-11-12 20:59:21 +00:00
|
|
|
} = options;
|
2018-09-19 20:12:28 +00:00
|
|
|
|
2018-11-20 23:32:46 +00:00
|
|
|
const watcher = new LifecycleWatcher(this, frame, waitUntil, timeout);
|
2018-09-19 20:12:28 +00:00
|
|
|
let ensureNewDocumentNavigation = false;
|
|
|
|
let error = await Promise.race([
|
2018-11-12 20:59:21 +00:00
|
|
|
navigate(this._client, url, referer, frame._id),
|
2018-09-19 20:12:28 +00:00
|
|
|
watcher.timeoutOrTerminationPromise(),
|
|
|
|
]);
|
|
|
|
if (!error) {
|
|
|
|
error = await Promise.race([
|
|
|
|
watcher.timeoutOrTerminationPromise(),
|
|
|
|
ensureNewDocumentNavigation ? watcher.newDocumentNavigationPromise() : watcher.sameDocumentNavigationPromise(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
watcher.dispose();
|
|
|
|
if (error)
|
|
|
|
throw error;
|
|
|
|
return watcher.navigationResponse();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Puppeteer.CDPSession} client
|
|
|
|
* @param {string} url
|
|
|
|
* @param {string} referrer
|
2018-09-20 18:31:19 +00:00
|
|
|
* @param {string} frameId
|
2018-09-19 20:12:28 +00:00
|
|
|
* @return {!Promise<?Error>}
|
|
|
|
*/
|
2018-09-20 18:31:19 +00:00
|
|
|
async function navigate(client, url, referrer, frameId) {
|
2018-09-19 20:12:28 +00:00
|
|
|
try {
|
2018-09-20 18:31:19 +00:00
|
|
|
const response = await client.send('Page.navigate', {url, referrer, frameId});
|
2018-09-19 20:12:28 +00:00
|
|
|
ensureNewDocumentNavigation = !!response.loaderId;
|
|
|
|
return response.errorText ? new Error(`${response.errorText} at ${url}`) : null;
|
|
|
|
} catch (error) {
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Puppeteer.Frame} frame
|
2018-11-12 20:59:21 +00:00
|
|
|
* @param {!{timeout?: number, waitUntil?: string|!Array<string>}=} options
|
2018-09-19 20:12:28 +00:00
|
|
|
* @return {!Promise<?Puppeteer.Response>}
|
|
|
|
*/
|
2018-11-12 20:59:21 +00:00
|
|
|
async waitForFrameNavigation(frame, options = {}) {
|
2018-11-20 23:32:46 +00:00
|
|
|
assertNoLegacyNavigationOptions(options);
|
2018-11-12 20:59:21 +00:00
|
|
|
const {
|
2018-11-20 23:32:46 +00:00
|
|
|
waitUntil = ['load'],
|
2019-01-29 01:16:12 +00:00
|
|
|
timeout = this._timeoutSettings.navigationTimeout(),
|
2018-11-12 20:59:21 +00:00
|
|
|
} = options;
|
2018-11-20 23:32:46 +00:00
|
|
|
const watcher = new LifecycleWatcher(this, frame, waitUntil, timeout);
|
2018-09-19 20:12:28 +00:00
|
|
|
const error = await Promise.race([
|
|
|
|
watcher.timeoutOrTerminationPromise(),
|
|
|
|
watcher.sameDocumentNavigationPromise(),
|
|
|
|
watcher.newDocumentNavigationPromise()
|
|
|
|
]);
|
|
|
|
watcher.dispose();
|
|
|
|
if (error)
|
|
|
|
throw error;
|
|
|
|
return watcher.navigationResponse();
|
|
|
|
}
|
|
|
|
|
2017-11-10 23:33:14 +00:00
|
|
|
/**
|
2018-04-08 00:58:52 +00:00
|
|
|
* @param {!Protocol.Page.lifecycleEventPayload} event
|
2017-11-10 23:33:14 +00:00
|
|
|
*/
|
|
|
|
_onLifecycleEvent(event) {
|
|
|
|
const frame = this._frames.get(event.frameId);
|
|
|
|
if (!frame)
|
|
|
|
return;
|
|
|
|
frame._onLifecycleEvent(event.loaderId, event.name);
|
2019-01-15 03:57:05 +00:00
|
|
|
this.emit(Events.FrameManager.LifecycleEvent, frame);
|
2017-11-10 23:33:14 +00:00
|
|
|
}
|
|
|
|
|
2018-04-10 22:59:41 +00:00
|
|
|
/**
|
|
|
|
* @param {string} frameId
|
|
|
|
*/
|
|
|
|
_onFrameStoppedLoading(frameId) {
|
|
|
|
const frame = this._frames.get(frameId);
|
|
|
|
if (!frame)
|
|
|
|
return;
|
|
|
|
frame._onLoadingStopped();
|
2019-01-15 03:57:05 +00:00
|
|
|
this.emit(Events.FrameManager.LifecycleEvent, frame);
|
2018-04-10 22:59:41 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 02:14:57 +00:00
|
|
|
/**
|
2018-04-08 00:58:52 +00:00
|
|
|
* @param {!Protocol.Page.FrameTree} frameTree
|
2017-10-18 02:14:57 +00:00
|
|
|
*/
|
|
|
|
_handleFrameTree(frameTree) {
|
|
|
|
if (frameTree.frame.parentId)
|
|
|
|
this._onFrameAttached(frameTree.frame.id, frameTree.frame.parentId);
|
|
|
|
this._onFrameNavigated(frameTree.frame);
|
|
|
|
if (!frameTree.childFrames)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (const child of frameTree.childFrames)
|
|
|
|
this._handleFrameTree(child);
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
|
|
|
|
2018-08-16 23:16:27 +00:00
|
|
|
/**
|
|
|
|
* @return {!Puppeteer.Page}
|
|
|
|
*/
|
|
|
|
page() {
|
|
|
|
return this._page;
|
|
|
|
}
|
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-06-21 20:58:49 +00:00
|
|
|
* @return {!Frame}
|
|
|
|
*/
|
2017-06-21 20:51:06 +00:00
|
|
|
mainFrame() {
|
|
|
|
return this._mainFrame;
|
|
|
|
}
|
2017-06-21 20:36:04 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-06-21 20:58:49 +00:00
|
|
|
* @return {!Array<!Frame>}
|
|
|
|
*/
|
2017-06-21 20:51:06 +00:00
|
|
|
frames() {
|
|
|
|
return Array.from(this._frames.values());
|
|
|
|
}
|
2017-06-21 20:36:04 +00:00
|
|
|
|
2018-01-10 02:47:21 +00:00
|
|
|
/**
|
|
|
|
* @param {!string} frameId
|
|
|
|
* @return {?Frame}
|
|
|
|
*/
|
|
|
|
frame(frameId) {
|
|
|
|
return this._frames.get(frameId) || null;
|
|
|
|
}
|
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-06-21 20:58:49 +00:00
|
|
|
* @param {string} frameId
|
|
|
|
* @param {?string} parentFrameId
|
|
|
|
*/
|
2017-06-27 19:40:46 +00:00
|
|
|
_onFrameAttached(frameId, parentFrameId) {
|
2017-06-21 20:51:06 +00:00
|
|
|
if (this._frames.has(frameId))
|
|
|
|
return;
|
2018-05-31 23:53:51 +00:00
|
|
|
assert(parentFrameId);
|
2017-08-21 23:39:04 +00:00
|
|
|
const parentFrame = this._frames.get(parentFrameId);
|
2018-08-16 23:16:27 +00:00
|
|
|
const frame = new Frame(this, this._client, parentFrame, frameId);
|
2017-06-21 20:51:06 +00:00
|
|
|
this._frames.set(frame._id, frame);
|
2019-01-15 03:57:05 +00:00
|
|
|
this.emit(Events.FrameManager.FrameAttached, frame);
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-07 01:20:48 +00:00
|
|
|
* @param {!Protocol.Page.Frame} framePayload
|
2017-06-21 20:58:49 +00:00
|
|
|
*/
|
2017-06-27 19:40:46 +00:00
|
|
|
_onFrameNavigated(framePayload) {
|
2017-07-28 23:52:38 +00:00
|
|
|
const isMainFrame = !framePayload.parentId;
|
|
|
|
let frame = isMainFrame ? this._mainFrame : this._frames.get(framePayload.id);
|
2018-05-31 23:53:51 +00:00
|
|
|
assert(isMainFrame || frame, 'We either navigate top level or have old version of the navigated frame');
|
2017-07-28 23:52:38 +00:00
|
|
|
|
|
|
|
// Detach all child frames first.
|
|
|
|
if (frame) {
|
2017-08-21 23:39:04 +00:00
|
|
|
for (const child of frame.childFrames())
|
2017-07-28 23:52:38 +00:00
|
|
|
this._removeFramesRecursively(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update or create main frame.
|
|
|
|
if (isMainFrame) {
|
|
|
|
if (frame) {
|
|
|
|
// Update frame id to retain frame identity on cross-process navigation.
|
2017-10-10 05:31:40 +00:00
|
|
|
this._frames.delete(frame._id);
|
2017-07-28 23:52:38 +00:00
|
|
|
frame._id = framePayload.id;
|
|
|
|
} else {
|
|
|
|
// Initial main frame navigation.
|
2018-08-16 23:16:27 +00:00
|
|
|
frame = new Frame(this, this._client, null, framePayload.id);
|
2017-07-28 23:52:38 +00:00
|
|
|
}
|
|
|
|
this._frames.set(framePayload.id, frame);
|
|
|
|
this._mainFrame = frame;
|
2017-06-21 20:36:04 +00:00
|
|
|
}
|
2017-07-28 23:52:38 +00:00
|
|
|
|
|
|
|
// Update frame payload.
|
|
|
|
frame._navigated(framePayload);
|
|
|
|
|
2019-01-15 03:57:05 +00:00
|
|
|
this.emit(Events.FrameManager.FrameNavigated, frame);
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-21 20:36:04 +00:00
|
|
|
|
2019-01-22 22:55:33 +00:00
|
|
|
async ensureSecondaryDOMWorld() {
|
|
|
|
await this._ensureIsolatedWorld(UTILITY_WORLD_NAME);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} name
|
|
|
|
*/
|
|
|
|
async _ensureIsolatedWorld(name) {
|
|
|
|
if (this._isolatedWorlds.has(name))
|
|
|
|
return;
|
|
|
|
this._isolatedWorlds.add(name);
|
|
|
|
await this._client.send('Page.addScriptToEvaluateOnNewDocument', {
|
|
|
|
source: `//# sourceURL=${EVALUATION_SCRIPT_URL}`,
|
|
|
|
worldName: name,
|
|
|
|
}),
|
|
|
|
await Promise.all(this.frames().map(frame => this._client.send('Page.createIsolatedWorld', {
|
|
|
|
frameId: frame._id,
|
|
|
|
grantUniveralAccess: true,
|
|
|
|
worldName: name,
|
|
|
|
})));
|
|
|
|
}
|
|
|
|
|
2018-04-10 06:38:20 +00:00
|
|
|
/**
|
|
|
|
* @param {string} frameId
|
|
|
|
* @param {string} url
|
|
|
|
*/
|
|
|
|
_onFrameNavigatedWithinDocument(frameId, url) {
|
|
|
|
const frame = this._frames.get(frameId);
|
|
|
|
if (!frame)
|
|
|
|
return;
|
|
|
|
frame._navigatedWithinDocument(url);
|
2019-01-15 03:57:05 +00:00
|
|
|
this.emit(Events.FrameManager.FrameNavigatedWithinDocument, frame);
|
|
|
|
this.emit(Events.FrameManager.FrameNavigated, frame);
|
2018-04-10 06:38:20 +00:00
|
|
|
}
|
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-06-21 20:58:49 +00:00
|
|
|
* @param {string} frameId
|
|
|
|
*/
|
2017-06-27 19:40:46 +00:00
|
|
|
_onFrameDetached(frameId) {
|
2017-08-21 23:39:04 +00:00
|
|
|
const frame = this._frames.get(frameId);
|
2017-06-21 20:51:06 +00:00
|
|
|
if (frame)
|
|
|
|
this._removeFramesRecursively(frame);
|
|
|
|
}
|
2017-06-21 20:36:04 +00:00
|
|
|
|
2017-10-06 22:35:02 +00:00
|
|
|
_onExecutionContextCreated(contextPayload) {
|
2018-08-09 21:57:08 +00:00
|
|
|
const frameId = contextPayload.auxData ? contextPayload.auxData.frameId : null;
|
|
|
|
const frame = this._frames.get(frameId) || null;
|
2019-01-22 22:55:33 +00:00
|
|
|
let world = null;
|
|
|
|
if (frame) {
|
|
|
|
if (contextPayload.auxData && !!contextPayload.auxData['isDefault'])
|
|
|
|
world = frame._mainWorld;
|
|
|
|
else if (contextPayload.name === UTILITY_WORLD_NAME)
|
|
|
|
world = frame._secondaryWorld;
|
|
|
|
}
|
|
|
|
if (contextPayload.auxData && contextPayload.auxData['type'] === 'isolated')
|
|
|
|
this._isolatedWorlds.add(contextPayload.name);
|
2018-08-01 20:53:08 +00:00
|
|
|
/** @type {!ExecutionContext} */
|
2019-01-22 22:55:33 +00:00
|
|
|
const context = new ExecutionContext(this._client, contextPayload, world);
|
|
|
|
if (world)
|
|
|
|
world._setContext(context);
|
2017-10-06 22:35:02 +00:00
|
|
|
this._contextIdToContext.set(contextPayload.id, context);
|
2017-11-19 00:27:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-08 00:58:52 +00:00
|
|
|
* @param {number} executionContextId
|
2017-11-19 00:27:52 +00:00
|
|
|
*/
|
|
|
|
_onExecutionContextDestroyed(executionContextId) {
|
|
|
|
const context = this._contextIdToContext.get(executionContextId);
|
|
|
|
if (!context)
|
2017-07-25 21:30:04 +00:00
|
|
|
return;
|
2017-11-19 00:27:52 +00:00
|
|
|
this._contextIdToContext.delete(executionContextId);
|
2019-01-22 22:55:33 +00:00
|
|
|
if (context._world)
|
|
|
|
context._world._setContext(null);
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-21 20:36:04 +00:00
|
|
|
|
2017-11-19 00:27:52 +00:00
|
|
|
_onExecutionContextsCleared() {
|
2018-08-10 01:14:21 +00:00
|
|
|
for (const context of this._contextIdToContext.values()) {
|
2019-01-22 22:55:33 +00:00
|
|
|
if (context._world)
|
|
|
|
context._world._setContext(null);
|
2018-08-09 21:57:08 +00:00
|
|
|
}
|
2018-08-10 01:14:21 +00:00
|
|
|
this._contextIdToContext.clear();
|
2017-10-06 22:35:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-08 00:58:52 +00:00
|
|
|
* @param {number} contextId
|
2018-08-01 20:53:08 +00:00
|
|
|
* @return {!ExecutionContext}
|
2017-10-06 22:35:02 +00:00
|
|
|
*/
|
2018-08-01 20:53:08 +00:00
|
|
|
executionContextById(contextId) {
|
2017-10-06 22:35:02 +00:00
|
|
|
const context = this._contextIdToContext.get(contextId);
|
2018-05-31 23:53:51 +00:00
|
|
|
assert(context, 'INTERNAL ERROR: missing context with id = ' + contextId);
|
2018-08-01 20:53:08 +00:00
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-06-21 20:58:49 +00:00
|
|
|
* @param {!Frame} frame
|
|
|
|
*/
|
2017-06-21 20:51:06 +00:00
|
|
|
_removeFramesRecursively(frame) {
|
2017-08-21 23:39:04 +00:00
|
|
|
for (const child of frame.childFrames())
|
2017-06-21 20:51:06 +00:00
|
|
|
this._removeFramesRecursively(child);
|
|
|
|
frame._detach();
|
|
|
|
this._frames.delete(frame._id);
|
2019-01-15 03:57:05 +00:00
|
|
|
this.emit(Events.FrameManager.FrameDetached, frame);
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-17 21:27:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @unrestricted
|
|
|
|
*/
|
|
|
|
class Frame {
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2018-08-16 23:16:27 +00:00
|
|
|
* @param {!FrameManager} frameManager
|
2018-01-11 03:33:22 +00:00
|
|
|
* @param {!Puppeteer.CDPSession} client
|
2017-06-21 20:58:49 +00:00
|
|
|
* @param {?Frame} parentFrame
|
|
|
|
* @param {string} frameId
|
|
|
|
*/
|
2018-08-16 23:16:27 +00:00
|
|
|
constructor(frameManager, client, parentFrame, frameId) {
|
|
|
|
this._frameManager = frameManager;
|
2017-07-25 21:30:04 +00:00
|
|
|
this._client = client;
|
2017-06-21 20:51:06 +00:00
|
|
|
this._parentFrame = parentFrame;
|
|
|
|
this._url = '';
|
|
|
|
this._id = frameId;
|
2018-08-24 22:17:36 +00:00
|
|
|
this._detached = false;
|
2017-11-19 00:27:52 +00:00
|
|
|
|
2017-11-10 23:33:14 +00:00
|
|
|
this._loaderId = '';
|
|
|
|
/** @type {!Set<string>} */
|
|
|
|
this._lifecycleEvents = new Set();
|
2019-01-29 01:16:12 +00:00
|
|
|
this._mainWorld = new DOMWorld(frameManager, this, frameManager._timeoutSettings);
|
|
|
|
this._secondaryWorld = new DOMWorld(frameManager, this, frameManager._timeoutSettings);
|
2017-07-20 02:04:51 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
/** @type {!Set<!Frame>} */
|
|
|
|
this._childFrames = new Set();
|
|
|
|
if (this._parentFrame)
|
|
|
|
this._parentFrame._childFrames.add(this);
|
|
|
|
}
|
|
|
|
|
2018-09-20 18:31:19 +00:00
|
|
|
/**
|
|
|
|
* @param {string} url
|
2018-11-12 20:59:21 +00:00
|
|
|
* @param {!{referer?: string, timeout?: number, waitUntil?: string|!Array<string>}=} options
|
2018-09-20 18:31:19 +00:00
|
|
|
* @return {!Promise<?Puppeteer.Response>}
|
|
|
|
*/
|
2018-11-12 20:59:21 +00:00
|
|
|
async goto(url, options) {
|
2018-09-20 18:31:19 +00:00
|
|
|
return await this._frameManager.navigateFrame(this, url, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-12 20:59:21 +00:00
|
|
|
* @param {!{timeout?: number, waitUntil?: string|!Array<string>}=} options
|
2018-09-20 18:31:19 +00:00
|
|
|
* @return {!Promise<?Puppeteer.Response>}
|
|
|
|
*/
|
2018-11-12 20:59:21 +00:00
|
|
|
async waitForNavigation(options) {
|
2018-09-20 18:31:19 +00:00
|
|
|
return await this._frameManager.waitForFrameNavigation(this, options);
|
|
|
|
}
|
|
|
|
|
2017-11-19 00:27:52 +00:00
|
|
|
/**
|
|
|
|
* @return {!Promise<!ExecutionContext>}
|
2017-10-06 22:35:02 +00:00
|
|
|
*/
|
|
|
|
executionContext() {
|
2019-01-16 01:21:23 +00:00
|
|
|
return this._mainWorld.executionContext();
|
2017-10-06 22:35:02 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 05:16:01 +00:00
|
|
|
/**
|
2019-01-11 06:56:39 +00:00
|
|
|
* @param {Function|string} pageFunction
|
2018-01-25 05:16:01 +00:00
|
|
|
* @param {!Array<*>} args
|
|
|
|
* @return {!Promise<!Puppeteer.JSHandle>}
|
|
|
|
*/
|
|
|
|
async evaluateHandle(pageFunction, ...args) {
|
2019-01-16 01:21:23 +00:00
|
|
|
return this._mainWorld.evaluateHandle(pageFunction, ...args);
|
2018-01-25 05:16:01 +00:00
|
|
|
}
|
|
|
|
|
2017-06-27 19:40:46 +00:00
|
|
|
/**
|
2017-10-10 05:31:40 +00:00
|
|
|
* @param {Function|string} pageFunction
|
2017-06-27 19:40:46 +00:00
|
|
|
* @param {!Array<*>} args
|
2017-10-10 05:31:40 +00:00
|
|
|
* @return {!Promise<*>}
|
2017-06-27 19:40:46 +00:00
|
|
|
*/
|
2017-07-14 20:51:22 +00:00
|
|
|
async evaluate(pageFunction, ...args) {
|
2019-01-16 01:21:23 +00:00
|
|
|
return this._mainWorld.evaluate(pageFunction, ...args);
|
2017-08-15 21:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} selector
|
2018-08-16 23:16:27 +00:00
|
|
|
* @return {!Promise<?Puppeteer.ElementHandle>}
|
2017-08-15 21:54:02 +00:00
|
|
|
*/
|
|
|
|
async $(selector) {
|
2019-01-16 01:21:23 +00:00
|
|
|
return this._mainWorld.$(selector);
|
2017-12-20 00:23:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} expression
|
2018-08-16 23:16:27 +00:00
|
|
|
* @return {!Promise<!Array<!Puppeteer.ElementHandle>>}
|
2017-12-20 00:23:45 +00:00
|
|
|
*/
|
2018-01-03 23:37:08 +00:00
|
|
|
async $x(expression) {
|
2019-01-16 01:21:23 +00:00
|
|
|
return this._mainWorld.$x(expression);
|
2017-08-15 21:54:02 +00:00
|
|
|
}
|
|
|
|
|
2017-08-31 22:38:01 +00:00
|
|
|
/**
|
|
|
|
* @param {string} selector
|
2017-10-10 05:31:40 +00:00
|
|
|
* @param {Function|string} pageFunction
|
2017-08-31 22:38:01 +00:00
|
|
|
* @param {!Array<*>} args
|
|
|
|
* @return {!Promise<(!Object|undefined)>}
|
|
|
|
*/
|
|
|
|
async $eval(selector, pageFunction, ...args) {
|
2019-01-16 01:21:23 +00:00
|
|
|
return this._mainWorld.$eval(selector, pageFunction, ...args);
|
2017-08-31 22:38:01 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 06:23:14 +00:00
|
|
|
/**
|
|
|
|
* @param {string} selector
|
|
|
|
* @param {Function|string} pageFunction
|
|
|
|
* @param {!Array<*>} args
|
|
|
|
* @return {!Promise<(!Object|undefined)>}
|
|
|
|
*/
|
|
|
|
async $$eval(selector, pageFunction, ...args) {
|
2019-01-16 01:21:23 +00:00
|
|
|
return this._mainWorld.$$eval(selector, pageFunction, ...args);
|
2017-10-11 06:23:14 +00:00
|
|
|
}
|
|
|
|
|
2017-08-23 05:56:55 +00:00
|
|
|
/**
|
|
|
|
* @param {string} selector
|
2018-08-16 23:16:27 +00:00
|
|
|
* @return {!Promise<!Array<!Puppeteer.ElementHandle>>}
|
2017-08-23 05:56:55 +00:00
|
|
|
*/
|
|
|
|
async $$(selector) {
|
2019-01-16 01:21:23 +00:00
|
|
|
return this._mainWorld.$$(selector);
|
2017-08-23 05:56:55 +00:00
|
|
|
}
|
|
|
|
|
2017-11-23 02:44:33 +00:00
|
|
|
/**
|
|
|
|
* @return {!Promise<String>}
|
|
|
|
*/
|
|
|
|
async content() {
|
2019-01-23 04:24:14 +00:00
|
|
|
return this._secondaryWorld.content();
|
2017-11-23 02:44:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} html
|
2018-11-20 23:32:46 +00:00
|
|
|
* @param {!{timeout?: number, waitUntil?: string|!Array<string>}=} options
|
2017-11-23 02:44:33 +00:00
|
|
|
*/
|
2018-11-20 23:32:46 +00:00
|
|
|
async setContent(html, options = {}) {
|
2019-01-23 04:24:14 +00:00
|
|
|
return this._secondaryWorld.setContent(html, options);
|
2017-11-23 02:44:33 +00:00
|
|
|
}
|
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-06-21 20:58:49 +00:00
|
|
|
* @return {string}
|
|
|
|
*/
|
2017-06-21 20:51:06 +00:00
|
|
|
name() {
|
|
|
|
return this._name || '';
|
|
|
|
}
|
2017-06-21 20:36:04 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-06-21 20:58:49 +00:00
|
|
|
* @return {string}
|
|
|
|
*/
|
2017-06-21 20:51:06 +00:00
|
|
|
url() {
|
|
|
|
return this._url;
|
|
|
|
}
|
2017-06-21 20:36:04 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-06-21 20:58:49 +00:00
|
|
|
* @return {?Frame}
|
|
|
|
*/
|
2017-06-21 20:51:06 +00:00
|
|
|
parentFrame() {
|
|
|
|
return this._parentFrame;
|
|
|
|
}
|
2017-06-21 20:36:04 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-06-21 20:58:49 +00:00
|
|
|
* @return {!Array.<!Frame>}
|
|
|
|
*/
|
2017-06-21 20:51:06 +00:00
|
|
|
childFrames() {
|
|
|
|
return Array.from(this._childFrames);
|
|
|
|
}
|
2017-06-21 20:36:04 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-06-21 20:58:49 +00:00
|
|
|
* @return {boolean}
|
|
|
|
*/
|
2017-06-21 20:51:06 +00:00
|
|
|
isDetached() {
|
|
|
|
return this._detached;
|
|
|
|
}
|
2017-06-21 20:36:04 +00:00
|
|
|
|
2017-07-25 18:37:46 +00:00
|
|
|
/**
|
2018-11-12 20:59:21 +00:00
|
|
|
* @param {!{url?: string, path?: string, content?: string, type?: string}} options
|
2018-08-16 23:16:27 +00:00
|
|
|
* @return {!Promise<!Puppeteer.ElementHandle>}
|
2017-07-25 18:37:46 +00:00
|
|
|
*/
|
2017-10-12 08:26:44 +00:00
|
|
|
async addScriptTag(options) {
|
2019-01-16 01:21:23 +00:00
|
|
|
return this._mainWorld.addScriptTag(options);
|
2017-07-25 18:37:46 +00:00
|
|
|
}
|
|
|
|
|
2017-10-04 20:42:26 +00:00
|
|
|
/**
|
2018-11-12 20:59:21 +00:00
|
|
|
* @param {!{url?: string, path?: string, content?: string}} options
|
2018-08-16 23:16:27 +00:00
|
|
|
* @return {!Promise<!Puppeteer.ElementHandle>}
|
2017-10-04 20:42:26 +00:00
|
|
|
*/
|
2017-10-12 08:26:44 +00:00
|
|
|
async addStyleTag(options) {
|
2019-01-16 01:21:23 +00:00
|
|
|
return this._mainWorld.addStyleTag(options);
|
2017-10-04 20:42:26 +00:00
|
|
|
}
|
|
|
|
|
2018-02-05 22:58:03 +00:00
|
|
|
/**
|
|
|
|
* @param {string} selector
|
2018-11-12 20:59:21 +00:00
|
|
|
* @param {!{delay?: number, button?: "left"|"right"|"middle", clickCount?: number}=} options
|
2018-02-05 22:58:03 +00:00
|
|
|
*/
|
2018-11-12 20:59:21 +00:00
|
|
|
async click(selector, options) {
|
2019-01-23 04:24:14 +00:00
|
|
|
return this._secondaryWorld.click(selector, options);
|
2018-02-05 22:58:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} selector
|
|
|
|
*/
|
|
|
|
async focus(selector) {
|
2019-01-23 04:24:14 +00:00
|
|
|
return this._secondaryWorld.focus(selector);
|
2018-02-05 22:58:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} selector
|
|
|
|
*/
|
|
|
|
async hover(selector) {
|
2019-01-23 04:24:14 +00:00
|
|
|
return this._secondaryWorld.hover(selector);
|
2018-02-05 22:58:03 +00:00
|
|
|
}
|
|
|
|
|
2017-11-02 05:06:04 +00:00
|
|
|
/**
|
|
|
|
* @param {string} selector
|
|
|
|
* @param {!Array<string>} values
|
|
|
|
* @return {!Promise<!Array<string>>}
|
|
|
|
*/
|
2018-01-25 05:12:27 +00:00
|
|
|
select(selector, ...values){
|
2019-01-22 22:55:33 +00:00
|
|
|
return this._secondaryWorld.select(selector, ...values);
|
2017-11-02 05:06:04 +00:00
|
|
|
}
|
|
|
|
|
2018-02-05 22:58:03 +00:00
|
|
|
/**
|
|
|
|
* @param {string} selector
|
|
|
|
*/
|
|
|
|
async tap(selector) {
|
2019-01-23 04:24:14 +00:00
|
|
|
return this._secondaryWorld.tap(selector);
|
2018-02-05 22:58:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} selector
|
|
|
|
* @param {string} text
|
|
|
|
* @param {{delay: (number|undefined)}=} options
|
|
|
|
*/
|
|
|
|
async type(selector, text, options) {
|
2019-01-16 01:21:23 +00:00
|
|
|
return this._mainWorld.type(selector, text, options);
|
2018-02-05 22:58:03 +00:00
|
|
|
}
|
|
|
|
|
2017-07-21 19:41:49 +00:00
|
|
|
/**
|
2017-10-10 05:31:40 +00:00
|
|
|
* @param {(string|number|Function)} selectorOrFunctionOrTimeout
|
2017-07-21 19:41:49 +00:00
|
|
|
* @param {!Object=} options
|
2017-09-15 21:28:15 +00:00
|
|
|
* @param {!Array<*>} args
|
2019-01-28 19:24:53 +00:00
|
|
|
* @return {!Promise<?Puppeteer.JSHandle>}
|
2017-07-21 19:41:49 +00:00
|
|
|
*/
|
2017-09-15 21:28:15 +00:00
|
|
|
waitFor(selectorOrFunctionOrTimeout, options = {}, ...args) {
|
2019-01-22 22:55:33 +00:00
|
|
|
const xPathPattern = '//';
|
|
|
|
|
|
|
|
if (helper.isString(selectorOrFunctionOrTimeout)) {
|
|
|
|
const string = /** @type {string} */ (selectorOrFunctionOrTimeout);
|
|
|
|
if (string.startsWith(xPathPattern))
|
|
|
|
return this.waitForXPath(string, options);
|
|
|
|
return this.waitForSelector(string, options);
|
|
|
|
}
|
|
|
|
if (helper.isNumber(selectorOrFunctionOrTimeout))
|
|
|
|
return new Promise(fulfill => setTimeout(fulfill, /** @type {number} */ (selectorOrFunctionOrTimeout)));
|
|
|
|
if (typeof selectorOrFunctionOrTimeout === 'function')
|
|
|
|
return this.waitForFunction(selectorOrFunctionOrTimeout, options, ...args);
|
|
|
|
return Promise.reject(new Error('Unsupported target type: ' + (typeof selectorOrFunctionOrTimeout)));
|
2017-07-21 19:41:49 +00:00
|
|
|
}
|
|
|
|
|
2017-07-07 22:39:02 +00:00
|
|
|
/**
|
|
|
|
* @param {string} selector
|
2018-11-12 20:59:21 +00:00
|
|
|
* @param {!{visible?: boolean, hidden?: boolean, timeout?: number}=} options
|
2019-01-28 19:24:53 +00:00
|
|
|
* @return {!Promise<?Puppeteer.ElementHandle>}
|
2017-07-07 22:39:02 +00:00
|
|
|
*/
|
2019-01-28 20:24:27 +00:00
|
|
|
async waitForSelector(selector, options) {
|
|
|
|
const handle = await this._secondaryWorld.waitForSelector(selector, options);
|
|
|
|
if (!handle)
|
|
|
|
return null;
|
|
|
|
const mainExecutionContext = await this._mainWorld.executionContext();
|
|
|
|
const result = await mainExecutionContext._adoptElementHandle(handle);
|
|
|
|
await handle.dispose();
|
|
|
|
return result;
|
2018-01-22 23:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} xpath
|
2018-11-12 20:59:21 +00:00
|
|
|
* @param {!{visible?: boolean, hidden?: boolean, timeout?: number}=} options
|
2019-01-28 19:24:53 +00:00
|
|
|
* @return {!Promise<?Puppeteer.ElementHandle>}
|
2018-01-22 23:16:20 +00:00
|
|
|
*/
|
2019-01-28 20:24:27 +00:00
|
|
|
async waitForXPath(xpath, options) {
|
|
|
|
const handle = await this._secondaryWorld.waitForXPath(xpath, options);
|
|
|
|
if (!handle)
|
|
|
|
return null;
|
|
|
|
const mainExecutionContext = await this._mainWorld.executionContext();
|
|
|
|
const result = await mainExecutionContext._adoptElementHandle(handle);
|
|
|
|
await handle.dispose();
|
|
|
|
return result;
|
2018-01-22 23:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Function|string} pageFunction
|
2018-11-12 20:59:21 +00:00
|
|
|
* @param {!{polling?: string|number, timeout?: number}=} options
|
2018-11-08 06:48:43 +00:00
|
|
|
* @return {!Promise<!Puppeteer.JSHandle>}
|
2018-01-22 23:16:20 +00:00
|
|
|
*/
|
|
|
|
waitForFunction(pageFunction, options = {}, ...args) {
|
2019-01-16 01:21:23 +00:00
|
|
|
return this._mainWorld.waitForFunction(pageFunction, options, ...args);
|
2018-01-22 23:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {!Promise<string>}
|
|
|
|
*/
|
|
|
|
async title() {
|
2019-01-23 04:24:14 +00:00
|
|
|
return this._secondaryWorld.title();
|
2017-07-27 22:17:43 +00:00
|
|
|
}
|
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2018-04-07 01:20:48 +00:00
|
|
|
* @param {!Protocol.Page.Frame} framePayload
|
2017-06-21 20:58:49 +00:00
|
|
|
*/
|
2017-07-25 21:58:10 +00:00
|
|
|
_navigated(framePayload) {
|
2017-06-21 20:51:06 +00:00
|
|
|
this._name = framePayload.name;
|
2018-04-28 04:15:40 +00:00
|
|
|
// TODO(lushnikov): remove this once requestInterception has loaderId exposed.
|
|
|
|
this._navigationURL = framePayload.url;
|
2017-06-21 20:51:06 +00:00
|
|
|
this._url = framePayload.url;
|
2017-11-10 23:33:14 +00:00
|
|
|
}
|
|
|
|
|
2018-04-10 06:38:20 +00:00
|
|
|
/**
|
|
|
|
* @param {string} url
|
|
|
|
*/
|
|
|
|
_navigatedWithinDocument(url) {
|
|
|
|
this._url = url;
|
|
|
|
}
|
|
|
|
|
2017-11-10 23:33:14 +00:00
|
|
|
/**
|
|
|
|
* @param {string} loaderId
|
|
|
|
* @param {string} name
|
|
|
|
*/
|
|
|
|
_onLifecycleEvent(loaderId, name) {
|
|
|
|
if (name === 'init') {
|
|
|
|
this._loaderId = loaderId;
|
|
|
|
this._lifecycleEvents.clear();
|
|
|
|
}
|
|
|
|
this._lifecycleEvents.add(name);
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
|
|
|
|
2018-04-10 22:59:41 +00:00
|
|
|
_onLoadingStopped() {
|
|
|
|
this._lifecycleEvents.add('DOMContentLoaded');
|
|
|
|
this._lifecycleEvents.add('load');
|
|
|
|
}
|
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
_detach() {
|
|
|
|
this._detached = true;
|
2019-01-16 01:21:23 +00:00
|
|
|
this._mainWorld._detach();
|
2019-01-22 22:55:33 +00:00
|
|
|
this._secondaryWorld._detach();
|
2017-06-21 20:51:06 +00:00
|
|
|
if (this._parentFrame)
|
|
|
|
this._parentFrame._childFrames.delete(this);
|
|
|
|
this._parentFrame = null;
|
|
|
|
}
|
2017-06-17 21:27:51 +00:00
|
|
|
}
|
|
|
|
|
2018-11-20 23:32:46 +00:00
|
|
|
function assertNoLegacyNavigationOptions(options) {
|
|
|
|
assert(options['networkIdleTimeout'] === undefined, 'ERROR: networkIdleTimeout option is no longer supported.');
|
|
|
|
assert(options['networkIdleInflight'] === undefined, 'ERROR: networkIdleInflight option is no longer supported.');
|
|
|
|
assert(options.waitUntil !== 'networkidle', 'ERROR: "networkidle" option is no longer supported. Use "networkidle2" instead');
|
|
|
|
}
|
|
|
|
|
2017-10-10 05:31:40 +00:00
|
|
|
module.exports = {FrameManager, Frame};
|