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:
@@ -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', () => {
|
describe('getDefaultTimeout', () => {
|
||||||
const originalEnv = { ...process.env };
|
const originalEnv = { ...process.env };
|
||||||
|
|
||||||
|
|||||||
+6
-2
@@ -1829,7 +1829,7 @@ export class BrowserManager {
|
|||||||
* Create a new tab in the current context
|
* Create a new tab in the current context
|
||||||
*/
|
*/
|
||||||
async newTab(): Promise<{ index: number; total: number }> {
|
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');
|
throw new Error('Browser not launched');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1856,7 +1856,11 @@ export class BrowserManager {
|
|||||||
total: number;
|
total: number;
|
||||||
}> {
|
}> {
|
||||||
if (!this.browser) {
|
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({
|
const context = await this.browser.newContext({
|
||||||
|
|||||||
Reference in New Issue
Block a user