2020-08-05 02:32:10 +03:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2021-05-06 17:08:22 +03:00
|
|
|
import { test as it, expect } from './pageTest';
|
2020-08-12 01:50:53 +03:00
|
|
|
import url from 'url';
|
2020-08-05 02:32:10 +03:00
|
|
|
|
2022-03-10 21:42:52 +03:00
|
|
|
it('page.goBack should work @smoke', async ({ page, server }) => {
|
2020-08-05 02:32:10 +03:00
|
|
|
expect(await page.goBack()).toBe(null);
|
|
|
|
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.goto(server.PREFIX + '/grid.html');
|
|
|
|
|
|
|
|
let response = await page.goBack();
|
|
|
|
expect(response.ok()).toBe(true);
|
|
|
|
expect(response.url()).toContain(server.EMPTY_PAGE);
|
|
|
|
|
|
|
|
response = await page.goForward();
|
|
|
|
expect(response.ok()).toBe(true);
|
|
|
|
expect(response.url()).toContain('/grid.html');
|
|
|
|
|
|
|
|
response = await page.goForward();
|
|
|
|
expect(response).toBe(null);
|
|
|
|
});
|
|
|
|
|
2021-09-27 19:58:08 +03:00
|
|
|
it('page.goBack should work with HistoryAPI', async ({ page, server }) => {
|
2020-08-05 02:32:10 +03: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');
|
|
|
|
});
|
|
|
|
|
2023-07-26 02:47:04 +03:00
|
|
|
it('page.goBack should work for file urls', async ({ page, server, asset, browserName, platform, isAndroid, mode }) => {
|
2021-04-09 17:59:09 +03:00
|
|
|
it.skip(isAndroid, 'No files on Android');
|
2023-07-26 02:47:04 +03:00
|
|
|
it.skip(mode.startsWith('service'));
|
2021-04-05 05:32:14 +03:00
|
|
|
|
2022-08-26 18:56:31 +03:00
|
|
|
const url1 = url.pathToFileURL(asset('consolelog.html')).href;
|
|
|
|
const url2 = server.PREFIX + '/consolelog.html';
|
|
|
|
await Promise.all([
|
|
|
|
page.waitForEvent('console', message => message.text() === 'here:' + url1),
|
|
|
|
page.goto(url1),
|
|
|
|
]);
|
2020-08-05 02:32:10 +03:00
|
|
|
await page.setContent(`<a href='${url2}'>url2</a>`);
|
|
|
|
expect(page.url().toLowerCase()).toBe(url1.toLowerCase());
|
|
|
|
|
2022-08-26 18:56:31 +03:00
|
|
|
await Promise.all([
|
|
|
|
page.waitForEvent('console', message => message.text() === 'here:' + url2),
|
|
|
|
page.click('a'),
|
|
|
|
]);
|
2020-08-05 02:32:10 +03:00
|
|
|
expect(page.url()).toBe(url2);
|
|
|
|
|
2022-08-26 18:56:31 +03:00
|
|
|
await Promise.all([
|
|
|
|
page.waitForEvent('console', message => message.text() === 'here:' + url1),
|
|
|
|
page.goBack(),
|
|
|
|
]);
|
2020-08-05 02:32:10 +03:00
|
|
|
expect(page.url().toLowerCase()).toBe(url1.toLowerCase());
|
|
|
|
// Should be able to evaluate in the new context, and
|
|
|
|
// not reach for the old cross-process one.
|
|
|
|
expect(await page.evaluate(() => window.scrollX)).toBe(0);
|
|
|
|
// Should be able to screenshot.
|
|
|
|
await page.screenshot();
|
|
|
|
|
2022-08-26 18:56:31 +03:00
|
|
|
await Promise.all([
|
|
|
|
page.waitForEvent('console', message => message.text() === 'here:' + url2),
|
|
|
|
page.goForward(),
|
|
|
|
]);
|
2020-08-05 02:32:10 +03:00
|
|
|
expect(page.url()).toBe(url2);
|
|
|
|
expect(await page.evaluate(() => window.scrollX)).toBe(0);
|
|
|
|
await page.screenshot();
|
|
|
|
});
|
|
|
|
|
2022-10-04 05:13:28 +03:00
|
|
|
it('goBack/goForward should work with bfcache-able pages', async ({ page, server }) => {
|
2024-08-07 16:20:12 +03:00
|
|
|
await page.goto(server.PREFIX + '/cached/bfcached.html');
|
|
|
|
await page.setContent(`<a href=${JSON.stringify(server.PREFIX + '/cached/bfcached.html?foo')}>click me</a>`);
|
2022-10-04 05:13:28 +03:00
|
|
|
await page.click('a');
|
|
|
|
|
|
|
|
let response = await page.goBack();
|
2024-08-07 16:20:12 +03:00
|
|
|
expect(response.url()).toBe(server.PREFIX + '/cached/bfcached.html');
|
|
|
|
// BFCache should be disabled.
|
|
|
|
expect(await page.evaluate('window.didShow')).toEqual({ persisted: false });
|
2022-10-04 05:13:28 +03:00
|
|
|
|
|
|
|
response = await page.goForward();
|
2024-08-07 16:20:12 +03:00
|
|
|
expect(response.url()).toBe(server.PREFIX + '/cached/bfcached.html?foo');
|
2022-10-04 05:13:28 +03:00
|
|
|
});
|
|
|
|
|
2021-09-27 19:58:08 +03:00
|
|
|
it('page.reload should work', async ({ page, server }) => {
|
2020-08-05 02:32:10 +03:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2020-08-28 14:20:29 +03:00
|
|
|
await page.evaluate(() => window['_foo'] = 10);
|
2020-08-05 02:32:10 +03:00
|
|
|
await page.reload();
|
2020-08-28 14:20:29 +03:00
|
|
|
expect(await page.evaluate(() => window['_foo'])).toBe(undefined);
|
2020-08-05 02:32:10 +03:00
|
|
|
});
|
|
|
|
|
2021-09-27 19:58:08 +03:00
|
|
|
it('page.reload should work with data url', async ({ page, server }) => {
|
2020-08-05 02:32:10 +03:00
|
|
|
await page.goto('data:text/html,hello');
|
|
|
|
expect(await page.content()).toContain('hello');
|
|
|
|
expect(await page.reload()).toBe(null);
|
|
|
|
expect(await page.content()).toContain('hello');
|
|
|
|
});
|
2021-01-23 02:58:53 +03:00
|
|
|
|
2021-09-27 19:58:08 +03:00
|
|
|
it('page.reload during renderer-initiated navigation', async ({ page, server }) => {
|
2021-01-23 02:58:53 +03:00
|
|
|
await page.goto(server.PREFIX + '/one-style.html');
|
|
|
|
await page.setContent(`<form method='POST' action='/post'>Form is here<input type='submit'></form>`);
|
|
|
|
server.setRoute('/post', (req, res) => {});
|
|
|
|
|
|
|
|
let callback;
|
|
|
|
const reloadFailedPromise = new Promise(f => callback = f);
|
|
|
|
page.once('request', async () => {
|
|
|
|
await page.reload().catch(e => {});
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
const clickPromise = page.click('input[type=submit]').catch(e => {});
|
|
|
|
await reloadFailedPromise;
|
|
|
|
await clickPromise;
|
|
|
|
|
|
|
|
// Form submit should be canceled, and reload should eventually arrive
|
|
|
|
// to the original one-style.html.
|
|
|
|
await page.waitForSelector('text=hello');
|
|
|
|
});
|
|
|
|
|
2022-08-12 23:48:47 +03:00
|
|
|
it('page.reload should not resolve with same-document navigation', async ({ page, server }) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
// 1. Make sure execution contexts are ready for fast evaluate.
|
|
|
|
await page.evaluate('1');
|
|
|
|
|
|
|
|
// 2. Stall the reload request.
|
|
|
|
let response;
|
|
|
|
server.setRoute('/empty.html', (req, res) => { response = res; });
|
|
|
|
const requestPromise = server.waitForRequest('/empty.html');
|
|
|
|
|
|
|
|
// 3. Trigger push state that could resolve the reload.
|
|
|
|
page.evaluate(() => {
|
|
|
|
window.history.pushState({}, '');
|
|
|
|
}).catch(() => {});
|
|
|
|
|
|
|
|
// 4. Trigger the reload, it should not resolve.
|
|
|
|
const reloadPromise = page.reload();
|
|
|
|
|
|
|
|
// 5. Trigger push state again, for the good measure :)
|
|
|
|
page.evaluate(() => {
|
|
|
|
window.history.pushState({}, '');
|
|
|
|
}).catch(() => {});
|
|
|
|
|
|
|
|
// 5. Serve the request, it should resolve the reload.
|
|
|
|
await requestPromise;
|
|
|
|
response.end('hello');
|
|
|
|
|
|
|
|
// 6. Check the reload response.
|
|
|
|
const gotResponse = await reloadPromise;
|
|
|
|
expect(await gotResponse.text()).toBe('hello');
|
|
|
|
});
|
|
|
|
|
2022-08-19 03:35:02 +03:00
|
|
|
it('page.reload should work with same origin redirect', async ({ page, server }) => {
|
|
|
|
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/16147' });
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
server.setRedirect('/empty.html', server.PREFIX + '/title.html');
|
|
|
|
await page.reload();
|
|
|
|
await expect(page).toHaveURL(server.PREFIX + '/title.html');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('page.reload should work with cross-origin redirect', async ({ page, server, browserName }) => {
|
|
|
|
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/16147' });
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
server.setRedirect('/empty.html', server.CROSS_PROCESS_PREFIX + '/title.html');
|
|
|
|
await page.reload();
|
|
|
|
await expect(page).toHaveURL(server.CROSS_PROCESS_PREFIX + '/title.html');
|
|
|
|
});
|
|
|
|
|
2023-03-02 05:56:30 +03:00
|
|
|
it('page.reload should work on a page with a hash', async ({ page, server }) => {
|
2023-02-23 15:22:22 +03:00
|
|
|
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/21145' });
|
|
|
|
await page.goto(server.EMPTY_PAGE + '#hash');
|
|
|
|
await page.reload();
|
|
|
|
await expect(page).toHaveURL(server.EMPTY_PAGE + '#hash');
|
|
|
|
});
|
|
|
|
|
2023-03-10 20:25:54 +03:00
|
|
|
it('page.reload should work on a page with a hash at the end', async ({ page, server }) => {
|
2023-03-07 19:53:30 +03:00
|
|
|
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/21430' });
|
|
|
|
await page.goto(server.EMPTY_PAGE + '#');
|
|
|
|
await page.reload();
|
|
|
|
await expect(page).toHaveURL(server.EMPTY_PAGE + '#');
|
|
|
|
});
|
|
|
|
|
2021-09-27 19:58:08 +03:00
|
|
|
it('page.goBack during renderer-initiated navigation', async ({ page, server }) => {
|
2021-01-23 02:58:53 +03:00
|
|
|
await page.goto(server.PREFIX + '/one-style.html');
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.setContent(`<form method='POST' action='/post'>Form is here<input type='submit'></form>`);
|
|
|
|
server.setRoute('/post', (req, res) => {});
|
|
|
|
|
|
|
|
let callback;
|
|
|
|
const reloadFailedPromise = new Promise(f => callback = f);
|
|
|
|
page.once('request', async () => {
|
|
|
|
await page.goBack().catch(e => {});
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
const clickPromise = page.click('input[type=submit]').catch(e => {});
|
|
|
|
await reloadFailedPromise;
|
|
|
|
await clickPromise;
|
|
|
|
|
|
|
|
// Form submit should be canceled, and goBack should eventually arrive
|
|
|
|
// to the original one-style.html.
|
|
|
|
await page.waitForSelector('text=hello');
|
|
|
|
});
|
|
|
|
|
2021-09-27 19:58:08 +03:00
|
|
|
it('page.goForward during renderer-initiated navigation', async ({ page, server }) => {
|
2021-01-23 02:58:53 +03:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.goto(server.PREFIX + '/one-style.html');
|
|
|
|
await page.goBack();
|
|
|
|
|
|
|
|
await page.setContent(`<form method='POST' action='/post'>Form is here<input type='submit'></form>`);
|
|
|
|
server.setRoute('/post', (req, res) => {});
|
|
|
|
|
|
|
|
let callback;
|
|
|
|
const reloadFailedPromise = new Promise(f => callback = f);
|
|
|
|
page.once('request', async () => {
|
|
|
|
await page.goForward().catch(e => {});
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
const clickPromise = page.click('input[type=submit]').catch(e => {});
|
|
|
|
await reloadFailedPromise;
|
|
|
|
await clickPromise;
|
|
|
|
|
|
|
|
// Form submit should be canceled, and goForward should eventually arrive
|
|
|
|
// to the original one-style.html.
|
|
|
|
await page.waitForSelector('text=hello');
|
|
|
|
});
|
2023-03-01 03:05:12 +03:00
|
|
|
|
|
|
|
it('regression test for issue 20791', async ({ page, server }) => {
|
|
|
|
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/20791' });
|
2024-06-17 19:17:38 +03:00
|
|
|
it.skip(process.env.PW_CLOCK === 'frozen');
|
2023-03-01 03:05:12 +03:00
|
|
|
server.setRoute('/iframe.html', (req, res) => {
|
|
|
|
res.writeHead(200, { 'content-type': 'text/html; charset=utf-8' });
|
|
|
|
// iframe access parent frame to log a value from it.
|
|
|
|
res.end(`
|
|
|
|
<!doctype html>
|
|
|
|
<script type="text/javascript">
|
|
|
|
console.log(window.parent.foo);
|
|
|
|
</script>
|
|
|
|
`);
|
|
|
|
});
|
|
|
|
server.setRoute('/main.html', (req, res) => {
|
|
|
|
res.writeHead(200, { 'content-type': 'text/html; charset=utf-8' });
|
|
|
|
res.end(`
|
|
|
|
<!doctype html>
|
|
|
|
<iframe id=myframe src="about:blank"></iframe>
|
|
|
|
<script type="text/javascript">
|
|
|
|
setTimeout(() => window.foo = 'foo', 0);
|
|
|
|
setTimeout(() => myframe.contentDocument.location.href = '${server.PREFIX}/iframe.html', 0);
|
|
|
|
</script>
|
|
|
|
`);
|
|
|
|
});
|
|
|
|
const messages = [];
|
|
|
|
page.on('console', msg => messages.push(msg.text()));
|
|
|
|
await page.goto(server.PREFIX + '/main.html');
|
|
|
|
await expect.poll(() => messages).toEqual(['foo']);
|
|
|
|
await page.reload();
|
|
|
|
await expect.poll(() => messages).toEqual(['foo', 'foo']);
|
|
|
|
});
|
2023-03-01 21:52:55 +03:00
|
|
|
|
|
|
|
it('should reload proper page', async ({ page, server }) => {
|
|
|
|
let mainRequest = 0, popupRequest = 0;
|
|
|
|
server.setRoute('/main.html', (req, res) => {
|
|
|
|
res.writeHead(200, { 'content-type': 'text/html; charset=utf-8' });
|
|
|
|
res.end(`<!doctype html><h1>main: ${++mainRequest}</h1>`);
|
|
|
|
});
|
|
|
|
server.setRoute('/popup.html', (req, res) => {
|
|
|
|
res.writeHead(200, { 'content-type': 'text/html; charset=utf-8' });
|
|
|
|
res.end(`<!doctype html><h1>popup: ${++popupRequest}</h1>`);
|
|
|
|
});
|
|
|
|
await page.goto(server.PREFIX + '/main.html');
|
|
|
|
const [popup] = await Promise.all([
|
|
|
|
page.waitForEvent('popup'),
|
|
|
|
page.evaluate(() => window.open('/popup.html')),
|
|
|
|
]);
|
|
|
|
await expect(page.locator('h1')).toHaveText('main: 1');
|
|
|
|
await expect(popup.locator('h1')).toHaveText('popup: 1');
|
|
|
|
|
|
|
|
await page.reload();
|
|
|
|
await expect(page.locator('h1')).toHaveText('main: 2');
|
|
|
|
await expect(popup.locator('h1')).toHaveText('popup: 1');
|
|
|
|
|
|
|
|
await popup.reload();
|
|
|
|
await expect(page.locator('h1')).toHaveText('main: 2');
|
|
|
|
await expect(popup.locator('h1')).toHaveText('popup: 2');
|
|
|
|
});
|