chore: move Page.close() tests to tests/library (#36390)

This commit is contained in:
Yury Semikhatsky 2025-06-23 08:39:47 -07:00 committed by GitHub
parent a5b68a5c0e
commit 68d7f66566
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 231 additions and 266 deletions

View File

@ -0,0 +1,231 @@
/**
* Copyright 2017 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 { stripAnsi } from '../config/utils';
import { browserTest as test, expect } from '../config/browserTest';
import { kTargetClosedErrorMessage } from '../config/errors';
test('should close page with active dialog', async ({ page }) => {
await page.evaluate('"trigger builtins.setTimeout"');
await page.setContent(`<button onclick="builtins.setTimeout(() => alert(1))">alert</button>`);
void page.click('button').catch(() => {});
await page.waitForEvent('dialog');
await page.close();
});
test('should not accept dialog after close', async ({ page, mode }) => {
test.fixme(mode.startsWith('service2'), 'Times out');
const promise = page.waitForEvent('dialog');
page.evaluate(() => alert()).catch(() => {});
const dialog = await promise;
await page.close();
const e = await dialog.dismiss().catch(e => e);
expect(e.message).toContain('Target page, context or browser has been closed');
});
test('expect should not print timed out error message when page closes', async ({ page }) => {
await page.setContent('<div id=node>Text content</div>');
const [error] = await Promise.all([
expect(page.locator('div')).toHaveText('hey', { timeout: 100000 }).catch(e => e),
page.close(),
]);
expect(stripAnsi(error.message)).toContain(`expect(locator).toHaveText(expected)`);
expect(stripAnsi(error.message)).not.toContain('Timed out');
});
test('addLocatorHandler should throw when page closes', async ({ page, server }) => {
await page.goto(server.PREFIX + '/input/handle-locator.html');
await page.addLocatorHandler(page.getByText('This interstitial covers the button'), async () => {
await page.close();
});
await page.locator('#aside').hover();
await page.evaluate(() => {
(window as any).clicked = 0;
(window as any).setupAnnoyingInterstitial('mouseover', 1);
});
const error = await page.locator('#target').click().catch(e => e);
expect(error.message).toContain(kTargetClosedErrorMessage);
});
test('should reject all promises when page is closed', async ({ page }) => {
let error = null;
await Promise.all([
page.evaluate(() => new Promise(r => {})).catch(e => error = e),
page.close(),
]);
expect(error.message).toContain(kTargetClosedErrorMessage);
});
test('should set the page close state', async ({ page }) => {
expect(page.isClosed()).toBe(false);
await page.close();
expect(page.isClosed()).toBe(true);
});
test('should pass page to close event', async ({ page }) => {
const [closedPage] = await Promise.all([
page.waitForEvent('close'),
page.close()
]);
expect(closedPage).toBe(page);
});
test('should terminate network waiters', async ({ page, server }) => {
const results = await Promise.all([
page.waitForRequest(server.EMPTY_PAGE).catch(e => e),
page.waitForResponse(server.EMPTY_PAGE).catch(e => e),
page.close()
]);
for (let i = 0; i < 2; i++) {
const message = results[i].message;
expect(message).toContain(kTargetClosedErrorMessage);
expect(message).not.toContain('Timeout');
}
});
test('should be callable twice', async ({ page }) => {
await Promise.all([
page.close(),
page.close(),
]);
await page.close();
});
test('should return null if parent page has been closed', async ({ page }) => {
const [popup] = await Promise.all([
page.waitForEvent('popup'),
page.evaluate(() => window.open('about:blank')),
]);
await page.close();
const opener = await popup.opener();
expect(opener).toBe(null);
});
test('should fail with error upon disconnect', async ({ page }) => {
let error;
const waitForPromise = page.waitForEvent('download').catch(e => error = e);
await page.close();
await waitForPromise;
expect(error.message).toContain(kTargetClosedErrorMessage);
});
test('page.close should work with window.close', async function({ page }) {
const closedPromise = new Promise(x => page.on('close', x));
await page.close();
await closedPromise;
});
test('should not throw UnhandledPromiseRejection when page closes', async ({ page, browserName, isWindows }) => {
test.fixme(browserName === 'firefox' && isWindows, 'makes the next test to always timeout');
await Promise.all([
page.close(),
page.mouse.click(1, 2),
]).catch(e => {});
});
test('interrupt request.response() and request.allHeaders() on page.close', async ({ page, server, browserName }) => {
test.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/27227' });
server.setRoute('/one-style.css', (req, res) => {
res.setHeader('Content-Type', 'text/css');
});
const reqPromise = page.waitForRequest('**/one-style.css');
await page.goto(server.PREFIX + '/one-style.html', { waitUntil: 'domcontentloaded' });
const req = await reqPromise;
const respPromise = req.response().catch(e => e);
const headersPromise = req.allHeaders().catch(e => e);
await page.close();
expect((await respPromise).message).toContain(kTargetClosedErrorMessage);
// All headers are the same as "provisional" headers in Firefox.
if (browserName === 'firefox')
expect((await headersPromise)['user-agent']).toBeTruthy();
else
expect((await headersPromise).message).toContain(kTargetClosedErrorMessage);
});
test('should not treat navigations as new popups', async ({ page, server }) => {
await page.goto(server.EMPTY_PAGE);
await page.setContent('<a target=_blank rel=noopener href="/one-style.html">yo</a>');
const [popup] = await Promise.all([
page.waitForEvent('popup'),
page.click('a'),
]);
let badSecondPopup = false;
page.on('popup', () => badSecondPopup = true);
await popup.goto(server.CROSS_PROCESS_PREFIX + '/empty.html');
await page.close();
expect(badSecondPopup).toBe(false);
});
test('should not result in unhandled rejection', async ({ page }) => {
const closedPromise = page.waitForEvent('close');
await page.exposeFunction('foo', async () => {
await page.close();
});
await page.evaluate(() => {
window.builtins.setTimeout(() => (window as any).foo(), 0);
return undefined;
});
await closedPromise;
// Make a round-trip to be sure we did not throw immediately after closing.
expect(await page.evaluate('1 + 1').catch(e => e)).toBeInstanceOf(Error);
});
test('should reject response.finished if page closes', async ({ page, server }) => {
await page.goto(server.EMPTY_PAGE);
server.setRoute('/get', (req, res) => {
// In Firefox, |fetch| will be hanging until it receives |Content-Type| header
// from server.
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
res.write('hello ');
});
// send request and wait for server response
const [pageResponse] = await Promise.all([
page.waitForEvent('response'),
page.evaluate(() => fetch('./get', { method: 'GET' })),
]);
const finishPromise = pageResponse.finished().catch(e => e);
await page.close();
const error = await finishPromise;
expect(error.message).toContain('closed');
});
test('should not throw when continuing while page is closing', async ({ page, server }) => {
let done;
await page.route('**/*', async route => {
done = Promise.all([
void route.continue(),
page.close(),
]);
});
await page.goto(server.EMPTY_PAGE).catch(e => e);
await done;
});
test('should not throw when continuing after page is closed', async ({ page, server }) => {
let done;
await page.route('**/*', async route => {
await page.close();
done = route.continue();
});
const error = await page.goto(server.EMPTY_PAGE).catch(e => e);
await done;
expect(error).toBeInstanceOf(Error);
});

