2018-11-21 04:09:25 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2018 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.
|
|
|
|
*/
|
|
|
|
|
2023-02-15 23:09:31 +00:00
|
|
|
import {ServerResponse} from 'http';
|
|
|
|
|
2020-06-23 05:18:46 +00:00
|
|
|
import expect from 'expect';
|
2023-02-15 23:09:31 +00:00
|
|
|
import {TimeoutError} from 'puppeteer';
|
2023-03-15 16:51:34 +00:00
|
|
|
import {HTTPRequest} from 'puppeteer-core/internal/api/HTTPRequest.js';
|
2023-02-15 23:09:31 +00:00
|
|
|
|
2020-06-23 05:18:46 +00:00
|
|
|
import {
|
2020-05-07 10:54:55 +00:00
|
|
|
getTestState,
|
|
|
|
setupTestBrowserHooks,
|
|
|
|
setupTestPageAndContextHooks,
|
2022-06-15 10:09:22 +00:00
|
|
|
} from './mocha-utils.js';
|
2023-03-10 15:59:02 +00:00
|
|
|
import {attachFrame, isFavicon, waitEvent} from './utils.js';
|
2018-11-21 04:09:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('navigation', function () {
|
2020-04-09 05:56:25 +00:00
|
|
|
setupTestBrowserHooks();
|
|
|
|
setupTestPageAndContextHooks();
|
2023-03-10 15:59:02 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Page.goto', function () {
|
|
|
|
it('should work', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-03 01:49:12 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
expect(page.url()).toBe(server.EMPTY_PAGE);
|
|
|
|
});
|
2022-05-25 06:46:17 +00:00
|
|
|
it('should work with anchor navigation', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-21 21:10:15 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
expect(page.url()).toBe(server.EMPTY_PAGE);
|
|
|
|
await page.goto(server.EMPTY_PAGE + '#foo');
|
|
|
|
expect(page.url()).toBe(server.EMPTY_PAGE + '#foo');
|
|
|
|
await page.goto(server.EMPTY_PAGE + '#bar');
|
|
|
|
expect(page.url()).toBe(server.EMPTY_PAGE + '#bar');
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with redirects', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-03 01:49:12 +00:00
|
|
|
server.setRedirect('/redirect/1.html', '/redirect/2.html');
|
|
|
|
server.setRedirect('/redirect/2.html', '/empty.html');
|
|
|
|
await page.goto(server.PREFIX + '/redirect/1.html');
|
|
|
|
expect(page.url()).toBe(server.EMPTY_PAGE);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should navigate to about:blank', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 04:09:25 +00:00
|
|
|
const response = await page.goto('about:blank');
|
|
|
|
expect(response).toBe(null);
|
|
|
|
});
|
2020-06-12 13:55:51 +00:00
|
|
|
it('should return response when page changes its URL after load', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
const response = (await page.goto(server.PREFIX + '/historyapi.html'))!;
|
2020-06-12 13:55:51 +00:00
|
|
|
expect(response.status()).toBe(200);
|
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should work with subframes return 204', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
server.setRoute('/frames/frame.html', (_req, res) => {
|
2018-11-21 04:09:25 +00:00
|
|
|
res.statusCode = 204;
|
|
|
|
res.end();
|
|
|
|
});
|
2022-06-15 10:09:22 +00:00
|
|
|
let error!: Error;
|
2020-06-23 11:55:42 +00:00
|
|
|
await page
|
|
|
|
.goto(server.PREFIX + '/frames/one-frame.html')
|
2022-06-22 13:25:44 +00:00
|
|
|
.catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (error = error_);
|
|
|
|
});
|
|
|
|
expect(error).toBeUndefined();
|
2018-11-21 04:09:25 +00:00
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should fail when server returns 204', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server, isChrome} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
server.setRoute('/empty.html', (_req, res) => {
|
2018-11-21 04:09:25 +00:00
|
|
|
res.statusCode = 204;
|
|
|
|
res.end();
|
|
|
|
});
|
2022-06-15 10:09:22 +00:00
|
|
|
let error!: Error;
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE).catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (error = error_);
|
|
|
|
});
|
2018-11-21 04:09:25 +00:00
|
|
|
expect(error).not.toBe(null);
|
2022-06-14 11:55:35 +00:00
|
|
|
if (isChrome) {
|
|
|
|
expect(error.message).toContain('net::ERR_ABORTED');
|
|
|
|
} else {
|
|
|
|
expect(error.message).toContain('NS_BINDING_ABORTED');
|
|
|
|
}
|
2018-11-21 04:09:25 +00:00
|
|
|
});
|
2020-06-12 13:55:51 +00:00
|
|
|
it('should navigate to empty page with domcontentloaded', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-06-12 13:55:51 +00:00
|
|
|
const response = await page.goto(server.EMPTY_PAGE, {
|
|
|
|
waitUntil: 'domcontentloaded',
|
|
|
|
});
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(response!.status()).toBe(200);
|
2020-06-12 13:55:51 +00:00
|
|
|
});
|
|
|
|
it('should work when page calls history API in beforeunload', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-06-12 13:55:51 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.evaluate(() => {
|
|
|
|
window.addEventListener(
|
|
|
|
'beforeunload',
|
2022-06-15 10:09:22 +00:00
|
|
|
() => {
|
|
|
|
return history.replaceState(null, 'initial', window.location.href);
|
|
|
|
},
|
2020-06-12 13:55:51 +00:00
|
|
|
false
|
|
|
|
);
|
|
|
|
});
|
|
|
|
const response = await page.goto(server.PREFIX + '/grid.html');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(response!.status()).toBe(200);
|
2020-06-12 13:55:51 +00:00
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should navigate to empty page with networkidle0', async () => {
|
|
|
|
const {page, server} = getTestState();
|
2020-05-07 10:54:55 +00:00
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
const response = await page.goto(server.EMPTY_PAGE, {
|
|
|
|
waitUntil: 'networkidle0',
|
|
|
|
});
|
|
|
|
expect(response!.status()).toBe(200);
|
|
|
|
});
|
|
|
|
it('should navigate to empty page with networkidle2', async () => {
|
|
|
|
const {page, server} = getTestState();
|
|
|
|
|
|
|
|
const response = await page.goto(server.EMPTY_PAGE, {
|
|
|
|
waitUntil: 'networkidle2',
|
|
|
|
});
|
|
|
|
expect(response!.status()).toBe(200);
|
|
|
|
});
|
|
|
|
it('should fail when navigating to bad url', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, isChrome} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
let error!: Error;
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.goto('asdfasdf').catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (error = error_);
|
|
|
|
});
|
2022-06-14 11:55:35 +00:00
|
|
|
if (isChrome) {
|
2019-02-07 15:15:19 +00:00
|
|
|
expect(error.message).toContain('Cannot navigate to invalid URL');
|
2022-06-14 11:55:35 +00:00
|
|
|
} else {
|
|
|
|
expect(error.message).toContain('Invalid url');
|
|
|
|
}
|
2018-11-21 04:09:25 +00:00
|
|
|
});
|
2020-05-21 14:36:59 +00:00
|
|
|
|
2022-12-07 13:54:00 +00:00
|
|
|
const EXPECTED_SSL_CERT_MESSAGE_REGEX =
|
|
|
|
/net::ERR_CERT_INVALID|net::ERR_CERT_AUTHORITY_INVALID/;
|
2020-05-21 14:36:59 +00:00
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should fail when navigating to bad SSL', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, httpsServer, isChrome} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 04:09:25 +00:00
|
|
|
// Make sure that network events do not emit 'undefined'.
|
|
|
|
// @see https://crbug.com/750469
|
2022-06-15 10:09:22 +00:00
|
|
|
const requests: string[] = [];
|
|
|
|
page.on('request', () => {
|
|
|
|
return requests.push('request');
|
|
|
|
});
|
|
|
|
page.on('requestfinished', () => {
|
|
|
|
return requests.push('requestfinished');
|
|
|
|
});
|
|
|
|
page.on('requestfailed', () => {
|
|
|
|
return requests.push('requestfailed');
|
|
|
|
});
|
2020-05-26 08:22:11 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
let error!: Error;
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.goto(httpsServer.EMPTY_PAGE).catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (error = error_);
|
|
|
|
});
|
2022-06-14 11:55:35 +00:00
|
|
|
if (isChrome) {
|
2022-12-07 13:54:00 +00:00
|
|
|
expect(error.message).toMatch(EXPECTED_SSL_CERT_MESSAGE_REGEX);
|
2022-06-14 11:55:35 +00:00
|
|
|
} else {
|
|
|
|
expect(error.message).toContain('SSL_ERROR_UNKNOWN');
|
|
|
|
}
|
2020-05-26 08:22:11 +00:00
|
|
|
|
|
|
|
expect(requests.length).toBe(2);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(requests[0]!).toBe('request');
|
|
|
|
expect(requests[1]!).toBe('requestfailed');
|
2020-05-07 10:54:55 +00:00
|
|
|
});
|
2020-06-12 13:55:51 +00:00
|
|
|
it('should fail when navigating to bad SSL after redirects', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server, httpsServer, isChrome} = getTestState();
|
2020-06-12 13:55:51 +00:00
|
|
|
|
|
|
|
server.setRedirect('/redirect/1.html', '/redirect/2.html');
|
|
|
|
server.setRedirect('/redirect/2.html', '/empty.html');
|
2022-06-15 10:09:22 +00:00
|
|
|
let error!: Error;
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.goto(httpsServer.PREFIX + '/redirect/1.html').catch(error_ => {
|
|
|
|
return (error = error_);
|
|
|
|
});
|
2022-06-14 11:55:35 +00:00
|
|
|
if (isChrome) {
|
2022-12-07 13:54:00 +00:00
|
|
|
expect(error.message).toMatch(EXPECTED_SSL_CERT_MESSAGE_REGEX);
|
2022-06-14 11:55:35 +00:00
|
|
|
} else {
|
|
|
|
expect(error.message).toContain('SSL_ERROR_UNKNOWN');
|
|
|
|
}
|
2020-06-12 13:55:51 +00:00
|
|
|
});
|
|
|
|
it('should fail when main resources failed to load', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, isChrome} = getTestState();
|
2020-06-12 13:55:51 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
let error!: Error;
|
2020-06-12 13:55:51 +00:00
|
|
|
await page
|
|
|
|
.goto('http://localhost:44123/non-existing-url')
|
2022-06-22 13:25:44 +00:00
|
|
|
.catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (error = error_);
|
|
|
|
});
|
2022-06-14 11:55:35 +00:00
|
|
|
if (isChrome) {
|
2020-06-12 13:55:51 +00:00
|
|
|
expect(error.message).toContain('net::ERR_CONNECTION_REFUSED');
|
2022-06-14 11:55:35 +00:00
|
|
|
} else {
|
|
|
|
expect(error.message).toContain('NS_ERROR_CONNECTION_REFUSED');
|
|
|
|
}
|
2020-06-12 13:55:51 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should fail when exceeding maximum navigation timeout', async () => {
|
2022-10-06 14:21:24 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 04:09:25 +00:00
|
|
|
// Hang for request to the empty.html
|
2020-06-23 05:18:46 +00:00
|
|
|
server.setRoute('/empty.html', () => {});
|
2022-06-15 10:09:22 +00:00
|
|
|
let error!: Error;
|
2020-05-07 10:54:55 +00:00
|
|
|
await page
|
2022-06-22 13:25:44 +00:00
|
|
|
.goto(server.PREFIX + '/empty.html', {timeout: 1})
|
|
|
|
.catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (error = error_);
|
|
|
|
});
|
2019-10-23 09:41:01 +00:00
|
|
|
expect(error.message).toContain('Navigation timeout of 1 ms exceeded');
|
2022-10-06 14:21:24 +00:00
|
|
|
expect(error).toBeInstanceOf(TimeoutError);
|
2018-11-21 04:09:25 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should fail when exceeding default maximum navigation timeout', async () => {
|
2022-10-06 14:21:24 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 04:09:25 +00:00
|
|
|
// Hang for request to the empty.html
|
2020-06-23 05:18:46 +00:00
|
|
|
server.setRoute('/empty.html', () => {});
|
2022-06-15 10:09:22 +00:00
|
|
|
let error!: Error;
|
2018-11-21 04:09:25 +00:00
|
|
|
page.setDefaultNavigationTimeout(1);
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.goto(server.PREFIX + '/empty.html').catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (error = error_);
|
|
|
|
});
|
2019-10-23 09:41:01 +00:00
|
|
|
expect(error.message).toContain('Navigation timeout of 1 ms exceeded');
|
2022-10-06 14:21:24 +00:00
|
|
|
expect(error).toBeInstanceOf(TimeoutError);
|
2018-11-21 04:09:25 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should fail when exceeding default maximum timeout', async () => {
|
2022-10-06 14:21:24 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-01-29 01:16:12 +00:00
|
|
|
// Hang for request to the empty.html
|
2020-06-23 05:18:46 +00:00
|
|
|
server.setRoute('/empty.html', () => {});
|
2022-06-15 10:09:22 +00:00
|
|
|
let error!: Error;
|
2019-01-29 01:16:12 +00:00
|
|
|
page.setDefaultTimeout(1);
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.goto(server.PREFIX + '/empty.html').catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (error = error_);
|
|
|
|
});
|
2019-10-23 09:41:01 +00:00
|
|
|
expect(error.message).toContain('Navigation timeout of 1 ms exceeded');
|
2022-10-06 14:21:24 +00:00
|
|
|
expect(error).toBeInstanceOf(TimeoutError);
|
2019-01-29 01:16:12 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should prioritize default navigation timeout over default timeout', async () => {
|
2022-10-06 14:21:24 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-01-29 01:16:12 +00:00
|
|
|
// Hang for request to the empty.html
|
2020-06-23 05:18:46 +00:00
|
|
|
server.setRoute('/empty.html', () => {});
|
2022-06-15 10:09:22 +00:00
|
|
|
let error!: Error;
|
2019-01-29 01:16:12 +00:00
|
|
|
page.setDefaultTimeout(0);
|
|
|
|
page.setDefaultNavigationTimeout(1);
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.goto(server.PREFIX + '/empty.html').catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (error = error_);
|
|
|
|
});
|
2019-10-23 09:41:01 +00:00
|
|
|
expect(error.message).toContain('Navigation timeout of 1 ms exceeded');
|
2022-10-06 14:21:24 +00:00
|
|
|
expect(error).toBeInstanceOf(TimeoutError);
|
2019-01-29 01:16:12 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should disable timeout when its set to 0', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
let error!: Error;
|
2018-11-21 04:09:25 +00:00
|
|
|
let loaded = false;
|
2022-06-15 10:09:22 +00:00
|
|
|
page.once('load', () => {
|
|
|
|
return (loaded = true);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
await page
|
2022-06-22 13:25:44 +00:00
|
|
|
.goto(server.PREFIX + '/grid.html', {timeout: 0, waitUntil: ['load']})
|
|
|
|
.catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (error = error_);
|
|
|
|
});
|
|
|
|
expect(error).toBeUndefined();
|
2018-11-21 04:09:25 +00:00
|
|
|
expect(loaded).toBe(true);
|
|
|
|
});
|
2020-06-12 13:55:51 +00:00
|
|
|
it('should work when navigating to valid url', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
const response = (await page.goto(server.EMPTY_PAGE))!;
|
2018-11-21 04:09:25 +00:00
|
|
|
expect(response.ok()).toBe(true);
|
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should work when navigating to data url', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
const response = (await page.goto('data:text/html,hello'))!;
|
2018-11-21 04:09:25 +00:00
|
|
|
expect(response.ok()).toBe(true);
|
|
|
|
});
|
2020-06-12 13:55:51 +00:00
|
|
|
it('should work when navigating to 404', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
const response = (await page.goto(server.PREFIX + '/not-found'))!;
|
2018-11-21 04:09:25 +00:00
|
|
|
expect(response.ok()).toBe(false);
|
|
|
|
expect(response.status()).toBe(404);
|
|
|
|
});
|
2023-02-06 09:40:51 +00:00
|
|
|
it('should not throw an error for a 404 response with an empty body', async () => {
|
|
|
|
const {page, server} = getTestState();
|
|
|
|
|
|
|
|
server.setRoute('/404-error', (_, res) => {
|
|
|
|
res.statusCode = 404;
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
const response = (await page.goto(server.PREFIX + '/404-error'))!;
|
|
|
|
expect(response.ok()).toBe(false);
|
|
|
|
expect(response.status()).toBe(404);
|
|
|
|
});
|
|
|
|
it('should not throw an error for a 500 response with an empty body', async () => {
|
|
|
|
const {page, server} = getTestState();
|
|
|
|
|
|
|
|
server.setRoute('/500-error', (_, res) => {
|
|
|
|
res.statusCode = 500;
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
const response = (await page.goto(server.PREFIX + '/500-error'))!;
|
|
|
|
expect(response.ok()).toBe(false);
|
|
|
|
expect(response.status()).toBe(500);
|
|
|
|
});
|
2020-06-12 13:55:51 +00:00
|
|
|
it('should return last response in redirect chain', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-05-07 10:54:55 +00:00
|
|
|
|
2020-06-12 13:55:51 +00:00
|
|
|
server.setRedirect('/redirect/1.html', '/redirect/2.html');
|
|
|
|
server.setRedirect('/redirect/2.html', '/redirect/3.html');
|
|
|
|
server.setRedirect('/redirect/3.html', server.EMPTY_PAGE);
|
2022-06-15 10:09:22 +00:00
|
|
|
const response = (await page.goto(server.PREFIX + '/redirect/1.html'))!;
|
2020-06-12 13:55:51 +00:00
|
|
|
expect(response.ok()).toBe(true);
|
|
|
|
expect(response.url()).toBe(server.EMPTY_PAGE);
|
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should wait for network idle to succeed navigation', async () => {
|
|
|
|
const {page, server} = getTestState();
|
2020-05-07 10:54:55 +00:00
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
let responses: ServerResponse[] = [];
|
|
|
|
// Hold on to a bunch of requests without answering.
|
|
|
|
server.setRoute('/fetch-request-a.js', (_req, res) => {
|
|
|
|
return responses.push(res);
|
|
|
|
});
|
|
|
|
server.setRoute('/fetch-request-b.js', (_req, res) => {
|
|
|
|
return responses.push(res);
|
|
|
|
});
|
|
|
|
server.setRoute('/fetch-request-c.js', (_req, res) => {
|
|
|
|
return responses.push(res);
|
|
|
|
});
|
|
|
|
server.setRoute('/fetch-request-d.js', (_req, res) => {
|
|
|
|
return responses.push(res);
|
|
|
|
});
|
|
|
|
const initialFetchResourcesRequested = Promise.all([
|
|
|
|
server.waitForRequest('/fetch-request-a.js'),
|
|
|
|
server.waitForRequest('/fetch-request-b.js'),
|
|
|
|
server.waitForRequest('/fetch-request-c.js'),
|
2023-03-10 15:59:02 +00:00
|
|
|
]).catch(() => {
|
|
|
|
// Ignore Error that arise from test server during hooks
|
|
|
|
});
|
|
|
|
const secondFetchResourceRequested = server
|
|
|
|
.waitForRequest('/fetch-request-d.js')
|
|
|
|
.catch(() => {
|
|
|
|
// Ignore Error that arise from test server during hooks
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
// Navigate to a page which loads immediately and then does a bunch of
|
|
|
|
// requests via javascript's fetch method.
|
|
|
|
const navigationPromise = page.goto(server.PREFIX + '/networkidle.html', {
|
|
|
|
waitUntil: 'networkidle0',
|
|
|
|
});
|
|
|
|
// Track when the navigation gets completed.
|
|
|
|
let navigationFinished = false;
|
|
|
|
navigationPromise.then(() => {
|
|
|
|
return (navigationFinished = true);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
// Wait for the page's 'load' event.
|
|
|
|
await new Promise(fulfill => {
|
|
|
|
return page.once('load', fulfill);
|
|
|
|
});
|
|
|
|
expect(navigationFinished).toBe(false);
|
2020-05-07 10:54:55 +00:00
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
// Wait for the initial three resources to be requested.
|
|
|
|
await initialFetchResourcesRequested;
|
2020-05-07 10:54:55 +00:00
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
// Expect navigation still to be not finished.
|
|
|
|
expect(navigationFinished).toBe(false);
|
2020-05-07 10:54:55 +00:00
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
// Respond to initial requests.
|
|
|
|
for (const response of responses) {
|
|
|
|
response.statusCode = 404;
|
|
|
|
response.end(`File not found`);
|
|
|
|
}
|
2020-05-07 10:54:55 +00:00
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
// Reset responses array
|
|
|
|
responses = [];
|
2020-05-07 10:54:55 +00:00
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
// Wait for the second round to be requested.
|
|
|
|
await secondFetchResourceRequested;
|
|
|
|
// Expect navigation still to be not finished.
|
|
|
|
expect(navigationFinished).toBe(false);
|
2020-05-07 10:54:55 +00:00
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
// Respond to requests.
|
|
|
|
for (const response of responses) {
|
|
|
|
response.statusCode = 404;
|
|
|
|
response.end(`File not found`);
|
2018-11-21 04:09:25 +00:00
|
|
|
}
|
2022-09-08 10:32:39 +00:00
|
|
|
|
|
|
|
const response = (await navigationPromise)!;
|
|
|
|
// Expect navigation to succeed.
|
|
|
|
expect(response.ok()).toBe(true);
|
|
|
|
});
|
2023-03-28 15:09:18 +00:00
|
|
|
it('should not leak listeners during navigation', async function () {
|
|
|
|
this.timeout(25_000);
|
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 04:09:25 +00:00
|
|
|
let warning = null;
|
2022-06-22 13:25:44 +00:00
|
|
|
const warningHandler: NodeJS.WarningListener = w => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (warning = w);
|
|
|
|
};
|
2018-11-21 04:09:25 +00:00
|
|
|
process.on('warning', warningHandler);
|
2022-06-14 11:55:35 +00:00
|
|
|
for (let i = 0; i < 20; ++i) {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
}
|
2018-11-21 04:09:25 +00:00
|
|
|
process.removeListener('warning', warningHandler);
|
|
|
|
expect(warning).toBe(null);
|
|
|
|
});
|
2023-03-28 15:09:18 +00:00
|
|
|
it('should not leak listeners during bad navigation', async function () {
|
|
|
|
this.timeout(25_000);
|
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = getTestState();
|
2020-05-07 10:54:55 +00:00
|
|
|
|
2020-06-12 13:55:51 +00:00
|
|
|
let warning = null;
|
2022-06-22 13:25:44 +00:00
|
|
|
const warningHandler: NodeJS.WarningListener = w => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (warning = w);
|
|
|
|
};
|
2020-06-12 13:55:51 +00:00
|
|
|
process.on('warning', warningHandler);
|
2022-06-14 11:55:35 +00:00
|
|
|
for (let i = 0; i < 20; ++i) {
|
2020-06-23 05:18:46 +00:00
|
|
|
await page.goto('asdf').catch(() => {
|
2020-06-12 13:55:51 +00:00
|
|
|
/* swallow navigation error */
|
|
|
|
});
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2020-06-12 13:55:51 +00:00
|
|
|
process.removeListener('warning', warningHandler);
|
|
|
|
expect(warning).toBe(null);
|
|
|
|
});
|
2023-03-28 15:09:18 +00:00
|
|
|
it('should not leak listeners during navigation of 11 pages', async function () {
|
|
|
|
this.timeout(25_000);
|
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
const {context, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 04:09:25 +00:00
|
|
|
let warning = null;
|
2022-06-22 13:25:44 +00:00
|
|
|
const warningHandler: NodeJS.WarningListener = w => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (warning = w);
|
|
|
|
};
|
2018-11-21 04:09:25 +00:00
|
|
|
process.on('warning', warningHandler);
|
2020-05-07 10:54:55 +00:00
|
|
|
await Promise.all(
|
|
|
|
[...Array(20)].map(async () => {
|
|
|
|
const page = await context.newPage();
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.close();
|
|
|
|
})
|
|
|
|
);
|
2018-11-21 04:09:25 +00:00
|
|
|
process.removeListener('warning', warningHandler);
|
|
|
|
expect(warning).toBe(null);
|
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should navigate to dataURL and fire dataURL requests', async () => {
|
|
|
|
const {page} = getTestState();
|
2020-05-07 10:54:55 +00:00
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
const requests: HTTPRequest[] = [];
|
|
|
|
page.on('request', request => {
|
2023-03-10 15:59:02 +00:00
|
|
|
return !isFavicon(request) && requests.push(request);
|
2022-09-08 10:32:39 +00:00
|
|
|
});
|
|
|
|
const dataURL = 'data:text/html,<div>yo</div>';
|
|
|
|
const response = (await page.goto(dataURL))!;
|
|
|
|
expect(response.status()).toBe(200);
|
|
|
|
expect(requests.length).toBe(1);
|
|
|
|
expect(requests[0]!.url()).toBe(dataURL);
|
|
|
|
});
|
|
|
|
it('should navigate to URL with hash and fire requests without hash', async () => {
|
|
|
|
const {page, server} = getTestState();
|
|
|
|
|
|
|
|
const requests: HTTPRequest[] = [];
|
|
|
|
page.on('request', request => {
|
2023-03-10 15:59:02 +00:00
|
|
|
return !isFavicon(request) && requests.push(request);
|
2022-09-08 10:32:39 +00:00
|
|
|
});
|
|
|
|
const response = (await page.goto(server.EMPTY_PAGE + '#hash'))!;
|
|
|
|
expect(response.status()).toBe(200);
|
|
|
|
expect(response.url()).toBe(server.EMPTY_PAGE);
|
|
|
|
expect(requests.length).toBe(1);
|
|
|
|
expect(requests[0]!.url()).toBe(server.EMPTY_PAGE);
|
|
|
|
});
|
2020-06-12 13:55:51 +00:00
|
|
|
it('should work with self requesting page', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
const response = (await page.goto(server.PREFIX + '/self-request.html'))!;
|
2018-11-21 04:09:25 +00:00
|
|
|
expect(response.status()).toBe(200);
|
|
|
|
expect(response.url()).toContain('self-request.html');
|
|
|
|
});
|
2020-06-12 13:55:51 +00:00
|
|
|
it('should fail when navigating and show the url at the error message', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, httpsServer} = getTestState();
|
2020-06-12 13:55:51 +00:00
|
|
|
|
|
|
|
const url = httpsServer.PREFIX + '/redirect/1.html';
|
2022-06-15 10:09:22 +00:00
|
|
|
let error!: Error;
|
2020-06-12 13:55:51 +00:00
|
|
|
try {
|
|
|
|
await page.goto(url);
|
|
|
|
} catch (error_) {
|
2022-06-15 10:09:22 +00:00
|
|
|
error = error_ as Error;
|
2018-11-21 04:09:25 +00:00
|
|
|
}
|
2020-06-12 13:55:51 +00:00
|
|
|
expect(error.message).toContain(url);
|
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should send referer', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2023-03-10 15:59:02 +00:00
|
|
|
const requests = Promise.all([
|
2018-11-21 04:09:25 +00:00
|
|
|
server.waitForRequest('/grid.html'),
|
|
|
|
server.waitForRequest('/digits/1.png'),
|
|
|
|
page.goto(server.PREFIX + '/grid.html', {
|
|
|
|
referer: 'http://google.com/',
|
|
|
|
}),
|
2023-03-10 15:59:02 +00:00
|
|
|
]).catch(() => {
|
|
|
|
return [];
|
|
|
|
});
|
|
|
|
|
|
|
|
const [request1, request2] = await requests;
|
2018-11-21 04:09:25 +00:00
|
|
|
expect(request1.headers['referer']).toBe('http://google.com/');
|
|
|
|
// Make sure subresources do not inherit referer.
|
|
|
|
expect(request2.headers['referer']).toBe(server.PREFIX + '/grid.html');
|
|
|
|
});
|
2023-01-23 11:11:20 +00:00
|
|
|
|
|
|
|
it('should send referer policy', async () => {
|
|
|
|
const {page, server} = getTestState();
|
|
|
|
|
|
|
|
const [request1, request2] = await Promise.all([
|
|
|
|
server.waitForRequest('/grid.html'),
|
|
|
|
server.waitForRequest('/digits/1.png'),
|
|
|
|
page.goto(server.PREFIX + '/grid.html', {
|
|
|
|
referrerPolicy: 'no-referer',
|
|
|
|
}),
|
2023-03-10 15:59:02 +00:00
|
|
|
]).catch(() => {
|
|
|
|
return [];
|
|
|
|
});
|
2023-01-23 11:11:20 +00:00
|
|
|
expect(request1.headers['referer']).toBeUndefined();
|
|
|
|
expect(request2.headers['referer']).toBe(server.PREFIX + '/grid.html');
|
|
|
|
});
|
2018-11-21 04:09:25 +00:00
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Page.waitForNavigation', function () {
|
2022-05-25 06:46:17 +00:00
|
|
|
it('should work', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 04:09:25 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const [response] = await Promise.all([
|
|
|
|
page.waitForNavigation(),
|
2022-06-15 10:09:22 +00:00
|
|
|
page.evaluate((url: string) => {
|
|
|
|
return (window.location.href = url);
|
|
|
|
}, server.PREFIX + '/grid.html'),
|
2018-11-21 04:09:25 +00:00
|
|
|
]);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(response!.ok()).toBe(true);
|
|
|
|
expect(response!.url()).toContain('grid.html');
|
2018-11-21 04:09:25 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with both domcontentloaded and load', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
let response!: ServerResponse;
|
|
|
|
server.setRoute('/one-style.css', (_req, res) => {
|
|
|
|
return (response = res);
|
|
|
|
});
|
2018-11-21 04:09:25 +00:00
|
|
|
const navigationPromise = page.goto(server.PREFIX + '/one-style.html');
|
|
|
|
const domContentLoadedPromise = page.waitForNavigation({
|
2020-05-07 10:54:55 +00:00
|
|
|
waitUntil: 'domcontentloaded',
|
2018-11-21 04:09:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let bothFired = false;
|
2020-05-07 10:54:55 +00:00
|
|
|
const bothFiredPromise = page
|
|
|
|
.waitForNavigation({
|
|
|
|
waitUntil: ['load', 'domcontentloaded'],
|
|
|
|
})
|
2022-06-15 10:09:22 +00:00
|
|
|
.then(() => {
|
|
|
|
return (bothFired = true);
|
|
|
|
});
|
2018-11-21 04:09:25 +00:00
|
|
|
|
2023-03-10 15:59:02 +00:00
|
|
|
await server.waitForRequest('/one-style.css').catch(() => {});
|
2018-11-21 04:09:25 +00:00
|
|
|
await domContentLoadedPromise;
|
|
|
|
expect(bothFired).toBe(false);
|
|
|
|
response.end();
|
|
|
|
await bothFiredPromise;
|
|
|
|
await navigationPromise;
|
|
|
|
});
|
2022-05-25 06:46:17 +00:00
|
|
|
it('should work with clicking on anchor links', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 04:09:25 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.setContent(`<a href='#foobar'>foobar</a>`);
|
|
|
|
const [response] = await Promise.all([
|
|
|
|
page.waitForNavigation(),
|
|
|
|
page.click('a'),
|
|
|
|
]);
|
|
|
|
expect(response).toBe(null);
|
|
|
|
expect(page.url()).toBe(server.EMPTY_PAGE + '#foobar');
|
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should work with history.pushState()', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 04:09:25 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.setContent(`
|
|
|
|
<a onclick='javascript:pushState()'>SPA</a>
|
|
|
|
<script>
|
|
|
|
function pushState() { history.pushState({}, '', 'wow.html') }
|
|
|
|
</script>
|
|
|
|
`);
|
|
|
|
const [response] = await Promise.all([
|
|
|
|
page.waitForNavigation(),
|
|
|
|
page.click('a'),
|
|
|
|
]);
|
|
|
|
expect(response).toBe(null);
|
|
|
|
expect(page.url()).toBe(server.PREFIX + '/wow.html');
|
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should work with history.replaceState()', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 04:09:25 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.setContent(`
|
|
|
|
<a onclick='javascript:replaceState()'>SPA</a>
|
|
|
|
<script>
|
|
|
|
function replaceState() { history.replaceState({}, '', '/replaced.html') }
|
|
|
|
</script>
|
|
|
|
`);
|
|
|
|
const [response] = await Promise.all([
|
|
|
|
page.waitForNavigation(),
|
|
|
|
page.click('a'),
|
|
|
|
]);
|
|
|
|
expect(response).toBe(null);
|
|
|
|
expect(page.url()).toBe(server.PREFIX + '/replaced.html');
|
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should work with DOM history.back()/history.forward()', async () => {
|
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.setContent(`
|
2018-11-21 04:09:25 +00:00
|
|
|
<a id=back onclick='javascript:goBack()'>back</a>
|
|
|
|
<a id=forward onclick='javascript:goForward()'>forward</a>
|
|
|
|
<script>
|
|
|
|
function goBack() { history.back(); }
|
|
|
|
function goForward() { history.forward(); }
|
|
|
|
history.pushState({}, '', '/first.html');
|
|
|
|
history.pushState({}, '', '/second.html');
|
|
|
|
</script>
|
|
|
|
`);
|
2022-09-08 10:32:39 +00:00
|
|
|
expect(page.url()).toBe(server.PREFIX + '/second.html');
|
|
|
|
const [backResponse] = await Promise.all([
|
|
|
|
page.waitForNavigation(),
|
|
|
|
page.click('a#back'),
|
|
|
|
]);
|
|
|
|
expect(backResponse).toBe(null);
|
|
|
|
expect(page.url()).toBe(server.PREFIX + '/first.html');
|
|
|
|
const [forwardResponse] = await Promise.all([
|
|
|
|
page.waitForNavigation(),
|
|
|
|
page.click('a#forward'),
|
|
|
|
]);
|
|
|
|
expect(forwardResponse).toBe(null);
|
|
|
|
expect(page.url()).toBe(server.PREFIX + '/second.html');
|
|
|
|
});
|
|
|
|
it('should work when subframe issues window.stop()', async () => {
|
|
|
|
const {page, server} = getTestState();
|
|
|
|
|
|
|
|
server.setRoute('/frames/style.css', () => {});
|
|
|
|
const navigationPromise = page.goto(
|
|
|
|
server.PREFIX + '/frames/one-frame.html'
|
|
|
|
);
|
2023-03-10 15:59:02 +00:00
|
|
|
const frame = await waitEvent(page, 'frameattached');
|
2022-09-08 10:32:39 +00:00
|
|
|
await new Promise<void>(fulfill => {
|
|
|
|
page.on('framenavigated', f => {
|
|
|
|
if (f === frame) {
|
|
|
|
fulfill();
|
|
|
|
}
|
2018-11-21 04:09:25 +00:00
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
});
|
|
|
|
await Promise.all([
|
|
|
|
frame.evaluate(() => {
|
|
|
|
return window.stop();
|
|
|
|
}),
|
|
|
|
navigationPromise,
|
|
|
|
]);
|
|
|
|
});
|
2018-11-21 04:09:25 +00:00
|
|
|
});
|
|
|
|
|
2020-06-12 13:55:51 +00:00
|
|
|
describe('Page.goBack', function () {
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 04:09:25 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.goto(server.PREFIX + '/grid.html');
|
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
let response = (await page.goBack())!;
|
2018-11-21 04:09:25 +00:00
|
|
|
expect(response.ok()).toBe(true);
|
|
|
|
expect(response.url()).toContain(server.EMPTY_PAGE);
|
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
response = (await page.goForward())!;
|
2018-11-21 04:09:25 +00:00
|
|
|
expect(response.ok()).toBe(true);
|
|
|
|
expect(response.url()).toContain('/grid.html');
|
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
response = (await page.goForward())!;
|
2018-11-21 04:09:25 +00:00
|
|
|
expect(response).toBe(null);
|
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should work with HistoryAPI', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 04:09:25 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.evaluate(() => {
|
|
|
|
history.pushState({}, '', '/first.html');
|
|
|
|
history.pushState({}, '', '/second.html');
|
|
|
|
});
|
|
|
|
expect(page.url()).toBe(server.PREFIX + '/second.html');
|
|
|
|
|
|
|
|
await page.goBack();
|
|
|
|
expect(page.url()).toBe(server.PREFIX + '/first.html');
|
|
|
|
await page.goBack();
|
|
|
|
expect(page.url()).toBe(server.EMPTY_PAGE);
|
|
|
|
await page.goForward();
|
|
|
|
expect(page.url()).toBe(server.PREFIX + '/first.html');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
describe('Frame.goto', function () {
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should navigate subframes', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 04:09:25 +00:00
|
|
|
await page.goto(server.PREFIX + '/frames/one-frame.html');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(page.frames()[0]!.url()).toContain('/frames/one-frame.html');
|
|
|
|
expect(page.frames()[1]!.url()).toContain('/frames/frame.html');
|
2018-11-21 04:09:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
const response = (await page.frames()[1]!.goto(server.EMPTY_PAGE))!;
|
2018-11-21 04:09:25 +00:00
|
|
|
expect(response.ok()).toBe(true);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(response.frame()).toBe(page.frames()[1]!);
|
2018-11-21 04:09:25 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should reject when frame detaches', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 04:09:25 +00:00
|
|
|
await page.goto(server.PREFIX + '/frames/one-frame.html');
|
|
|
|
|
|
|
|
server.setRoute('/empty.html', () => {});
|
2020-05-07 10:54:55 +00:00
|
|
|
const navigationPromise = page
|
2022-06-15 10:09:22 +00:00
|
|
|
.frames()[1]!
|
2020-05-07 10:54:55 +00:00
|
|
|
.goto(server.EMPTY_PAGE)
|
2022-06-22 13:25:44 +00:00
|
|
|
.catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return error_;
|
|
|
|
});
|
2023-03-10 15:59:02 +00:00
|
|
|
await server.waitForRequest('/empty.html').catch(() => {});
|
2018-11-21 04:09:25 +00:00
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.$eval('iframe', frame => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return frame.remove();
|
|
|
|
});
|
2018-11-21 04:09:25 +00:00
|
|
|
const error = await navigationPromise;
|
|
|
|
expect(error.message).toBe('Navigating frame was detached');
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should return matching responses', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 04:09:25 +00:00
|
|
|
// Disable cache: otherwise, chromium will cache similar requests.
|
|
|
|
await page.setCacheEnabled(false);
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
// Attach three frames.
|
|
|
|
const frames = await Promise.all([
|
2023-03-10 15:59:02 +00:00
|
|
|
attachFrame(page, 'frame1', server.EMPTY_PAGE),
|
|
|
|
attachFrame(page, 'frame2', server.EMPTY_PAGE),
|
|
|
|
attachFrame(page, 'frame3', server.EMPTY_PAGE),
|
2018-11-21 04:09:25 +00:00
|
|
|
]);
|
|
|
|
// Navigate all frames to the same URL.
|
2022-06-15 10:09:22 +00:00
|
|
|
const serverResponses: ServerResponse[] = [];
|
|
|
|
server.setRoute('/one-style.html', (_req, res) => {
|
|
|
|
return serverResponses.push(res);
|
|
|
|
});
|
2018-11-21 04:09:25 +00:00
|
|
|
const navigations = [];
|
|
|
|
for (let i = 0; i < 3; ++i) {
|
2022-06-15 10:09:22 +00:00
|
|
|
navigations.push(frames[i]!.goto(server.PREFIX + '/one-style.html'));
|
2018-11-21 04:09:25 +00:00
|
|
|
await server.waitForRequest('/one-style.html');
|
|
|
|
}
|
|
|
|
// Respond from server out-of-order.
|
|
|
|
const serverResponseTexts = ['AAA', 'BBB', 'CCC'];
|
|
|
|
for (const i of [1, 2, 0]) {
|
2022-06-15 10:09:22 +00:00
|
|
|
serverResponses[i]!.end(serverResponseTexts[i]);
|
|
|
|
const response = (await navigations[i])!;
|
2018-11-21 04:09:25 +00:00
|
|
|
expect(response.frame()).toBe(frames[i]);
|
|
|
|
expect(await response.text()).toBe(serverResponseTexts[i]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
describe('Frame.waitForNavigation', function () {
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 04:09:25 +00:00
|
|
|
await page.goto(server.PREFIX + '/frames/one-frame.html');
|
2022-06-15 10:09:22 +00:00
|
|
|
const frame = page.frames()[1]!;
|
2018-11-21 04:09:25 +00:00
|
|
|
const [response] = await Promise.all([
|
|
|
|
frame.waitForNavigation(),
|
2022-06-15 10:09:22 +00:00
|
|
|
frame.evaluate((url: string) => {
|
|
|
|
return (window.location.href = url);
|
|
|
|
}, server.PREFIX + '/grid.html'),
|
2018-11-21 04:09:25 +00:00
|
|
|
]);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(response!.ok()).toBe(true);
|
|
|
|
expect(response!.url()).toContain('grid.html');
|
|
|
|
expect(response!.frame()).toBe(frame);
|
2018-11-21 04:09:25 +00:00
|
|
|
expect(page.url()).toContain('/frames/one-frame.html');
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should fail when frame detaches', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 04:09:25 +00:00
|
|
|
await page.goto(server.PREFIX + '/frames/one-frame.html');
|
2022-06-15 10:09:22 +00:00
|
|
|
const frame = page.frames()[1]!;
|
2018-11-21 04:09:25 +00:00
|
|
|
|
|
|
|
server.setRoute('/empty.html', () => {});
|
2022-06-15 10:09:22 +00:00
|
|
|
let error!: Error;
|
2022-06-22 13:25:44 +00:00
|
|
|
const navigationPromise = frame.waitForNavigation().catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (error = error_);
|
|
|
|
});
|
2018-11-21 04:09:25 +00:00
|
|
|
await Promise.all([
|
|
|
|
server.waitForRequest('/empty.html'),
|
2022-06-15 10:09:22 +00:00
|
|
|
frame.evaluate(() => {
|
|
|
|
return ((window as any).location = '/empty.html');
|
|
|
|
}),
|
2018-11-21 04:09:25 +00:00
|
|
|
]);
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.$eval('iframe', frame => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return frame.remove();
|
|
|
|
});
|
2018-11-21 04:09:25 +00:00
|
|
|
await navigationPromise;
|
2019-04-29 03:19:01 +00:00
|
|
|
expect(error.message).toBe('Navigating frame was detached');
|
2018-11-21 04:09:25 +00:00
|
|
|
});
|
|
|
|
});
|
2019-02-03 01:49:12 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Page.reload', function () {
|
|
|
|
it('should work', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-03 01:49:12 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return ((globalThis as any)._foo = 10);
|
|
|
|
});
|
2019-02-03 01:49:12 +00:00
|
|
|
await page.reload();
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return (globalThis as any)._foo;
|
|
|
|
})
|
|
|
|
).toBe(undefined);
|
2019-02-03 01:49:12 +00:00
|
|
|
});
|
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|