fix(browser): browser closing/disconnecting should abort navigations (#3245)

Fixes #2721.
This commit is contained in:
Andrey Lushnikov
2018-09-14 19:44:54 +01:00
committed by GitHub
parent f0beabd22a
commit d547b9d24a
6 changed files with 72 additions and 25 deletions

View File

@@ -17,15 +17,17 @@
const {helper, assert} = require('./helper');
const {FrameManager} = require('./FrameManager');
const {TimeoutError} = require('./Errors');
const {Connection} = require('./Connection');
class NavigatorWatcher {
/**
* @param {!Puppeteer.CDPSession} client
* @param {!FrameManager} frameManager
* @param {!Puppeteer.Frame} frame
* @param {number} timeout
* @param {!Object=} options
*/
constructor(frameManager, frame, timeout, options = {}) {
constructor(client, frameManager, frame, timeout, 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');
@@ -46,6 +48,7 @@ class NavigatorWatcher {
this._timeout = timeout;
this._hasSameDocumentNavigation = false;
this._eventListeners = [
helper.addEventListener(Connection.fromSession(client), Connection.Events.Disconnected, () => this._terminate(new Error('Navigation failed because browser has disconnected!'))),
helper.addEventListener(this._frameManager, FrameManager.Events.LifecycleEvent, this._checkLifecycleComplete.bind(this)),
helper.addEventListener(this._frameManager, FrameManager.Events.FrameNavigatedWithinDocument, this._navigatedWithinDocument.bind(this)),
helper.addEventListener(this._frameManager, FrameManager.Events.FrameDetached, this._checkLifecycleComplete.bind(this))
@@ -60,6 +63,16 @@ class NavigatorWatcher {
});
this._timeoutPromise = this._createTimeoutPromise();
this._terminationPromise = new Promise(fulfill => {
this._terminationCallback = fulfill;
});
}
/**
* @param {!Error} error
*/
_terminate(error) {
this._terminationCallback.call(null, error);
}
/**
@@ -79,8 +92,8 @@ class NavigatorWatcher {
/**
* @return {!Promise<?Error>}
*/
timeoutPromise() {
return this._timeoutPromise;
timeoutOrTerminationPromise() {
return Promise.race([this._timeoutPromise, this._terminationPromise]);
}
/**