Skip to content

Commit

Permalink
add fixture to track errors in console
Browse files Browse the repository at this point in the history
  • Loading branch information
tryb3l committed Oct 27, 2024
1 parent 4bba2b0 commit eb91716
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions e2e/lib/fixtures/base-fixtures.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { test as base } from '@playwright/test';
import { test as base, ConsoleMessage } from '@playwright/test';
import { PageManager } from '@pages/page-manager';
import { StartPage } from '@fixtures/start-page.enum';
import { FormLayoutsPage } from '@pages/form-layouts.page';
Expand All @@ -9,7 +9,38 @@ export type TestOptions = {
formLayoutsPage: FormLayoutsPage;
};

export const test = base.extend<TestOptions>({
export const test = base.extend<TestOptions & { trackConsoleErrors: boolean }>({
trackConsoleErrors: [true, { option: true }],
page: async ({ page, trackConsoleErrors }, use, testInfo) => {
const errors: string[] = [];

if (trackConsoleErrors) {
const listener = (msg: ConsoleMessage) => {
if (msg.type() === 'error') {
errors.push(msg.text());
}
};
page.on('console', listener);
await use(page);

page.off('console', listener);

if (errors.length > 0) {
await testInfo.attach('Console Errors', {
body: errors.join('\n'),
contentType: 'text/plain',
});

testInfo.status = 'failed';
testInfo.error = new Error(
`Console errors detected: \n${errors.join('\n')}`
);
}
} else {
await use(page);
}
},

startPage: [StartPage.IoTDashboard, { option: true }],
pageManager: async ({ page, startPage }, use) => {
const pageManager = new PageManager(page);
Expand Down

0 comments on commit eb91716

Please sign in to comment.