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-06-22 20:38:10 +00:00
|
|
|
let EventEmitter = require('events');
|
2017-06-27 19:40:46 +00:00
|
|
|
let helper = require('./helper');
|
2017-06-17 21:27:51 +00:00
|
|
|
|
|
|
|
class FrameManager extends EventEmitter {
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-06-21 20:58:49 +00:00
|
|
|
* @param {!Connection} client
|
2017-07-22 03:29:31 +00:00
|
|
|
* @param {!Mouse} mouse
|
2017-06-29 06:09:28 +00:00
|
|
|
* @return {!Promise<!FrameManager>}
|
2017-06-21 20:58:49 +00:00
|
|
|
*/
|
2017-07-22 03:29:31 +00:00
|
|
|
static async create(client, mouse) {
|
2017-06-22 20:38:10 +00:00
|
|
|
let mainFramePayload = await client.send('Page.getResourceTree');
|
2017-07-22 03:29:31 +00:00
|
|
|
return new FrameManager(client, mainFramePayload.frameTree, mouse);
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-21 20:36:04 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-06-21 20:58:49 +00:00
|
|
|
* @param {!Connection} client
|
|
|
|
* @param {!Object} frameTree
|
2017-07-22 03:29:31 +00:00
|
|
|
* @param {!Mouse} mouse
|
2017-06-21 20:58:49 +00:00
|
|
|
*/
|
2017-07-22 03:29:31 +00:00
|
|
|
constructor(client, frameTree, mouse) {
|
2017-06-21 20:51:06 +00:00
|
|
|
super();
|
|
|
|
this._client = client;
|
2017-07-22 03:29:31 +00:00
|
|
|
this._mouse = mouse;
|
2017-06-21 20:51:06 +00:00
|
|
|
/** @type {!Map<string, !Frame>} */
|
|
|
|
this._frames = new Map();
|
|
|
|
this._mainFrame = this._addFramesRecursively(null, frameTree);
|
|
|
|
|
2017-06-27 19:40:46 +00:00
|
|
|
/** @type {!Map<string, string>} */
|
|
|
|
this._frameIdToExecutionContextId = new Map();
|
|
|
|
|
|
|
|
this._client.on('Page.frameAttached', event => this._onFrameAttached(event.frameId, event.parentFrameId));
|
|
|
|
this._client.on('Page.frameNavigated', event => this._onFrameNavigated(event.frame));
|
|
|
|
this._client.on('Page.frameDetached', event => this._onFrameDetached(event.frameId));
|
|
|
|
this._client.on('Runtime.executionContextCreated', event => this._onExecutionContextCreated(event.context));
|
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
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-06-21 20:58:49 +00:00
|
|
|
* @param {string} frameId
|
|
|
|
* @param {?string} parentFrameId
|
|
|
|
* @return {?Frame}
|
|
|
|
*/
|
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;
|
|
|
|
|
|
|
|
if (!parentFrameId) {
|
|
|
|
// Navigation to the new backend process.
|
|
|
|
this._navigateFrame(this._mainFrame, frameId, null);
|
|
|
|
return;
|
|
|
|
}
|
2017-06-22 20:38:10 +00:00
|
|
|
let parentFrame = this._frames.get(parentFrameId);
|
2017-06-27 19:40:46 +00:00
|
|
|
let frame = new Frame(this, parentFrame, frameId, null);
|
2017-06-21 20:51:06 +00:00
|
|
|
this._frames.set(frame._id, frame);
|
|
|
|
this.emit(FrameManager.Events.FrameAttached, frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-21 20:58:49 +00:00
|
|
|
* @param {!Object} framePayload
|
|
|
|
*/
|
2017-06-27 19:40:46 +00:00
|
|
|
_onFrameNavigated(framePayload) {
|
2017-06-22 20:38:10 +00:00
|
|
|
let frame = this._frames.get(framePayload.id);
|
2017-06-21 20:51:06 +00:00
|
|
|
if (!frame) {
|
|
|
|
// Simulate missed "frameAttached" for a main frame navigation to the new backend process.
|
|
|
|
console.assert(!framePayload.parentId, 'Main frame shouldn\'t have parent frame id.');
|
|
|
|
frame = this._mainFrame;
|
2017-06-21 20:36:04 +00:00
|
|
|
}
|
2017-06-21 20:51:06 +00:00
|
|
|
this._navigateFrame(frame, framePayload.id, framePayload);
|
|
|
|
}
|
2017-06-21 20:36:04 +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-06-22 20:38:10 +00:00
|
|
|
let 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-06-27 19:40:46 +00:00
|
|
|
_onExecutionContextCreated(context) {
|
|
|
|
if (context.auxData && context.auxData.isDefault && context.auxData.frameId)
|
|
|
|
this._frameIdToExecutionContextId.set(context.auxData.frameId, context.id);
|
|
|
|
}
|
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-06-21 20:58:49 +00:00
|
|
|
* @param {!Frame} frame
|
|
|
|
* @param {string} newFrameId
|
|
|
|
* @param {?Object} newFramePayload
|
|
|
|
*/
|
2017-06-21 20:51:06 +00:00
|
|
|
_navigateFrame(frame, newFrameId, newFramePayload) {
|
|
|
|
// Detach all child frames first.
|
2017-06-22 20:38:10 +00:00
|
|
|
for (let child of frame.childFrames())
|
2017-06-21 20:51:06 +00:00
|
|
|
this._removeFramesRecursively(child);
|
|
|
|
this._frames.delete(frame._id, frame);
|
2017-07-12 04:52:09 +00:00
|
|
|
this._frameIdToExecutionContextId.delete(frame._id);
|
2017-06-21 20:51:06 +00:00
|
|
|
frame._id = newFrameId;
|
|
|
|
frame._adoptPayload(newFramePayload);
|
|
|
|
this._frames.set(newFrameId, frame);
|
|
|
|
this.emit(FrameManager.Events.FrameNavigated, frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-21 20:58:49 +00:00
|
|
|
* @param {?Frame} parentFrame
|
|
|
|
* @param {!Object} frameTreePayload
|
|
|
|
* @return {!Frame}
|
|
|
|
*/
|
2017-06-21 20:51:06 +00:00
|
|
|
_addFramesRecursively(parentFrame, frameTreePayload) {
|
2017-06-22 20:38:10 +00:00
|
|
|
let framePayload = frameTreePayload.frame;
|
2017-06-27 19:40:46 +00:00
|
|
|
let frame = new Frame(this, parentFrame, framePayload.id, framePayload);
|
2017-06-21 20:51:06 +00:00
|
|
|
this._frames.set(frame._id, frame);
|
|
|
|
|
2017-06-22 20:38:10 +00:00
|
|
|
for (let i = 0; frameTreePayload.childFrames && i < frameTreePayload.childFrames.length; ++i)
|
2017-06-21 20:51:06 +00:00
|
|
|
this._addFramesRecursively(frame, frameTreePayload.childFrames[i]);
|
|
|
|
return frame;
|
|
|
|
}
|
2017-06-21 20:36:04 +00:00
|
|
|
|
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-06-22 20:38:10 +00:00
|
|
|
for (let child of frame.childFrames())
|
2017-06-21 20:51:06 +00:00
|
|
|
this._removeFramesRecursively(child);
|
|
|
|
frame._detach();
|
|
|
|
this._frames.delete(frame._id);
|
2017-07-12 04:52:09 +00:00
|
|
|
this._frameIdToExecutionContextId.delete(frame._id);
|
2017-06-21 20:51:06 +00:00
|
|
|
this.emit(FrameManager.Events.FrameDetached, frame);
|
|
|
|
}
|
2017-06-27 19:40:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Frame} frame
|
2017-07-18 01:56:56 +00:00
|
|
|
* @param {string} expression
|
2017-06-27 19:40:46 +00:00
|
|
|
* @return {!Promise<(!Object|undefined)>}
|
|
|
|
*/
|
2017-07-18 01:56:56 +00:00
|
|
|
async _evaluateOnFrame(frame, expression) {
|
2017-06-27 19:40:46 +00:00
|
|
|
let contextId = undefined;
|
|
|
|
if (!frame.isMainFrame()) {
|
|
|
|
contextId = this._frameIdToExecutionContextId.get(frame._id);
|
|
|
|
console.assert(contextId, 'Frame does not have default context to evaluate in!');
|
|
|
|
}
|
2017-07-18 01:56:56 +00:00
|
|
|
expression = `Promise.resolve(${expression})`;
|
2017-07-06 17:41:01 +00:00
|
|
|
let { exceptionDetails, result: remoteObject } = await this._client.send('Runtime.evaluate', { expression, contextId, returnByValue: false, awaitPromise: true });
|
2017-07-21 18:57:25 +00:00
|
|
|
if (exceptionDetails)
|
|
|
|
throw new Error('Evaluation failed: ' + helper.getExceptionMessage(exceptionDetails));
|
2017-07-17 08:57:37 +00:00
|
|
|
return await helper.serializeRemoteObject(this._client, remoteObject);
|
2017-06-27 19:40:46 +00:00
|
|
|
}
|
2017-06-17 21:27:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @enum {string} */
|
|
|
|
FrameManager.Events = {
|
2017-06-21 20:51:06 +00:00
|
|
|
FrameAttached: 'frameattached',
|
|
|
|
FrameNavigated: 'framenavigated',
|
|
|
|
FrameDetached: 'framedetached'
|
2017-06-17 21:27:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @unrestricted
|
|
|
|
*/
|
|
|
|
class Frame {
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-06-27 19:40:46 +00:00
|
|
|
* @param {!FrameManager} frameManager
|
2017-06-21 20:58:49 +00:00
|
|
|
* @param {?Frame} parentFrame
|
|
|
|
* @param {string} frameId
|
|
|
|
* @param {?Object} payload
|
|
|
|
*/
|
2017-06-27 19:40:46 +00:00
|
|
|
constructor(frameManager, parentFrame, frameId, payload) {
|
|
|
|
this._frameManager = frameManager;
|
2017-06-21 20:51:06 +00:00
|
|
|
this._parentFrame = parentFrame;
|
|
|
|
this._url = '';
|
|
|
|
this._id = frameId;
|
2017-07-24 16:58:51 +00:00
|
|
|
/** @type {!Set<!WaitTask>} */
|
|
|
|
this._waitTasks = new Set();
|
2017-07-20 02:04:51 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
this._adoptPayload(payload);
|
|
|
|
|
|
|
|
/** @type {!Set<!Frame>} */
|
|
|
|
this._childFrames = new Set();
|
|
|
|
if (this._parentFrame)
|
|
|
|
this._parentFrame._childFrames.add(this);
|
|
|
|
}
|
|
|
|
|
2017-06-27 19:40:46 +00:00
|
|
|
/**
|
2017-07-14 20:51:22 +00:00
|
|
|
* @param {function()} pageFunction
|
2017-06-27 19:40:46 +00:00
|
|
|
* @param {!Array<*>} args
|
|
|
|
* @return {!Promise<(!Object|undefined)>}
|
|
|
|
*/
|
2017-07-14 20:51:22 +00:00
|
|
|
async evaluate(pageFunction, ...args) {
|
2017-07-18 01:56:56 +00:00
|
|
|
return this._frameManager._evaluateOnFrame(this, helper.evaluationString(pageFunction, ...args));
|
2017-06-27 19:40:46 +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
|
|
|
isMainFrame() {
|
|
|
|
return !this._detached && !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 {boolean}
|
|
|
|
*/
|
2017-06-21 20:51:06 +00:00
|
|
|
isDetached() {
|
|
|
|
return this._detached;
|
|
|
|
}
|
2017-06-21 20:36:04 +00:00
|
|
|
|
2017-07-21 19:41:49 +00:00
|
|
|
/**
|
|
|
|
* @param {(string|number)} target
|
|
|
|
* @param {!Object=} options
|
|
|
|
* @return {!Promise}
|
|
|
|
*/
|
|
|
|
waitFor(target, options = {}) {
|
|
|
|
if (typeof target === 'string' || target instanceof String)
|
|
|
|
return this.waitForSelector(target, options);
|
|
|
|
if (typeof target === 'number' || target instanceof Number)
|
|
|
|
return new Promise(fulfill => setTimeout(fulfill, target));
|
|
|
|
return Promise.reject(new Error('Unsupported target type: ' + (typeof target)));
|
|
|
|
}
|
|
|
|
|
2017-07-07 22:39:02 +00:00
|
|
|
/**
|
|
|
|
* @param {string} selector
|
2017-07-21 07:58:38 +00:00
|
|
|
* @param {!Object=} options
|
2017-07-20 02:04:51 +00:00
|
|
|
* @return {!Promise}
|
2017-07-07 22:39:02 +00:00
|
|
|
*/
|
2017-07-21 19:41:49 +00:00
|
|
|
waitForSelector(selector, options = {}) {
|
2017-07-21 18:46:57 +00:00
|
|
|
const timeout = options.timeout || 30000;
|
2017-07-24 16:58:51 +00:00
|
|
|
const waitForVisible = !!options.visible;
|
|
|
|
const pageScript = helper.evaluationString(waitForSelectorPageFunction, selector, waitForVisible, timeout);
|
|
|
|
const waitTask = new WaitTask(this._frameManager, this, pageScript, timeout);
|
2017-07-20 02:04:51 +00:00
|
|
|
|
2017-07-24 16:58:51 +00:00
|
|
|
this._waitTasks.add(waitTask);
|
|
|
|
let cleanup = () => this._waitTasks.delete(waitTask);
|
|
|
|
waitTask.promise.then(cleanup, cleanup);
|
|
|
|
return waitTask.promise;
|
2017-07-07 22:39:02 +00:00
|
|
|
}
|
|
|
|
|
2017-07-18 01:56:56 +00:00
|
|
|
/**
|
|
|
|
* @param {string} selector
|
|
|
|
* @param {function(!Element):T} pageFunction
|
|
|
|
* @param {!Array<*>} args
|
|
|
|
* @return {!Promise<?T>}
|
|
|
|
*/
|
|
|
|
async $(selector, pageFunction, ...args) {
|
|
|
|
let argsString = ['node'].concat(args.map(x => JSON.stringify(x))).join(',');
|
|
|
|
let expression = `(()=>{
|
|
|
|
let node = document.querySelector(${JSON.stringify(selector)});
|
|
|
|
if (!node)
|
|
|
|
return null;
|
|
|
|
return (${pageFunction})(${argsString});
|
|
|
|
})()`;
|
|
|
|
return this._frameManager._evaluateOnFrame(this, expression);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} selector
|
|
|
|
* @param {function(!Element):T} pageFunction
|
|
|
|
* @param {!Array<*>} args
|
|
|
|
* @return {!Promise<!Array<T>>}
|
|
|
|
*/
|
|
|
|
async $$(selector, pageFunction, ...args) {
|
|
|
|
let argsString = ['node, index'].concat(args.map(x => JSON.stringify(x))).join(',');
|
|
|
|
let expression = `(()=>{
|
|
|
|
let nodes = document.querySelectorAll(${JSON.stringify(selector)});
|
|
|
|
return Array.prototype.map.call(nodes, (node, index) => (${pageFunction})(${argsString}));
|
|
|
|
})()`;
|
|
|
|
return this._frameManager._evaluateOnFrame(this, expression);
|
|
|
|
}
|
|
|
|
|
2017-07-22 03:29:31 +00:00
|
|
|
/**
|
|
|
|
* @param {string} selector
|
|
|
|
* @return {!Promise}
|
|
|
|
*/
|
|
|
|
async hover(selector) {
|
|
|
|
let center = await this.evaluate(selector => {
|
|
|
|
let element = document.querySelector(selector);
|
|
|
|
if (!element)
|
|
|
|
return null;
|
|
|
|
element.scrollIntoViewIfNeeded();
|
|
|
|
let rect = element.getBoundingClientRect();
|
|
|
|
return {
|
2017-07-24 23:14:32 +00:00
|
|
|
x: (Math.max(rect.left, 0) + Math.min(rect.right, window.innerWidth)) / 2,
|
|
|
|
y: (Math.max(rect.top, 0) + Math.min(rect.bottom, window.innerHeight)) / 2
|
2017-07-22 03:29:31 +00:00
|
|
|
};
|
|
|
|
}, selector);
|
|
|
|
if (!center)
|
|
|
|
throw new Error('No node found for selector: ' + selector);
|
|
|
|
await this._frameManager._mouse.move(center.x, center.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} selector
|
|
|
|
* @param {!Object=} options
|
|
|
|
* @return {!Promise}
|
|
|
|
*/
|
|
|
|
async click(selector, options) {
|
|
|
|
await this.hover(selector);
|
|
|
|
await this._frameManager._mouse.press(options);
|
|
|
|
// This is a hack for now, to make clicking less race-prone
|
|
|
|
await this.evaluate(() => new Promise(f => requestAnimationFrame(f)));
|
|
|
|
}
|
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-06-21 20:58:49 +00:00
|
|
|
* @param {?Object} framePayload
|
|
|
|
*/
|
2017-06-21 20:51:06 +00:00
|
|
|
_adoptPayload(framePayload) {
|
|
|
|
framePayload = framePayload || {
|
|
|
|
name: '',
|
|
|
|
url: '',
|
|
|
|
};
|
|
|
|
this._name = framePayload.name;
|
|
|
|
this._url = framePayload.url;
|
2017-07-24 16:58:51 +00:00
|
|
|
for (let waitTask of this._waitTasks)
|
|
|
|
waitTask.run();
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_detach() {
|
2017-07-24 16:58:51 +00:00
|
|
|
for (let waitTask of this._waitTasks)
|
|
|
|
waitTask.terminate(new Error('waitForSelector failed: frame got detached.'));
|
2017-06-21 20:51:06 +00:00
|
|
|
this._detached = true;
|
|
|
|
if (this._parentFrame)
|
|
|
|
this._parentFrame._childFrames.delete(this);
|
|
|
|
this._parentFrame = null;
|
|
|
|
}
|
2017-06-17 21:27:51 +00:00
|
|
|
}
|
2017-07-19 03:53:00 +00:00
|
|
|
helper.tracePublicAPI(Frame);
|
2017-06-17 21:27:51 +00:00
|
|
|
|
2017-07-24 16:58:51 +00:00
|
|
|
class WaitTask {
|
2017-07-20 02:04:51 +00:00
|
|
|
/**
|
2017-07-24 16:58:51 +00:00
|
|
|
* @param {!FrameManager} frameManager
|
|
|
|
* @param {!Frame} frame
|
|
|
|
* @param {string} pageScript
|
|
|
|
* @param {number} timeout
|
2017-07-20 02:04:51 +00:00
|
|
|
*/
|
2017-07-24 16:58:51 +00:00
|
|
|
constructor(frameManager, frame, pageScript, timeout) {
|
|
|
|
this._frameManager = frameManager;
|
|
|
|
this._frame = frame;
|
|
|
|
this._pageScript = pageScript;
|
|
|
|
this._runningTask = null;
|
2017-07-20 02:04:51 +00:00
|
|
|
this.promise = new Promise((resolve, reject) => {
|
|
|
|
this._resolve = resolve;
|
|
|
|
this._reject = reject;
|
|
|
|
});
|
2017-07-24 16:58:51 +00:00
|
|
|
// Since page navigation requires us to re-install the pageScript, we should track
|
|
|
|
// timeout on our end.
|
|
|
|
this._timeoutTimer = setTimeout(() => this.terminate(new Error(`waiting failed: timeout ${timeout}ms exceeded`)), timeout);
|
|
|
|
this.run();
|
2017-07-20 02:04:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Error} error
|
|
|
|
*/
|
|
|
|
terminate(error) {
|
|
|
|
this._reject(error);
|
2017-07-24 16:58:51 +00:00
|
|
|
this._cleanup();
|
2017-07-20 02:04:51 +00:00
|
|
|
}
|
|
|
|
|
2017-07-24 16:58:51 +00:00
|
|
|
run() {
|
|
|
|
let runningTask = this._frameManager._evaluateOnFrame(this._frame, this._pageScript).then(finish.bind(this), finish.bind(this, false));
|
|
|
|
this._runningTask = runningTask;
|
2017-07-20 02:04:51 +00:00
|
|
|
|
|
|
|
/**
|
2017-07-24 16:58:51 +00:00
|
|
|
* @param {boolean} success
|
|
|
|
* @param {?Error=} error
|
2017-07-20 02:04:51 +00:00
|
|
|
*/
|
2017-07-24 16:58:51 +00:00
|
|
|
function finish(success, error) {
|
|
|
|
if (runningTask !== this._runningTask)
|
|
|
|
return;
|
|
|
|
// Ignore timeouts in pageScript - we track timeouts ourselves.
|
|
|
|
if (!success && !error)
|
2017-07-20 02:04:51 +00:00
|
|
|
return;
|
|
|
|
if (error)
|
|
|
|
this._reject(error);
|
|
|
|
else
|
|
|
|
this._resolve();
|
2017-07-24 16:58:51 +00:00
|
|
|
this._cleanup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_cleanup() {
|
|
|
|
clearTimeout(this._timeoutTimer);
|
|
|
|
this._runningTask = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} selector
|
|
|
|
* @param {boolean} waitForVisible
|
|
|
|
* @param {number} timeout
|
|
|
|
* @return {!Promise<boolean>}
|
|
|
|
*/
|
|
|
|
function waitForSelectorPageFunction(selector, visible, timeout) {
|
|
|
|
const resultPromise = visible ? waitForVisible(selector) : waitInDOM(selector);
|
|
|
|
const timeoutPromise = new Promise(fulfill => setTimeout(fulfill, timeout));
|
|
|
|
return Promise.race([
|
|
|
|
resultPromise.then(() => true),
|
|
|
|
timeoutPromise.then(() => false)
|
|
|
|
]);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} selector
|
|
|
|
* @return {!Promise<!Element>}
|
|
|
|
*/
|
|
|
|
function waitInDOM(selector) {
|
|
|
|
let node = document.querySelector(selector);
|
|
|
|
if (node)
|
|
|
|
return Promise.resolve(node);
|
|
|
|
|
|
|
|
let fulfill;
|
|
|
|
const result = new Promise(x => fulfill = x);
|
|
|
|
const observer = new MutationObserver(mutations => {
|
|
|
|
const node = document.querySelector(selector);
|
|
|
|
if (node) {
|
|
|
|
observer.disconnect();
|
|
|
|
fulfill(node);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
observer.observe(document, {
|
|
|
|
childList: true,
|
|
|
|
subtree: true
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} selector
|
|
|
|
* @return {!Promise<!Element>}
|
|
|
|
*/
|
|
|
|
async function waitForVisible(selector) {
|
|
|
|
let fulfill;
|
|
|
|
const result = new Promise(x => fulfill = x);
|
|
|
|
onRaf();
|
|
|
|
return result;
|
|
|
|
|
|
|
|
async function onRaf() {
|
|
|
|
const node = await waitInDOM(selector);
|
|
|
|
const style = window.getComputedStyle(node);
|
|
|
|
if (style.display === 'none' || style.visibility === 'hidden') {
|
|
|
|
requestAnimationFrame(onRaf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fulfill(node);
|
2017-07-20 02:04:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-17 21:27:51 +00:00
|
|
|
module.exports = FrameManager;
|