fix: allow newTab() in persistent context (--extension/--profile) mode (#731)

When launched with --extension or --profile, launchPersistentContext()
is used which sets isPersistentContext=true but leaves this.browser as
null. The guard in newTab() checked !this.browser, causing a false
"Browser not launched" error even though the browser was running.

Replace !this.browser with !this.isLaunched(), which already accounts
for both launch paths (browser !== null || isPersistentContext).

Also improve the error message in newWindow() to clarify that it is
not supported in persistent context mode, since it requires a Browser
object to create a new context.

Fixes #411
This commit is contained in:
mikewong23571
2026-03-12 13:55:55 -05:00
committed by GitHub
parent 01172eaa44
commit c562ef5bb7
2 changed files with 32 additions and 2 deletions
+26
View File
@@ -1243,6 +1243,32 @@ describe('BrowserManager', () => {
});
});
describe('BrowserManager (persistent context / --profile mode)', () => {
let profileBrowser: BrowserManager;
let tmpProfileDir: string;
beforeAll(async () => {
tmpProfileDir = path.join(os.tmpdir(), `agent-browser-test-profile-${Date.now()}`);
profileBrowser = new BrowserManager();
await profileBrowser.launch({ headless: true, profile: tmpProfileDir });
});
afterAll(async () => {
await profileBrowser.close();
rmSync(tmpProfileDir, { recursive: true, force: true });
});
it('should report as launched in persistent context mode', () => {
expect(profileBrowser.isLaunched()).toBe(true);
});
it('should create new tab in persistent context mode without throwing', async () => {
const result = await profileBrowser.newTab();
expect(result.index).toBe(1);
expect(result.total).toBe(2);
});
});
describe('getDefaultTimeout', () => {
const originalEnv = { ...process.env };
+6 -2
View File
@@ -1829,7 +1829,7 @@ export class BrowserManager {
* Create a new tab in the current context
*/
async newTab(): Promise<{ index: number; total: number }> {
if (!this.browser || this.contexts.length === 0) {
if (!this.isLaunched() || this.contexts.length === 0) {
throw new Error('Browser not launched');
}
@@ -1856,7 +1856,11 @@ export class BrowserManager {
total: number;
}> {
if (!this.browser) {
throw new Error('Browser not launched');
throw new Error(
this.isPersistentContext
? 'newWindow is not supported in extension (persistent context) mode'
: 'Browser not launched'
);
}
const context = await this.browser.newContext({