Skip to content

Commit 3c37260

Browse files
feat: collect logs for successful journey as well (#817)
* PR feedback * revert * update * add tests and fix algo * Update src/plugins/browser-console.ts * Update src/plugins/browser-console.ts --------- Co-authored-by: vigneshshanmugam <[email protected]>
1 parent d7c83d1 commit 3c37260

File tree

3 files changed

+100
-8
lines changed

3 files changed

+100
-8
lines changed

__tests__/plugins/browser-console.test.ts

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
*/
2525

2626
import { Gatherer } from '../../src/core/gatherer';
27-
import { BrowserConsole } from '../../src/plugins';
27+
import { BrowserConsole, filterBrowserMessages } from '../../src/plugins';
28+
import { BrowserMessage } from '../../src/common_types';
2829
import { Server } from '../utils/server';
2930
import { wsEndpoint } from '../utils/test-config';
3031

@@ -122,4 +123,59 @@ describe('BrowserConsole', () => {
122123
expect(unhandledError?.step).toEqual(currentStep);
123124
expect(unhandledError?.error?.stack).toContain('Error: Boom');
124125
});
126+
127+
describe('Filtering', () => {
128+
function getBrowserMessages({ errors = 100, warnings = 100, log = 100 }) {
129+
const messages: BrowserMessage[] = [];
130+
for (let i = 0; i < errors; i++) {
131+
messages.push({ type: 'error', text: `error ${i}`, timestamp: 0 });
132+
}
133+
for (let i = 0; i < warnings; i++) {
134+
messages.push({ type: 'warning', text: `warning ${i}`, timestamp: 0 });
135+
}
136+
for (let i = 0; i < log; i++) {
137+
messages.push({ type: 'log', text: `log ${i}`, timestamp: 0 });
138+
}
139+
return messages;
140+
}
141+
142+
it('skipped journey', () => {
143+
expect(
144+
filterBrowserMessages(getBrowserMessages({}), 'skipped').length
145+
).toEqual(0);
146+
});
147+
148+
it('failed journey', () => {
149+
expect(
150+
filterBrowserMessages(getBrowserMessages({}), 'failed').length
151+
).toEqual(300);
152+
});
153+
154+
it('successful journey', () => {
155+
expect(
156+
filterBrowserMessages(
157+
getBrowserMessages({ errors: 10, warnings: 10, log: 10 }),
158+
'succeeded'
159+
).length
160+
).toEqual(30);
161+
162+
expect(
163+
filterBrowserMessages(getBrowserMessages({}), 'succeeded').length
164+
).toEqual(100);
165+
166+
const withoutLog = filterBrowserMessages(
167+
getBrowserMessages({ errors: 10, log: 200 }),
168+
'succeeded'
169+
);
170+
expect(withoutLog.every(msg => msg.type !== 'log')).toBe(true);
171+
expect(withoutLog.length).toEqual(100);
172+
173+
const withLog = filterBrowserMessages(
174+
getBrowserMessages({ errors: 5, warnings: 5 }),
175+
'succeeded'
176+
);
177+
expect(withLog.some(msg => msg.type === 'log')).toBe(true);
178+
expect(withLog.length).toEqual(100);
179+
});
180+
});
125181
});

src/core/runner.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,11 @@ import {
4646
StepResult,
4747
PushOptions,
4848
} from '../common_types';
49-
import { PluginManager } from '../plugins';
50-
import { PerformanceManager } from '../plugins';
49+
import {
50+
PluginManager,
51+
PerformanceManager,
52+
filterBrowserMessages,
53+
} from '../plugins';
5154
import { Gatherer } from './gatherer';
5255
import { log } from './logger';
5356
import { Monitor, MonitorConfig } from '../dsl/monitor';
@@ -310,7 +313,10 @@ export default class Runner {
310313
timestamp: getTimestamp(),
311314
options,
312315
...pluginOutput,
313-
browserconsole: status == 'failed' ? pluginOutput.browserconsole : [],
316+
browserconsole: filterBrowserMessages(
317+
pluginOutput.browserconsole,
318+
status
319+
),
314320
});
315321
// clear screenshots cache after each journey
316322
await rm(Runner.screenshotPath, { recursive: true, force: true });

src/plugins/browser-console.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@
2424
*/
2525

2626
import type { ConsoleMessage } from 'playwright-core';
27-
import { BrowserMessage, Driver } from '../common_types';
27+
import { BrowserMessage, Driver, StatusValue } from '../common_types';
2828
import { log } from '../core/logger';
2929
import { Step } from '../dsl';
3030
import { getTimestamp } from '../helpers';
3131

32-
const defaultMessageLimit = 1000;
32+
const DEFAULT_MSG_LIMIt = 1000;
33+
const SUCCESSFUL_MSG_LIMIT = 100;
34+
35+
const allowedTypes = ['error', 'warning', 'log'];
3336

3437
export class BrowserConsole {
3538
private messages: BrowserMessage[] = [];
@@ -42,7 +45,7 @@ export class BrowserConsole {
4245
return;
4346
}
4447
const type = msg.type();
45-
if (type === 'error' || type === 'warning') {
48+
if (allowedTypes.includes(type)) {
4649
const { name, index } = this._currentStep;
4750
this.messages.push({
4851
timestamp: getTimestamp(),
@@ -72,7 +75,7 @@ export class BrowserConsole {
7275
};
7376

7477
private enforceMessagesLimit() {
75-
if (this.messages.length > defaultMessageLimit) {
78+
if (this.messages.length > DEFAULT_MSG_LIMIt) {
7679
this.messages.splice(0, 1);
7780
}
7881
}
@@ -90,3 +93,30 @@ export class BrowserConsole {
9093
return this.messages;
9194
}
9295
}
96+
97+
export function filterBrowserMessages(
98+
messages: BrowserMessage[],
99+
status: StatusValue
100+
) {
101+
if (status == 'skipped') {
102+
return [];
103+
} else if (status === 'failed' || messages.length <= SUCCESSFUL_MSG_LIMIT) {
104+
return messages;
105+
}
106+
// collect 100 messages from the browser console when the test is successful,
107+
// giving priority to errors and warnings
108+
const result = messages.filter(msg => msg.type === 'error');
109+
if (result.length >= SUCCESSFUL_MSG_LIMIT) {
110+
return result.slice(-SUCCESSFUL_MSG_LIMIT);
111+
}
112+
113+
// collect warnings
114+
result.push(...messages.filter(msg => msg.type === 'warning'));
115+
if (result.length >= SUCCESSFUL_MSG_LIMIT) {
116+
return result.slice(-SUCCESSFUL_MSG_LIMIT);
117+
}
118+
119+
// collect logs
120+
result.push(...messages.filter(msg => msg.type === 'log'));
121+
return result.slice(-SUCCESSFUL_MSG_LIMIT);
122+
}

0 commit comments

Comments
 (0)