import { Page } from '@playwright/test'; export interface CapturedError { type: 'console' | 'page'; message: string; stack?: string; timestamp: number; } export class ConsoleErrorCapture { private errors: CapturedError[] = []; constructor(private page: Page) { this.setupCapture(); } private setupCapture(): void { this.page.on('console', (msg) => { if (msg.type() === 'error') { this.errors.push({ type: 'console', message: msg.text(), timestamp: Date.now(), }); } }); this.page.on('pageerror', (error) => { this.errors.push({ type: 'page', message: error.message, stack: error.stack ?? '', timestamp: Date.now(), }); }); } public getErrors(): CapturedError[] { return this.errors; } public hasErrors(): boolean { return this.errors.length > 0; } public clear(): void { this.errors = []; } public async waitForErrors(timeout: number = 1000): Promise { await this.page.waitForTimeout(timeout); return this.hasErrors(); } }