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-07-19 01:54:24 +00:00
|
|
|
|
|
|
|
class NavigatorWatcher {
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-10-10 05:31:40 +00:00
|
|
|
* @param {!Puppeteer.Session} client
|
2017-10-24 01:10:59 +00:00
|
|
|
* @param {string} frameId
|
2017-06-21 20:58:49 +00:00
|
|
|
* @param {!Object=} options
|
|
|
|
*/
|
2017-11-01 20:28:00 +00:00
|
|
|
constructor(client, frameId, 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-06-21 20:51:06 +00:00
|
|
|
this._client = client;
|
2017-10-24 01:10:59 +00:00
|
|
|
this._frameId = frameId;
|
2017-10-24 17:05:15 +00:00
|
|
|
this._timeout = typeof options.timeout === 'number' ? options.timeout : 30000;
|
|
|
|
let waitUntil = ['load'];
|
|
|
|
if (Array.isArray(options.waitUntil))
|
|
|
|
waitUntil = options.waitUntil.slice();
|
|
|
|
else if (typeof options.waitUntil === 'string')
|
|
|
|
waitUntil = [options.waitUntil];
|
|
|
|
for (const value of waitUntil) {
|
|
|
|
const isAllowedValue = value === 'networkidle0' || value === 'networkidle2' || value === 'load' || value === 'domcontentloaded';
|
|
|
|
console.assert(isAllowedValue, 'Unknown value for options.waitUntil: ' + value);
|
|
|
|
}
|
|
|
|
this._pendingEvents = new Set(waitUntil);
|
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-09-11 23:43:37 +00:00
|
|
|
* @return {!Promise<?Error>}
|
2017-06-21 20:58:49 +00:00
|
|
|
*/
|
2017-07-19 01:54:24 +00:00
|
|
|
async waitForNavigation() {
|
2017-08-01 22:17:57 +00:00
|
|
|
this._eventListeners = [];
|
2017-07-23 00:03:58 +00:00
|
|
|
|
2017-09-30 07:29:38 +00:00
|
|
|
const navigationPromises = [];
|
|
|
|
if (this._timeout) {
|
|
|
|
const watchdog = new Promise(fulfill => this._maximumTimer = setTimeout(fulfill, this._timeout))
|
|
|
|
.then(() => 'Navigation Timeout Exceeded: ' + this._timeout + 'ms exceeded');
|
|
|
|
navigationPromises.push(watchdog);
|
|
|
|
}
|
2017-08-01 22:17:57 +00:00
|
|
|
|
2017-10-24 01:10:59 +00:00
|
|
|
this._eventListeners.push(helper.addEventListener(this._client, 'Page.lifecycleEvent', this._onLifecycleEvent.bind(this)));
|
|
|
|
const pendingEventsFired = new Promise(fulfill => this._pendingEventsCallback = fulfill);
|
|
|
|
navigationPromises.push(pendingEventsFired);
|
2017-08-01 22:17:57 +00:00
|
|
|
|
|
|
|
const error = await Promise.race(navigationPromises);
|
|
|
|
this._cleanup();
|
2017-09-11 23:43:37 +00:00
|
|
|
return error ? new Error(error) : null;
|
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-10-24 01:10:59 +00:00
|
|
|
* @param {!{frameId: string, name: string}} event
|
2017-06-28 21:39:37 +00:00
|
|
|
*/
|
2017-10-24 01:10:59 +00:00
|
|
|
_onLifecycleEvent(event) {
|
|
|
|
if (event.frameId !== this._frameId)
|
|
|
|
return;
|
|
|
|
const pptrName = protocolLifecycleToPuppeteer[event.name];
|
|
|
|
if (!pptrName)
|
|
|
|
return;
|
|
|
|
this._pendingEvents.delete(pptrName);
|
|
|
|
if (this._pendingEvents.size === 0)
|
|
|
|
this._pendingEventsCallback();
|
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-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-10-24 01:10:59 +00:00
|
|
|
const protocolLifecycleToPuppeteer = {
|
|
|
|
'load': 'load',
|
2017-10-24 07:35:18 +00:00
|
|
|
'DOMContentLoaded': 'domcontentloaded',
|
2017-10-24 01:10:59 +00:00
|
|
|
'networkIdle': 'networkidle0',
|
2017-10-24 07:35:18 +00:00
|
|
|
'networkAlmostIdle': 'networkidle2',
|
2017-10-24 01:10:59 +00:00
|
|
|
};
|
|
|
|
|
2017-07-19 01:54:24 +00:00
|
|
|
module.exports = NavigatorWatcher;
|