View File

@ -41,16 +41,6 @@ test('should print timed out error message when value does not match with imposs
expect(stripAnsi(error.message)).toContain(`Timed out 1ms waiting for expect(locator).toHaveText(expected)`);
});
test('should not print timed out error message when page closes', async ({ page }) => {
await page.setContent('<div id=node>Text content</div>');
const [error] = await Promise.all([
expect(page.locator('div')).toHaveText('hey', { timeout: 100000 }).catch(e => e),
page.close(),
]);
expect(stripAnsi(error.message)).toContain(`expect(locator).toHaveText(expected)`);
expect(stripAnsi(error.message)).not.toContain('Timed out');
});
test('should have timeout error name', async ({ page }) => {
const error = await page.waitForSelector('#not-found', { timeout: 1 }).catch(e => e);
expect(error.name).toBe('TimeoutError');

View File

@ -15,7 +15,6 @@
*/
import { test, expect } from './pageTest';
import { kTargetClosedErrorMessage } from '../config/errors';
test('should work', async ({ page, server }) => {
await page.goto(server.PREFIX + '/input/handle-locator.html');
@ -121,23 +120,6 @@ test('should not work with force:true', async ({ page, server }) => {
expect(await page.evaluate('window.clicked')).toBe(undefined);
});
test('should throw when page closes', async ({ page, server, isAndroid }) => {
test.fixme(isAndroid, 'GPU process crash: https://issues.chromium.org/issues/324909825');
await page.goto(server.PREFIX + '/input/handle-locator.html');
await page.addLocatorHandler(page.getByText('This interstitial covers the button'), async () => {
await page.close();
});
await page.locator('#aside').hover();
await page.evaluate(() => {
(window as any).clicked = 0;
(window as any).setupAnnoyingInterstitial('mouseover', 1);
});
const error = await page.locator('#target').click().catch(e => e);
expect(error.message).toContain(kTargetClosedErrorMessage);
});
test('should throw when handler times out', async ({ page, server }) => {
await page.goto(server.PREFIX + '/input/handle-locator.html');

View File

@ -15,66 +15,8 @@
* limitations under the License.
*/
import { kTargetClosedErrorMessage } from '../config/errors';
import { test as it, expect } from './pageTest';
it('should reject all promises when page is closed', async ({ page, isWebView2, isAndroid }) => {
it.skip(isWebView2, 'Page.close() is not supported in WebView2');
it.fixme(isAndroid, '"Target crashed" instead of "Target closed"');
let error = null;
await Promise.all([
page.evaluate(() => new Promise(r => {})).catch(e => error = e),
page.close(),
]);
expect(error.message).toContain(kTargetClosedErrorMessage);
});
it('should set the page close state', async ({ page, isWebView2 }) => {
it.skip(isWebView2, 'Page.close() is not supported in WebView2');
expect(page.isClosed()).toBe(false);
await page.close();
expect(page.isClosed()).toBe(true);
});
it('should pass page to close event', async ({ page, isAndroid, isWebView2 }) => {
it.fixme(isAndroid);
it.skip(isWebView2, 'Page.close() is not supported in WebView2');
const [closedPage] = await Promise.all([
page.waitForEvent('close'),
page.close()
]);
expect(closedPage).toBe(page);
});
it('should terminate network waiters', async ({ page, server, isAndroid, isWebView2 }) => {
it.fixme(isAndroid);
it.skip(isWebView2, 'Page.close() is not supported in WebView2');
const results = await Promise.all([
page.waitForRequest(server.EMPTY_PAGE).catch(e => e),
page.waitForResponse(server.EMPTY_PAGE).catch(e => e),
page.close()
]);
for (let i = 0; i < 2; i++) {
const message = results[i].message;
expect(message).toContain(kTargetClosedErrorMessage);
expect(message).not.toContain('Timeout');
}
});
it('should be callable twice', async ({ page, isWebView2 }) => {
it.skip(isWebView2, 'Page.close() is not supported in WebView2');
await Promise.all([
page.close(),
page.close(),
]);
await page.close();
});
it('should fire load when expected', async ({ page }) => {
await Promise.all([
page.goto('about:blank'),
@ -101,18 +43,6 @@ it('should provide access to the opener page', async ({ page }) => {
expect(opener).toBe(page);
});
it('should return null if parent page has been closed', async ({ page, isWebView2 }) => {
it.skip(isWebView2, 'Page.close() is not supported in WebView2');
const [popup] = await Promise.all([
page.waitForEvent('popup'),
page.evaluate(() => window.open('about:blank')),
]);
await page.close();
const opener = await popup.opener();
expect(opener).toBe(null);
});
it('should fire domcontentloaded when expected', async ({ page }) => {
const navigatedPromise = page.goto('about:blank');
await page.waitForEvent('domcontentloaded');
@ -135,17 +65,6 @@ it('should pass self as argument to load event', async ({ page }) => {
expect(eventArg).toBe(page);
});
it('should fail with error upon disconnect', async ({ page, isAndroid, isWebView2 }) => {
it.skip(isWebView2, 'Page.close() is not supported in WebView2');
it.fixme(isAndroid);
let error;
const waitForPromise = page.waitForEvent('download').catch(e => error = e);
await page.close();
await waitForPromise;
expect(error.message).toContain(kTargetClosedErrorMessage);
});
it('page.url should work', async ({ page, server }) => {
expect(page.url()).toBe('about:blank');
await page.goto(server.EMPTY_PAGE);
@ -175,14 +94,6 @@ it('page.close should work with window.close', async function({ page }) {
await closedPromise;
});
it('page.close should work with page.close', async function({ page, isWebView2 }) {
it.skip(isWebView2, 'Page.close() is not supported in WebView2');
const closedPromise = new Promise(x => page.on('close', x));
await page.close();
await closedPromise;
});
it('page.frame should respect name', async function({ page }) {
await page.setContent(`<iframe name=target></iframe>`);
expect(page.frame({ name: 'bogus' })).toBe(null);

View File

@ -85,16 +85,6 @@ it('should click on a span with an inline element inside', async ({ page }) => {
expect(await page.evaluate('CLICKED')).toBe(42);
});
it('should not throw UnhandledPromiseRejection when page closes', async ({ page, isWebView2, browserName, isWindows }) => {
it.skip(isWebView2, 'Page.close() is not supported in WebView2');
it.fixme(browserName === 'firefox' && isWindows, 'makes the next test to always timeout');
await Promise.all([
page.close(),
page.mouse.click(1, 2),
]).catch(e => {});
});
it('should click the aligned 1x1 div', async ({ page }) => {
await page.setContent(`<div style="width: 1px; height: 1px;" onclick="window.__clicked = true"></div>`);
await page.click('div');

View File

@ -1,38 +0,0 @@
/**
* Copyright 2017 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';
it.skip(({ isWebView2 }) => isWebView2, 'Page.close() is not supported in WebView2');
it('should close page with active dialog', async ({ page }) => {
await page.evaluate('"trigger builtins.setTimeout"');
await page.setContent(`<button onclick="builtins.setTimeout(() => alert(1))">alert</button>`);
void page.click('button').catch(() => {});
await page.waitForEvent('dialog');
await page.close();
});
it('should not accept dialog after close', async ({ page, mode }) => {
it.fixme(mode.startsWith('service2'), 'Times out');
const promise = page.waitForEvent('dialog');
page.evaluate(() => alert()).catch(() => {});
const dialog = await promise;
await page.close();
const e = await dialog.dismiss().catch(e => e);
expect(e.message).toContain('Target page, context or browser has been closed');
});

View File

@ -17,7 +17,6 @@
import type { ServerResponse } from 'http';
import { test as it, expect } from './pageTest';
import { kTargetClosedErrorMessage } from '../config/errors';
it('Page.Events.Request @smoke', async ({ page, server }) => {
const requests = [];
@ -138,23 +137,3 @@ it('should resolve responses after a navigation', async ({ page, server, browser
// the response should resolve to null, because the page navigated.
expect(await responsePromise).toBe(null);
});
it('interrupt request.response() and request.allHeaders() on page.close', async ({ page, server, browserName }) => {
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/27227' });
server.setRoute('/one-style.css', (req, res) => {
res.setHeader('Content-Type', 'text/css');
});
const reqPromise = page.waitForRequest('**/one-style.css');
await page.goto(server.PREFIX + '/one-style.html', { waitUntil: 'domcontentloaded' });
const req = await reqPromise;
const respPromise = req.response().catch(e => e);
const headersPromise = req.allHeaders().catch(e => e);
await page.close();
expect((await respPromise).message).toContain(kTargetClosedErrorMessage);
// All headers are the same as "provisional" headers in Firefox.
if (browserName === 'firefox')
expect((await headersPromise)['user-agent']).toBeTruthy();
else
expect((await headersPromise).message).toContain(kTargetClosedErrorMessage);
});

View File

@ -146,22 +146,6 @@ it('should work with clicking target=_blank and rel=noopener', async ({ page, se
expect(await popup.evaluate(() => !!window.opener)).toBe(false);
});
it('should not treat navigations as new popups', async ({ page, server, isWebView2 }) => {
it.skip(isWebView2, 'Page.close() is not supported in WebView2');
await page.goto(server.EMPTY_PAGE);
await page.setContent('<a target=_blank rel=noopener href="/one-style.html">yo</a>');
const [popup] = await Promise.all([
page.waitForEvent('popup'),
page.click('a'),
]);
let badSecondPopup = false;
page.on('popup', () => badSecondPopup = true);
await popup.goto(server.CROSS_PROCESS_PREFIX + '/empty.html');
await page.close();
expect(badSecondPopup).toBe(false);
});
it('should report popup opened from iframes', async ({ page, server, browserName }) => {
await page.goto(server.PREFIX + '/frames/two-frames.html');
const frame = page.frame('uno');

View File

@ -221,23 +221,6 @@ it('exposeBindingHandle should throw for multiple arguments', async ({ page }) =
expect(error.message).toContain('exposeBindingHandle supports a single argument, 2 received');
});
it('should not result in unhandled rejection', async ({ page, isAndroid, isWebView2 }) => {
it.fixme(isAndroid);
it.skip(isWebView2, 'Page.close() is not supported in WebView2');
const closedPromise = page.waitForEvent('close');
await page.exposeFunction('foo', async () => {
await page.close();
});
await page.evaluate(() => {
window.builtins.setTimeout(() => (window as any).foo(), 0);
return undefined;
});
await closedPromise;
// Make a round-trip to be sure we did not throw immediately after closing.
expect(await page.evaluate('1 + 1').catch(e => e)).toBeInstanceOf(Error);
});
it('exposeBinding(handle) should work with element handles', async ({ page }) => {
let cb;
const promise = new Promise(f => cb = f);

View File

@ -109,26 +109,6 @@ it('should wait until response completes', async ({ page, server }) => {
expect(await responseText).toBe('hello world!');
});
it('should reject response.finished if page closes', async ({ page, server }) => {
await page.goto(server.EMPTY_PAGE);
server.setRoute('/get', (req, res) => {
// In Firefox, |fetch| will be hanging until it receives |Content-Type| header
// from server.
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
res.write('hello ');
});
// send request and wait for server response
const [pageResponse] = await Promise.all([
page.waitForEvent('response'),
page.evaluate(() => fetch('./get', { method: 'GET' })),
]);
const finishPromise = pageResponse.finished().catch(e => e);
await page.close();
const error = await finishPromise;
expect(error.message).toContain('closed');
});
it('should return json', async ({ page, server }) => {
const response = await page.goto(server.PREFIX + '/simple.json');
expect(await response.json()).toEqual({ foo: 'bar' });

View File

@ -150,33 +150,6 @@ it('should not allow changing protocol when overriding url', async ({ page, serv
expect(error.message).toContain('New URL must have same protocol as overridden URL');
});
it('should not throw when continuing while page is closing', async ({ page, server, isWebView2 }) => {
it.skip(isWebView2, 'Page.close() is not supported in WebView2');
let done;
await page.route('**/*', async route => {
done = Promise.all([
void route.continue(),
page.close(),
]);
});
await page.goto(server.EMPTY_PAGE).catch(e => e);
await done;
});
it('should not throw when continuing after page is closed', async ({ page, server, isWebView2 }) => {
it.skip(isWebView2, 'Page.close() is not supported in WebView2');
let done;
await page.route('**/*', async route => {
await page.close();
done = route.continue();
});
const error = await page.goto(server.EMPTY_PAGE).catch(e => e);
await done;
expect(error).toBeInstanceOf(Error);
});
it('should not throw if request was cancelled by the page', async ({ page, server, browserName }) => {
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/28490' });
let interceptCallback;