/**
* Copyright 2018 Google Inc. All rights reserved.
* Modifications copyright (c) Microsoft Corporation.
*
* 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.
*/
import { test as it, expect } from './pageTest';
import type { Frame } from 'playwright-core';
import { expectedSSLError } from '../config/utils';
it('should work', async ({ page, server }) => {
await page.goto(server.EMPTY_PAGE);
const [response] = await Promise.all([
page.waitForNavigation(),
page.evaluate(url => window.location.href = url, server.PREFIX + '/grid.html')
]);
expect(response.ok()).toBe(true);
expect(response.url()).toContain('grid.html');
});
it('should respect timeout', async ({ page, server }) => {
const promise = page.waitForNavigation({ url: '**/frame.html', timeout: 5000 });
await page.goto(server.EMPTY_PAGE);
const error = await promise.catch(e => e);
expect(error.message).toContain('page.waitForNavigation: Timeout 5000ms exceeded.');
expect(error.message).toContain('waiting for navigation to "**/frame.html" until "load"');
expect(error.message).toContain(`navigated to "${server.EMPTY_PAGE}"`);
});
it('should work with both domcontentloaded and load', async ({ page, server }) => {
let response = null;
server.setRoute('/one-style.css', (req, res) => response = res);
const navigationPromise = page.goto(server.PREFIX + '/one-style.html');
const domContentLoadedPromise = page.waitForNavigation({
waitUntil: 'domcontentloaded'
});
let bothFired = false;
const bothFiredPromise = Promise.all([
page.waitForNavigation({ waitUntil: 'load' }),
domContentLoadedPromise
]).then(() => bothFired = true);
await server.waitForRequest('/one-style.css');
await domContentLoadedPromise;
expect(bothFired).toBe(false);
response.end();
await bothFiredPromise;
await navigationPromise;
});
it('should work with commit', async ({ page, server }) => {
server.setRoute('/empty.html', (req, res) => {
res.writeHead(200, {
'content-type': 'text/html',
'content-length': '8192'
});
// Write enought bytes of the body to trigge response received event.
res.write('
' + 'A'.repeat(4100));
res.uncork();
});
page.goto(server.EMPTY_PAGE).catch(e => {});
await page.waitForNavigation({ waitUntil: 'commit' });
});
it('should work with clicking on anchor links', async ({ page, server }) => {
await page.goto(server.EMPTY_PAGE);
await page.setContent(`foobar`);
const [response] = await Promise.all([
page.waitForNavigation(),
page.click('a'),
]);
expect(response).toBe(null);
expect(page.url()).toBe(server.EMPTY_PAGE + '#foobar');
});
it('should work with clicking on links which do not commit navigation', async ({ page, server, httpsServer, browserName }) => {
await page.goto(server.EMPTY_PAGE);
await page.setContent(`foobar`);
const [error] = await Promise.all([
page.waitForNavigation().catch(e => e),
page.click('a'),
]);
expect(error.message).toContain(expectedSSLError(browserName));
});
it('should work with history.pushState()', async ({ page, server }) => {
await page.goto(server.EMPTY_PAGE);
await page.setContent(`
SPA
`);
const [response] = await Promise.all([
page.waitForNavigation(),
page.click('a'),
]);
expect(response).toBe(null);
expect(page.url()).toBe(server.PREFIX + '/wow.html');
});
it('should work with history.replaceState()', async ({ page, server }) => {
await page.goto(server.EMPTY_PAGE);
await page.setContent(`
SPA
`);
const [response] = await Promise.all([
page.waitForNavigation(),
page.click('a'),
]);
expect(response).toBe(null);
expect(page.url()).toBe(server.PREFIX + '/replaced.html');
});
it('should work with DOM history.back()/history.forward()', async ({ page, server }) => {
await page.goto(server.EMPTY_PAGE);
await page.setContent(`
back
forward
`);
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 ({ browserName, page, server }) => {
it.fixme(browserName === 'webkit', 'WebKit issues load event in some cases, but not always');
server.setRoute('/frames/style.css', (req, res) => {});
let done = false;
page.goto(server.PREFIX + '/frames/one-frame.html').then(() => done = true).catch(() => {});
const frame = await new Promise(f => page.once('frameattached', f));
await new Promise(fulfill => page.on('framenavigated', f => {
if (f === frame)
fulfill();
}));
await frame.evaluate(() => window.stop());
expect(done).toBe(true);
});
it('should work with url match', async ({ page, server }) => {
let response1 = null;
const response1Promise = page.waitForNavigation({ url: /one-style\.html/ }).then(response => response1 = response);
let response2 = null;
const response2Promise = page.waitForNavigation({ url: /\/frame.html/ }).then(response => response2 = response);
let response3 = null;
const response3Promise = page.waitForNavigation({ url: url => url.searchParams.get('foo') === 'bar' }).then(response => response3 = response);
expect(response1).toBe(null);
expect(response2).toBe(null);
expect(response3).toBe(null);
await page.goto(server.EMPTY_PAGE);
expect(response1).toBe(null);
expect(response2).toBe(null);
expect(response3).toBe(null);
await page.goto(server.PREFIX + '/frame.html');
expect(response1).toBe(null);
await response2Promise;
expect(response2).not.toBe(null);
expect(response3).toBe(null);
await page.goto(server.PREFIX + '/one-style.html');
await response1Promise;
expect(response1).not.toBe(null);
expect(response2).not.toBe(null);
expect(response3).toBe(null);
await page.goto(server.PREFIX + '/frame.html?foo=bar');
await response3Promise;
expect(response1).not.toBe(null);
expect(response2).not.toBe(null);
expect(response3).not.toBe(null);
await page.goto(server.PREFIX + '/empty.html');
expect(response1.url()).toBe(server.PREFIX + '/one-style.html');
expect(response2.url()).toBe(server.PREFIX + '/frame.html');
expect(response3.url()).toBe(server.PREFIX + '/frame.html?foo=bar');
});
it('should work with url match for same document navigations', async ({ page, server }) => {
await page.goto(server.EMPTY_PAGE);
let resolved = false;
const waitPromise = page.waitForNavigation({ url: /third\.html/ }).then(() => resolved = true);
expect(resolved).toBe(false);
await page.evaluate(() => {
history.pushState({}, '', '/first.html');
});
expect(resolved).toBe(false);
await page.evaluate(() => {
history.pushState({}, '', '/second.html');
});
expect(resolved).toBe(false);
await page.evaluate(() => {
history.pushState({}, '', '/third.html');
});
await waitPromise;
expect(resolved).toBe(true);
});
it('should work for cross-process navigations', async ({ page, server }) => {
await page.goto(server.EMPTY_PAGE);
const waitPromise = page.waitForNavigation({ waitUntil: 'domcontentloaded' });
const url = server.CROSS_PROCESS_PREFIX + '/empty.html';
const gotoPromise = page.goto(url);
const response = await waitPromise;
expect(response.url()).toBe(url);
expect(page.url()).toBe(url);
expect(await page.evaluate('document.location.href')).toBe(url);
await gotoPromise;
});
it('should work on frame', async ({ page, server }) => {
await page.goto(server.PREFIX + '/frames/one-frame.html');
const frame = page.frames()[1];
const [response] = await Promise.all([
frame.waitForNavigation(),
frame.evaluate(url => window.location.href = url, server.PREFIX + '/grid.html')
]);
expect(response.ok()).toBe(true);
expect(response.url()).toContain('grid.html');
expect(response.frame()).toBe(frame);
expect(page.url()).toContain('/frames/one-frame.html');
});
it('should fail when frame detaches', async ({ page, server }) => {
await page.goto(server.PREFIX + '/frames/one-frame.html');
const frame = page.frames()[1];
server.setRoute('/empty.html', () => {});
const [error] = await Promise.all([
frame.waitForNavigation().catch(e => e),
page.$eval('iframe', frame => { frame.contentWindow.location.href = '/empty.html'; }),
// Make sure policy checks pass and navigation actually begins before removing the frame to avoid other errors
server.waitForRequest('/empty.html').then(() => page.$eval('iframe', frame => setTimeout(() => frame.remove(), 0)))
]);
expect(error.message).toContain('waiting for navigation until "load"');
expect(error.message).toContain('frame was detached');
});