fix(chromium): close background pages on close (#6608)

This commit is contained in:
Max Schmitt 2021-05-18 18:07:45 +02:00 committed by GitHub
parent d2938d0a30
commit b52cbfdb16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 16 additions and 2 deletions

View File

@ -151,6 +151,7 @@ export abstract class BrowserContext extends SdkObject {
abstract _doExposeBinding(binding: PageBinding): Promise<void>; abstract _doExposeBinding(binding: PageBinding): Promise<void>;
abstract _doUpdateRequestInterception(): Promise<void>; abstract _doUpdateRequestInterception(): Promise<void>;
abstract _doClose(): Promise<void>; abstract _doClose(): Promise<void>;
abstract _onClosePersistent(): Promise<void>;
async cookies(urls: string | string[] | undefined = []): Promise<types.NetworkCookie[]> { async cookies(urls: string | string[] | undefined = []): Promise<types.NetworkCookie[]> {
if (urls && !Array.isArray(urls)) if (urls && !Array.isArray(urls))
@ -283,6 +284,7 @@ export abstract class BrowserContext extends SdkObject {
// Close all the pages instead of the context, // Close all the pages instead of the context,
// because we cannot close the default context. // because we cannot close the default context.
await Promise.all(this.pages().map(page => page.close(metadata))); await Promise.all(this.pages().map(page => page.close(metadata)));
await this._onClosePersistent();
} else { } else {
// Close the context. // Close the context.
await this._doClose(); await this._doClose();

View File

@ -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[] { backgroundPages(): Page[] {
const result: Page[] = []; const result: Page[] = [];
for (const backgroundPage of this._browser._backgroundPages.values()) { for (const backgroundPage of this._browser._backgroundPages.values()) {

View File

@ -311,6 +311,8 @@ export class FFBrowserContext extends BrowserContext {
await this._browser._connection.send('Browser.setRequestInterception', { browserContextId: this._browserContextId, enabled: !!this._requestInterceptor }); await this._browser._connection.send('Browser.setRequestInterception', { browserContextId: this._browserContextId, enabled: !!this._requestInterceptor });
} }
async _onClosePersistent() {}
async _doClose() { async _doClose() {
assert(this._browserContextId); assert(this._browserContextId);
await this._browser._connection.send('Browser.removeBrowserContext', { browserContextId: this._browserContextId }); await this._browser._connection.send('Browser.removeBrowserContext', { browserContextId: this._browserContextId });

View File

@ -317,6 +317,8 @@ export class WKBrowserContext extends BrowserContext {
await (page._delegate as WKPage).updateRequestInterception(); await (page._delegate as WKPage).updateRequestInterception();
} }
async _onClosePersistent() {}
async _doClose() { async _doClose() {
assert(this._browserContextId); assert(this._browserContextId);
await this._browser._browserSession.send('Playwright.deleteContext', { browserContextId: this._browserContextId }); await this._browser._browserSession.send('Playwright.deleteContext', { browserContextId: this._browserContextId });

View File

@ -72,8 +72,7 @@ it('should return background pages', async ({browserType, browserOptions, create
expect(context.pages()).not.toContain(backgroundPage); expect(context.pages()).not.toContain(backgroundPage);
await context.close(); await context.close();
expect(context.pages().length).toBe(0); 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) => { it('should return background pages when recording video', async ({browserType, browserOptions, createUserDataDir, asset}, testInfo) => {