Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capture and log page errors #374

Merged
merged 8 commits into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions __tests__/plugins/browser-console.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,49 @@ describe('BrowserConsole', () => {
expect(testMessage.timestamp).toBeDefined();
expect(testMessage.step).toEqual({ name: 'step-name', index: 0 });
});

it('should capture browser page errors', async () => {
const driver = await Gatherer.setupDriver({ wsEndpoint });
const browserConsole = new BrowserConsole(driver);
const { page } = driver;
browserConsole.start();
await page.goto(server.TEST_PAGE);
browserConsole._currentStep = { name: 'step-name', index: 0 };
const bodyHandle = await page.$('body');
try {
await page.evaluate(
vigneshshanmugam marked this conversation as resolved.
Show resolved Hide resolved
([body]) => {
body.innerHTML = `<img
src="imagefound.gif"
onError="that.onerror=null;this.src='imagenotfound.gif';"
/>`;
},
[bodyHandle]
);
} catch (e) {}

await page.waitForTimeout(1000);

const messages = browserConsole.stop();
await Gatherer.stop();
const notFoundMessage = messages.find(
m => m.text.indexOf('Failed to load resource:') >= 0
);
expect(notFoundMessage.text).toEqual(
`Failed to load resource: the server responded with a status of 404 (Not Found)`
);
expect(notFoundMessage.type).toEqual('error');
expect(notFoundMessage.timestamp).toBeDefined();
vigneshshanmugam marked this conversation as resolved.
Show resolved Hide resolved
expect(notFoundMessage.step).toEqual({ name: 'step-name', index: 0 });

const referenceError = messages.find(
m => m.text.indexOf('that is not defined') >= 0
);
expect(referenceError.error.stack).toContain(
`ReferenceError: that is not defined\n at HTMLImageElement.onerror`
);
expect(referenceError.type).toEqual('error');
expect(referenceError.timestamp).toBeDefined();
expect(referenceError.step).toEqual({ name: 'step-name', index: 0 });
});
});
1 change: 1 addition & 0 deletions src/common_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export type NetworkInfo = {
export type BrowserMessage = {
text: string;
type: string;
error?: Error;
} & DefaultPluginOutput;

export type PluginOutput = {
Expand Down
30 changes: 27 additions & 3 deletions src/plugins/browser-console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import { BrowserMessage, Driver } from '../common_types';
import { Step } from '../dsl';
import { getTimestamp } from '../helpers';
import { log } from '../core/logger';

const defaultMessageLimit = 1000;

Expand All @@ -48,18 +49,41 @@ export class BrowserConsole {
type,
step: { name, index },
});
if (this.messages.length > defaultMessageLimit) {
this.messages.splice(0, 1);
}

this.enforceMessagesLimit();
}
};

private pageErrorEventListener = (error: Error) => {
if (!this._currentStep) {
return;
}
const { name, index } = this._currentStep;
this.messages.push({
timestamp: getTimestamp(),
text: error.message,
type: 'error',
step: { name, index },
error: error,
});

this.enforceMessagesLimit();
};

private enforceMessagesLimit() {
if (this.messages.length > defaultMessageLimit) {
shahzad31 marked this conversation as resolved.
Show resolved Hide resolved
this.messages.splice(0, 1);
}
}

start() {
this.driver.page.on('console', this.consoleEventListener);
this.driver.page.on('pageerror', this.pageErrorEventListener);
}

stop() {
this.driver.page.off('console', this.consoleEventListener);
this.driver.page.off('pageerror', this.pageErrorEventListener);
return this.messages;
}
}
10 changes: 7 additions & 3 deletions src/reporters/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,13 +483,17 @@ export default class JSONReporter extends BaseReporter {
});
}
if (browserconsole) {
browserconsole.forEach(({ timestamp, text, type, step }) => {
browserconsole.forEach(({ timestamp, text, type, step, error }) => {
this.writeJSON({
type: 'journey/browserconsole',
journey,
timestamp,
step,
payload: { text, type } as Payload,
payload: {
vigneshshanmugam marked this conversation as resolved.
Show resolved Hide resolved
text,
type,
stacktrace: error?.stack.toString(),
} as Payload,
});
});
}
Expand Down Expand Up @@ -554,7 +558,7 @@ export default class JSONReporter extends BaseReporter {
});
}

// Writes a structered synthetics event
// Writes a structured synthetics event
// Note that blob is ultimately stored in ES as a base64 encoded string. You must base 64 encode
// it before passing it into this function!
// The payload field is an un-indexed field with no ES mapping, so users can put arbitary structured
Expand Down