2017-06-20 23:41:27 +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-09-11 23:21:51 +00:00
|
|
|
const {helper} = require('./helper');
|
2017-11-10 23:33:14 +00:00
|
|
|
const {FrameManager} = require('./FrameManager');
|
2017-07-19 01:54:24 +00:00
|
|
|
|
|
|
|
class NavigatorWatcher {
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-11-10 23:33:14 +00:00
|
|
|
* @param {!FrameManager} frameManager
|
|
|
|
* @param {!Puppeteer.Frame} frame
|
2017-06-21 20:58:49 +00:00
|
|
|
* @param {!Object=} options
|
|
|
|
*/
|
2017-11-10 23:33:14 +00:00
|
|
|
constructor(frameManager, frame, options = {}) {
|
2017-10-24 01:10:59 +00:00
|
|
|
console.assert(options.networkIdleTimeout === undefined, 'ERROR: networkIdleTimeout option is no longer supported.');
|
|
|
|
console.assert(options.networkIdleInflight === undefined, 'ERROR: networkIdleInflight option is no longer supported.');
|
|
|
|
console.assert(options.waitUntil !== 'networkidle', 'ERROR: "networkidle" option is no longer supported. Use "networkidle2" instead');
|
2017-10-24 17:05:15 +00:00
|
|
|
let waitUntil = ['load'];
|
|
|
|
if (Array.isArray(options.waitUntil))
|
|
|
|
waitUntil = options.waitUntil.slice();
|
|
|
|
else if (typeof options.waitUntil === 'string')
|
|
|
|
waitUntil = [options.waitUntil];
|
2017-11-10 23:33:14 +00:00
|
|
|
this._expectedLifecycle = waitUntil.map(value => {
|
|
|
|
const protocolEvent = puppeteerToProtocolLifecycle[value];
|
|
|
|
console.assert(protocolEvent, 'Unknown value for options.waitUntil: ' + value);
|
|
|
|
return protocolEvent;
|
|
|
|
});
|
|
|
|
|
|
|
|
this._frameManager = frameManager;
|
|
|
|
this._frame = frame;
|
|
|
|
this._initialLoaderId = frame._loaderId;
|
|
|
|
this._timeout = typeof options.timeout === 'number' ? options.timeout : 30000;
|
|
|
|
this._eventListeners = [
|
2017-11-20 22:47:11 +00:00
|
|
|
helper.addEventListener(this._frameManager, FrameManager.Events.LifecycleEvent, this._checkLifecycleComplete.bind(this)),
|
|
|
|
helper.addEventListener(this._frameManager, FrameManager.Events.FrameDetached, this._checkLifecycleComplete.bind(this))
|
2017-11-10 23:33:14 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
const lifecycleCompletePromise = new Promise(fulfill => {
|
|
|
|
this._lifecycleCompleteCallback = fulfill;
|
|
|
|
});
|
|
|
|
this._navigationPromise = Promise.race([
|
|
|
|
this._createTimeoutPromise(),
|
|
|
|
lifecycleCompletePromise
|
|
|
|
]).then(error => {
|
|
|
|
this._cleanup();
|
|
|
|
return error;
|
|
|
|
});
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-20 23:41:27 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-11-10 23:33:14 +00:00
|
|
|
* @return {?Promise<?Error>}
|
2017-06-21 20:58:49 +00:00
|
|
|
*/
|
2017-11-10 23:33:14 +00:00
|
|
|
_createTimeoutPromise() {
|
|
|
|
if (!this._timeout)
|
2017-11-21 05:21:25 +00:00
|
|
|
return new Promise(() => {});
|
2017-11-10 23:33:14 +00:00
|
|
|
const errorMessage = 'Navigation Timeout Exceeded: ' + this._timeout + 'ms exceeded';
|
|
|
|
return new Promise(fulfill => this._maximumTimer = setTimeout(fulfill, this._timeout))
|
|
|
|
.then(() => new Error(errorMessage));
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-20 23:41:27 +00:00
|
|
|
|
2017-06-28 21:39:37 +00:00
|
|
|
/**
|
2017-11-10 23:33:14 +00:00
|
|
|
* @return {!Promise<?Error>}
|
2017-06-28 21:39:37 +00:00
|
|
|
*/
|
2017-11-10 23:33:14 +00:00
|
|
|
async navigationPromise() {
|
|
|
|
return this._navigationPromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
_checkLifecycleComplete() {
|
|
|
|
// We expect navigation to commit.
|
|
|
|
if (this._frame._loaderId === this._initialLoaderId)
|
2017-10-24 01:10:59 +00:00
|
|
|
return;
|
2017-11-10 23:33:14 +00:00
|
|
|
if (!checkLifecycle(this._frame, this._expectedLifecycle))
|
2017-10-24 01:10:59 +00:00
|
|
|
return;
|
2017-11-10 23:33:14 +00:00
|
|
|
this._lifecycleCompleteCallback();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Puppeteer.Frame} frame
|
|
|
|
* @param {!Array<string>} expectedLifecycle
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
function checkLifecycle(frame, expectedLifecycle) {
|
|
|
|
for (const event of expectedLifecycle) {
|
|
|
|
if (!frame._lifecycleEvents.has(event))
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-10 23:44:14 +00:00
|
|
|
for (const child of frame.childFrames()) {
|
|
|
|
if (!checkLifecycle(child, expectedLifecycle))
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-10 23:33:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-06-28 21:39:37 +00:00
|
|
|
}
|
|
|
|
|
2017-10-24 01:10:59 +00:00
|
|
|
cancel() {
|
|
|
|
this._cleanup();
|
2017-06-28 21:39:37 +00:00
|
|
|
}
|
|
|
|
|
2017-07-07 22:43:17 +00:00
|
|
|
_cleanup() {
|
2017-07-23 00:03:58 +00:00
|
|
|
helper.removeEventListeners(this._eventListeners);
|
2017-11-10 23:33:14 +00:00
|
|
|
this._lifecycleCompleteCallback(new Error('Navigation failed'));
|
2017-07-07 22:43:17 +00:00
|
|
|
clearTimeout(this._maximumTimer);
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-20 23:41:27 +00:00
|
|
|
}
|
|
|
|
|
2017-11-10 23:33:14 +00:00
|
|
|
const puppeteerToProtocolLifecycle = {
|
2017-10-24 01:10:59 +00:00
|
|
|
'load': 'load',
|
2017-11-10 23:33:14 +00:00
|
|
|
'domcontentloaded': 'DOMContentLoaded',
|
|
|
|
'networkidle0': 'networkIdle',
|
|
|
|
'networkidle2': 'networkAlmostIdle',
|
2017-10-24 01:10:59 +00:00
|
|
|
};
|
|
|
|
|
2017-07-19 01:54:24 +00:00
|
|
|
module.exports = NavigatorWatcher;
|