Files
n8n/packages/testing/playwright/fixtures/console-error-monitor.ts
T
alighasami 3d5eaf9445
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
first commit
2026-03-17 16:22:57 +03:30

83 lines
1.7 KiB
TypeScript

import type { BrowserContext, ConsoleMessage, TestInfo } from '@playwright/test';
interface ConsoleError {
type: string;
text: string;
location: string;
timestamp: number;
}
/**
* Monitors browser context for console errors.
* Attaches diagnostic info to test results when errors occur.
* No-op when no errors are detected.
*/
class ConsoleErrorMonitor {
private errors: ConsoleError[] = [];
private readonly listener = (message: ConsoleMessage) => {
if (message.type() === 'error') {
this.errors.push({
type: message.type(),
text: message.text(),
location: message.location().url,
timestamp: Date.now(),
});
}
};
attach(context: BrowserContext): void {
context.on('console', this.listener);
}
detach(context: BrowserContext): void {
context.off('console', this.listener);
}
hasErrors(): boolean {
return this.errors.length > 0;
}
getErrors(): ConsoleError[] {
return this.errors;
}
}
/**
* Console error monitor fixtures for capturing browser errors.
* Spread into test.extend() to enable monitoring.
*/
export const consoleErrorFixtures = {
_consoleErrorMonitor: [
async (
{ context }: { context: BrowserContext },
use: (monitor: ConsoleErrorMonitor) => Promise<void>,
testInfo: TestInfo,
) => {
const monitor = new ConsoleErrorMonitor();
monitor.attach(context);
await use(monitor);
monitor.detach(context);
// Attach diagnostics if errors occurred
if (monitor.hasErrors()) {
await testInfo.attach('console-errors', {
body: JSON.stringify(
{
errors: monitor.getErrors(),
testTitle: testInfo.title,
project: testInfo.project.name,
},
null,
2,
),
contentType: 'application/json',
});
}
},
{ auto: true },
],
};