fix(page): Page.goto should properly handle historyAPI in beforeunload (#3198)

Fixes #2764.
This commit is contained in:
Andrey Lushnikov
2018-09-05 22:59:29 +01:00
committed by GitHub
parent 28d92116b7
commit d54c7edeae
3 changed files with 50 additions and 29 deletions

View File

@@ -591,14 +591,18 @@ class Page extends EventEmitter {
const mainFrame = this._frameManager.mainFrame();
const timeout = typeof options.timeout === 'number' ? options.timeout : this._defaultNavigationTimeout;
const watcher = new NavigatorWatcher(this._frameManager, mainFrame, timeout, options);
const navigationPromise = watcher.navigationPromise();
let ensureNewDocumentNavigation = false;
let error = await Promise.race([
navigate(this._client, url, referrer),
navigationPromise,
watcher.timeoutPromise(),
]);
if (!error)
error = await navigationPromise;
watcher.cancel();
if (!error) {
error = await Promise.race([
watcher.timeoutPromise(),
ensureNewDocumentNavigation ? watcher.newDocumentNavigationPromise() : watcher.sameDocumentNavigationPromise(),
]);
}
watcher.dispose();
helper.removeEventListeners(eventListeners);
if (error)
throw error;
@@ -614,6 +618,7 @@ class Page extends EventEmitter {
async function navigate(client, url, referrer) {
try {
const response = await client.send('Page.navigate', {url, referrer});
ensureNewDocumentNavigation = !!response.loaderId;
return response.errorText ? new Error(`${response.errorText} at ${url}`) : null;
} catch (error) {
return error;
@@ -644,7 +649,12 @@ class Page extends EventEmitter {
const responses = new Map();
const listener = helper.addEventListener(this._networkManager, NetworkManager.Events.Response, response => responses.set(response.url(), response));
const error = await watcher.navigationPromise();
const error = await Promise.race([
watcher.timeoutPromise(),
watcher.sameDocumentNavigationPromise(),
watcher.newDocumentNavigationPromise()
]);
watcher.dispose();
helper.removeEventListeners([listener]);
if (error)
throw error;