mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
b97bddf8e5
This patch unifies logic in response trackign in page.goto and page.waitForNavigation. As a drive-by, we now make sure that we return the right response for the right frame. This will come handy for future frame navigation API. References #2918
185 lines
6.1 KiB
JavaScript
185 lines
6.1 KiB
JavaScript
/**
|
|
* 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.
|
|
*/
|
|
|
|
const {helper, assert} = require('./helper');
|
|
const {FrameManager} = require('./FrameManager');
|
|
const {NetworkManager} = require('./NetworkManager');
|
|
const {TimeoutError} = require('./Errors');
|
|
const {Connection} = require('./Connection');
|
|
|
|
class NavigatorWatcher {
|
|
/**
|
|
* @param {!Puppeteer.CDPSession} client
|
|
* @param {!FrameManager} frameManager
|
|
* @param {!NetworkManager} networkManager
|
|
* @param {!Puppeteer.Frame} frame
|
|
* @param {number} timeout
|
|
* @param {!Object=} options
|
|
*/
|
|
constructor(client, frameManager, networkManager, 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');
|
|
let waitUntil = ['load'];
|
|
if (Array.isArray(options.waitUntil))
|
|
waitUntil = options.waitUntil.slice();
|
|
else if (typeof options.waitUntil === 'string')
|
|
waitUntil = [options.waitUntil];
|
|
this._expectedLifecycle = waitUntil.map(value => {
|
|
const protocolEvent = puppeteerToProtocolLifecycle[value];
|
|
assert(protocolEvent, 'Unknown value for options.waitUntil: ' + value);
|
|
return protocolEvent;
|
|
});
|
|
|
|
this._frameManager = frameManager;
|
|
this._networkManager = networkManager;
|
|
this._frame = frame;
|
|
this._initialLoaderId = frame._loaderId;
|
|
this._timeout = timeout;
|
|
/** @type {?Puppeteer.Request} */
|
|
this._navigationRequest = null;
|
|
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)),
|
|
helper.addEventListener(this._networkManager, NetworkManager.Events.Request, this._onRequest.bind(this)),
|
|
];
|
|
|
|
this._sameDocumentNavigationPromise = new Promise(fulfill => {
|
|
this._sameDocumentNavigationCompleteCallback = fulfill;
|
|
});
|
|
|
|
this._newDocumentNavigationPromise = new Promise(fulfill => {
|
|
this._newDocumentNavigationCompleteCallback = fulfill;
|
|
});
|
|
|
|
this._timeoutPromise = this._createTimeoutPromise();
|
|
this._terminationPromise = new Promise(fulfill => {
|
|
this._terminationCallback = fulfill;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @param {!Puppeteer.Request} request
|
|
*/
|
|
_onRequest(request) {
|
|
if (request.frame() !== this._frame || !request.isNavigationRequest())
|
|
return;
|
|
this._navigationRequest = request;
|
|
}
|
|
|
|
/**
|
|
* @return {?Puppeteer.Response}
|
|
*/
|
|
navigationResponse() {
|
|
return this._navigationRequest ? this._navigationRequest.response() : null;
|
|
}
|
|
|
|
/**
|
|
* @param {!Error} error
|
|
*/
|
|
_terminate(error) {
|
|
this._terminationCallback.call(null, error);
|
|
}
|
|
|
|
/**
|
|
* @return {!Promise<?Error>}
|
|
*/
|
|
sameDocumentNavigationPromise() {
|
|
return this._sameDocumentNavigationPromise;
|
|
}
|
|
|
|
/**
|
|
* @return {!Promise<?Error>}
|
|
*/
|
|
newDocumentNavigationPromise() {
|
|
return this._newDocumentNavigationPromise;
|
|
}
|
|
|
|
/**
|
|
* @return {!Promise<?Error>}
|
|
*/
|
|
timeoutOrTerminationPromise() {
|
|
return Promise.race([this._timeoutPromise, this._terminationPromise]);
|
|
}
|
|
|
|
/**
|
|
* @return {!Promise<?Error>}
|
|
*/
|
|
_createTimeoutPromise() {
|
|
if (!this._timeout)
|
|
return new Promise(() => {});
|
|
const errorMessage = 'Navigation Timeout Exceeded: ' + this._timeout + 'ms exceeded';
|
|
return new Promise(fulfill => this._maximumTimer = setTimeout(fulfill, this._timeout))
|
|
.then(() => new TimeoutError(errorMessage));
|
|
}
|
|
|
|
/**
|
|
* @param {!Puppeteer.Frame} frame
|
|
*/
|
|
_navigatedWithinDocument(frame) {
|
|
if (frame !== this._frame)
|
|
return;
|
|
this._hasSameDocumentNavigation = true;
|
|
this._checkLifecycleComplete();
|
|
}
|
|
|
|
_checkLifecycleComplete() {
|
|
// We expect navigation to commit.
|
|
if (this._frame._loaderId === this._initialLoaderId && !this._hasSameDocumentNavigation)
|
|
return;
|
|
if (!checkLifecycle(this._frame, this._expectedLifecycle))
|
|
return;
|
|
if (this._hasSameDocumentNavigation)
|
|
this._sameDocumentNavigationCompleteCallback();
|
|
if (this._frame._loaderId !== this._initialLoaderId)
|
|
this._newDocumentNavigationCompleteCallback();
|
|
|
|
/**
|
|
* @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;
|
|
}
|
|
for (const child of frame.childFrames()) {
|
|
if (!checkLifecycle(child, expectedLifecycle))
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
|
|
dispose() {
|
|
helper.removeEventListeners(this._eventListeners);
|
|
clearTimeout(this._maximumTimer);
|
|
}
|
|
}
|
|
|
|
const puppeteerToProtocolLifecycle = {
|
|
'load': 'load',
|
|
'domcontentloaded': 'DOMContentLoaded',
|
|
'networkidle0': 'networkIdle',
|
|
'networkidle2': 'networkAlmostIdle',
|
|
};
|
|
|
|
module.exports = {NavigatorWatcher};
|