Skip to content

feat: add persistent browser context handling #200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class Context {
readonly options: ContextOptions;
private _browser: playwright.Browser | undefined;
private _browserContext: playwright.BrowserContext | undefined;
private _persistentContext: playwright.BrowserContext | null = null;
private _tabs: Tab[] = [];
private _currentTab: Tab | undefined;
private _modalStates: (ModalState & { tab: Tab })[] = [];
Expand Down Expand Up @@ -215,19 +216,29 @@ ${code.join('\n')}
return;
const browserContext = this._browserContext;
const browser = this._browser;
const persistentContext = this._persistentContext;
this._browserContext = undefined;
this._browser = undefined;
this._persistentContext = null;

await browserContext?.close().then(async () => {
await browser?.close();
}).catch(() => {});
await Promise.all([
browserContext?.close(),
browser?.close(),
persistentContext?.close()
]).catch(() => {});
}

private async _ensureBrowserContext() {
if (!this._browserContext) {
const context = await this._createBrowserContext();
this._browser = context.browser;
this._browserContext = context.browserContext;
if (this._persistentContext) {
this._browserContext = this._persistentContext;
} else {
const context = await this._createBrowserContext();
this._browser = context.browser;
this._browserContext = context.browserContext;
if (!this.options.remoteEndpoint && !this.options.cdpEndpoint)
this._persistentContext = this._browserContext;
}
for (const page of this._browserContext.pages())
this._onPageCreated(page);
this._browserContext.on('page', page => this._onPageCreated(page));
Expand All @@ -253,14 +264,10 @@ ${code.join('\n')}
return { browser, browserContext };
}

const browserContext = await this._launchPersistentContext();
return { browserContext };
}

private async _launchPersistentContext(): Promise<playwright.BrowserContext> {
try {
const browserType = this.options.browserName ? playwright[this.options.browserName] : playwright.chromium;
return await browserType.launchPersistentContext(this.options.userDataDir, this.options.launchOptions);
const browserContext = await browserType.launchPersistentContext(this.options.userDataDir, this.options.launchOptions);
return { browserContext };
} catch (error: any) {
if (error.message.includes('Executable doesn\'t exist'))
throw new Error(`Browser specified in your config is not installed. Either install it (likely) or change the config.`);
Expand Down