mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
fix(chromium): close background pages on close (#6608)
This commit is contained in:
parent
d2938d0a30
commit
b52cbfdb16
@ -151,6 +151,7 @@ export abstract class BrowserContext extends SdkObject {
|
||||
abstract _doExposeBinding(binding: PageBinding): Promise<void>;
|
||||
abstract _doUpdateRequestInterception(): Promise<void>;
|
||||
abstract _doClose(): Promise<void>;
|
||||
abstract _onClosePersistent(): Promise<void>;
|
||||
|
||||
async cookies(urls: string | string[] | undefined = []): Promise<types.NetworkCookie[]> {
|
||||
if (urls && !Array.isArray(urls))
|
||||
@ -283,6 +284,7 @@ export abstract class BrowserContext extends SdkObject {
|
||||
// Close all the pages instead of the context,
|
||||
// because we cannot close the default context.
|
||||
await Promise.all(this.pages().map(page => page.close(metadata)));
|
||||
await this._onClosePersistent();
|
||||
} else {
|
||||
// Close the context.
|
||||
await this._doClose();
|
||||
|
@ -437,6 +437,15 @@ export class CRBrowserContext extends BrowserContext {
|
||||
}
|
||||
}
|
||||
|
||||
async _onClosePersistent() {
|
||||
for (const [targetId, backgroundPage] of this._browser._backgroundPages.entries()) {
|
||||
if (backgroundPage._browserContext === this && backgroundPage._initializedPage) {
|
||||
backgroundPage.didClose();
|
||||
this._browser._backgroundPages.delete(targetId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
backgroundPages(): Page[] {
|
||||
const result: Page[] = [];
|
||||
for (const backgroundPage of this._browser._backgroundPages.values()) {
|
||||
|
@ -311,6 +311,8 @@ export class FFBrowserContext extends BrowserContext {
|
||||
await this._browser._connection.send('Browser.setRequestInterception', { browserContextId: this._browserContextId, enabled: !!this._requestInterceptor });
|
||||
}
|
||||
|
||||
async _onClosePersistent() {}
|
||||
|
||||
async _doClose() {
|
||||
assert(this._browserContextId);
|
||||
await this._browser._connection.send('Browser.removeBrowserContext', { browserContextId: this._browserContextId });
|
||||
|
@ -317,6 +317,8 @@ export class WKBrowserContext extends BrowserContext {
|
||||
await (page._delegate as WKPage).updateRequestInterception();
|
||||
}
|
||||
|
||||
async _onClosePersistent() {}
|
||||
|
||||
async _doClose() {
|
||||
assert(this._browserContextId);
|
||||
await this._browser._browserSession.send('Playwright.deleteContext', { browserContextId: this._browserContextId });
|
||||
|
@ -72,8 +72,7 @@ it('should return background pages', async ({browserType, browserOptions, create
|
||||
expect(context.pages()).not.toContain(backgroundPage);
|
||||
await context.close();
|
||||
expect(context.pages().length).toBe(0);
|
||||
// TODO: the following line is flaky, uncomment once fixed.
|
||||
// expect(context.backgroundPages().length).toBe(0);
|
||||
expect(context.backgroundPages().length).toBe(0);
|
||||
});
|
||||
|
||||
it('should return background pages when recording video', async ({browserType, browserOptions, createUserDataDir, asset}, testInfo) => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user