1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 09:57:02 +01:00

Capture all Error console.log events (#17710)

* Capture all Error console.log events

* throw early

* Update metamask-ui.spec.js

* Update init-load-stats.js
This commit is contained in:
Peter 2023-02-23 14:27:36 +00:00 committed by GitHub
parent 09c60e2038
commit 32b9237df3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 50 deletions

View File

@ -52,7 +52,6 @@ async function withFixtures(options, testSuite) {
let webDriver; let webDriver;
let driver; let driver;
const errors = [];
let failed = false; let failed = false;
try { try {
await ganacheServer.start(ganacheOptions); await ganacheServer.start(ganacheOptions);
@ -117,7 +116,8 @@ async function withFixtures(options, testSuite) {
webDriver = driver.driver; webDriver = driver.driver;
if (process.env.SELENIUM_BROWSER === 'chrome') { if (process.env.SELENIUM_BROWSER === 'chrome') {
await driver.checkBrowserForExceptions(); await driver.checkBrowserForExceptions(failOnConsoleError);
await driver.checkBrowserForConsoleErrors(failOnConsoleError);
} }
let driverProxy; let driverProxy;
@ -145,21 +145,6 @@ async function withFixtures(options, testSuite) {
mockServer, mockServer,
contractRegistry, contractRegistry,
}); });
if (process.env.SELENIUM_BROWSER === 'chrome') {
errors.concat(await driver.checkBrowserForConsoleErrors(driver));
if (errors.length) {
const errorReports = errors.map((err) => err.message);
const errorMessage = `Errors found in browser console:\n${errorReports.join(
'\n',
)}`;
if (failOnConsoleError) {
throw new Error(errorMessage);
} else {
console.error(new Error(errorMessage));
}
}
}
} catch (error) { } catch (error) {
failed = true; failed = true;
if (webDriver) { if (webDriver) {
@ -168,21 +153,13 @@ async function withFixtures(options, testSuite) {
} catch (verboseReportError) { } catch (verboseReportError) {
console.error(verboseReportError); console.error(verboseReportError);
} }
if ( if (driver.errors.length > 0 || driver.exceptions.length > 0) {
errors.length === 0 &&
driver.exceptions.length > 0 &&
failOnConsoleError
) {
/** /**
* Navigate to the background * Navigate to the background
* forcing background exceptions to be captured * forcing background exceptions to be captured
* proving more helpful context * proving more helpful context
*/ */
await driver.navigate(PAGES.BACKGROUND); await driver.navigate(PAGES.BACKGROUND);
const errorMessage = `Errors found in browser console including the background:\n${driver.exceptions.join(
'\n',
)}`;
throw Error(errorMessage);
} }
} }
throw error; throw error;

View File

@ -58,14 +58,7 @@ describe('MetaMask', function () {
afterEach(async function () { afterEach(async function () {
if (process.env.SELENIUM_BROWSER === 'chrome') { if (process.env.SELENIUM_BROWSER === 'chrome') {
const errors = await driver.checkBrowserForConsoleErrors(driver); await driver.checkBrowserForConsoleErrors(false);
if (errors.length) {
const errorReports = errors.map((err) => err.message);
const errorMessage = `Errors found in browser console:\n${errorReports.join(
'\n',
)}`;
console.error(new Error(errorMessage));
}
} }
if (this.currentTest.state === 'failed') { if (this.currentTest.state === 'failed') {
failed = true; failed = true;

View File

@ -22,7 +22,7 @@ async function profilePageLoad() {
const parsedLogs = {}; const parsedLogs = {};
try { try {
await withFixtures( await withFixtures(
{ fixtures: new FixtureBuilder().build() }, { fixtures: new FixtureBuilder().build(), failOnConsoleError: false },
async ({ driver }) => { async ({ driver }) => {
await driver.delay(tinyDelayMs); await driver.delay(tinyDelayMs);
await driver.navigate(); await driver.navigate();

View File

@ -64,6 +64,7 @@ class Driver {
this.extensionUrl = extensionUrl; this.extensionUrl = extensionUrl;
this.timeout = timeout; this.timeout = timeout;
this.exceptions = []; this.exceptions = [];
this.errors = [];
// The following values are found in // The following values are found in
// https://github.com/SeleniumHQ/selenium/blob/trunk/javascript/node/selenium-webdriver/lib/input.js#L50-L110 // https://github.com/SeleniumHQ/selenium/blob/trunk/javascript/node/selenium-webdriver/lib/input.js#L50-L110
// These should be replaced with string constants 'Enter' etc for playwright. // These should be replaced with string constants 'Enter' etc for playwright.
@ -462,17 +463,17 @@ class Driver {
return browserLogs; return browserLogs;
} }
async checkBrowserForExceptions() { async checkBrowserForExceptions(failOnConsoleError) {
const { exceptions } = this; const { exceptions } = this;
const cdpConnection = await this.driver.createCDPConnection('page'); const cdpConnection = await this.driver.createCDPConnection('page');
await this.driver.onLogException(cdpConnection, function (exception) { await this.driver.onLogException(cdpConnection, (exception) => {
const { description } = exception.exceptionDetails.exception; const { description } = exception.exceptionDetails.exception;
exceptions.push(description); exceptions.push(description);
logBrowserError(failOnConsoleError, description);
}); });
} }
async checkBrowserForConsoleErrors() { async checkBrowserForConsoleErrors(failOnConsoleError) {
const ignoredLogTypes = ['WARNING'];
const ignoredErrorMessages = [ const ignoredErrorMessages = [
// Third-party Favicon 404s show up as errors // Third-party Favicon 404s show up as errors
'favicon.ico - Failed to load resource: the server responded with a status of 404', 'favicon.ico - Failed to load resource: the server responded with a status of 404',
@ -481,17 +482,31 @@ class Driver {
// 4Byte // 4Byte
'Failed to load resource: the server responded with a status of 502 (Bad Gateway)', 'Failed to load resource: the server responded with a status of 502 (Bad Gateway)',
]; ];
const browserLogs = await this.driver.manage().logs().get('browser'); const { errors } = this;
const errorEntries = browserLogs.filter( const cdpConnection = await this.driver.createCDPConnection('page');
(entry) => !ignoredLogTypes.includes(entry.level.toString()), await this.driver.onLogEvent(cdpConnection, (event) => {
); if (event.type === 'error') {
const errorObjects = errorEntries.map((entry) => entry.toJSON()); const eventDescription = event.args.filter(
return errorObjects.filter( (err) => err.description !== undefined,
(entry) => );
!ignoredErrorMessages.some((message) => const [{ description }] = eventDescription;
entry.message.includes(message), const ignore = ignoredErrorMessages.some((message) =>
), description.includes(message),
); );
if (!ignore) {
errors.push(description);
logBrowserError(failOnConsoleError, description);
}
}
});
}
}
function logBrowserError(failOnConsoleError, errorMessage) {
if (failOnConsoleError) {
throw new Error(errorMessage);
} else {
console.error(new Error(errorMessage));
} }
} }