diff --git a/src/actions.ts b/src/actions.ts index ffc84b3..34a4915 100644 --- a/src/actions.ts +++ b/src/actions.ts @@ -1160,7 +1160,6 @@ async function handleConsole(command: ConsoleCommand, browser: BrowserManager): return successResponse(command.id, { cleared: true }); } - browser.startConsoleTracking(); const messages = browser.getConsoleMessages(); return successResponse(command.id, { messages }); } @@ -1171,7 +1170,6 @@ async function handleErrors(command: ErrorsCommand, browser: BrowserManager): Pr return successResponse(command.id, { cleared: true }); } - browser.startErrorTracking(); const errors = browser.getPageErrors(); return successResponse(command.id, { errors }); } diff --git a/src/browser.ts b/src/browser.ts index d3cf16c..fc803af 100644 --- a/src/browser.ts +++ b/src/browser.ts @@ -459,6 +459,29 @@ export class BrowserManager { const page = await context.newPage(); this.pages.push(page); this.activePageIndex = 0; + + // Automatically start console and error tracking + this.setupPageTracking(page); + } + + /** + * Set up console and error tracking for a page + */ + private setupPageTracking(page: Page): void { + page.on('console', (msg) => { + this.consoleMessages.push({ + type: msg.type(), + text: msg.text(), + timestamp: Date.now(), + }); + }); + + page.on('pageerror', (error) => { + this.pageErrors.push({ + message: error.message, + timestamp: Date.now(), + }); + }); } /** @@ -474,6 +497,9 @@ export class BrowserManager { this.pages.push(page); this.activePageIndex = this.pages.length - 1; + // Set up tracking for the new page + this.setupPageTracking(page); + return { index: this.activePageIndex, total: this.pages.length }; } @@ -498,6 +524,9 @@ export class BrowserManager { this.pages.push(page); this.activePageIndex = this.pages.length - 1; + // Set up tracking for the new page + this.setupPageTracking(page); + return { index: this.activePageIndex, total: this.pages.length }; }