diff --git a/test/e2e/helpers.js b/test/e2e/helpers.js index b3f74418a017..d950f0c0582d 100644 --- a/test/e2e/helpers.js +++ b/test/e2e/helpers.js @@ -51,6 +51,8 @@ async function withFixtures(options, testSuite) { const phishingPageServer = new PhishingWarningPageServer(); let webDriver; + let driver; + const errors = []; let failed = false; try { await ganacheServer.start(ganacheOptions); @@ -110,8 +112,12 @@ async function withFixtures(options, testSuite) { ) { await ensureXServerIsRunning(); } - const { driver } = await buildWebDriver(driverOptions); - webDriver = driver; + driver = (await buildWebDriver(driverOptions)).driver; + webDriver = driver.driver; + + if (process.env.SELENIUM_BROWSER === 'chrome') { + await driver.checkBrowserForExceptions(); + } await testSuite({ driver, @@ -120,7 +126,7 @@ async function withFixtures(options, testSuite) { }); if (process.env.SELENIUM_BROWSER === 'chrome') { - const errors = await driver.checkBrowserForConsoleErrors(driver); + 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( @@ -137,10 +143,20 @@ async function withFixtures(options, testSuite) { failed = true; if (webDriver) { try { - await webDriver.verboseReportOnFailure(title); + await driver.verboseReportOnFailure(title); } catch (verboseReportError) { console.error(verboseReportError); } + if ( + errors.length === 0 && + driver.exceptions.length > 0 && + failOnConsoleError + ) { + const errorMessage = `Errors found in browser console:\n${driver.exceptions.join( + '\n', + )}`; + throw Error(errorMessage); + } } throw error; } finally { @@ -151,7 +167,7 @@ async function withFixtures(options, testSuite) { await secondaryGanacheServer.quit(); } if (webDriver) { - await webDriver.quit(); + await driver.quit(); } if (dapp) { for (let i = 0; i < numberOfDapps; i++) { diff --git a/test/e2e/webdriver/driver.js b/test/e2e/webdriver/driver.js index 1969ddfcf5bb..4507d490513d 100644 --- a/test/e2e/webdriver/driver.js +++ b/test/e2e/webdriver/driver.js @@ -49,6 +49,7 @@ class Driver { this.browser = browser; this.extensionUrl = extensionUrl; this.timeout = timeout; + this.exceptions = []; // The following values are found in // 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. @@ -429,6 +430,15 @@ class Driver { return browserLogs; } + async checkBrowserForExceptions() { + const { exceptions } = this; + const cdpConnection = await this.driver.createCDPConnection('page'); + await this.driver.onLogException(cdpConnection, function (exception) { + const { description } = exception.exceptionDetails.exception; + exceptions.push(description); + }); + } + async checkBrowserForConsoleErrors() { const ignoredLogTypes = ['WARNING']; const ignoredErrorMessages = [