diff --git a/src/client/driver/driver.js b/src/client/driver/driver.js index 047a81a5597..8a8820e68c3 100644 --- a/src/client/driver/driver.js +++ b/src/client/driver/driver.js @@ -127,12 +127,7 @@ export default class Driver { } get consoleMessages () { - return this.contextStorage.getItem(CONSOLE_MESSAGES) || { - log: [], - info: [], - error: [], - warn: [] - }; + return this.contextStorage.getItem(CONSOLE_MESSAGES); } set consoleMessages (messages) { @@ -172,6 +167,15 @@ export default class Driver { } // Console messages + static _getDefaultConsoleMessages () { + return { + log: [], + info: [], + error: [], + warn: [] + }; + } + _onConsoleMessage (e) { const meth = e.meth; @@ -185,7 +189,7 @@ export default class Driver { return arg.toString(); }); - const messages = this.consoleMessages; + const messages = this.consoleMessages || Driver._getDefaultConsoleMessages(); messages[meth].push(Array.prototype.slice.call(args).join(' ')); @@ -208,12 +212,22 @@ export default class Driver { status.pageError = status.pageError || dialogError; } + _addConsoleMessagesToStatus (status) { + var messages = this.consoleMessages; + + if (messages) { + status.consoleMessages = messages; + this.consoleMessages = null; + } + } + _sendStatus (status) { // NOTE: We should not modify the status if it is resent after // the page load because the server has cached the response if (!status.resent) { this._addPendingErrorToStatus(status); this._addUnexpectedDialogErrorToStatus(status); + this._addConsoleMessagesToStatus(status); } this.contextStorage.setItem(PENDING_STATUS, status); @@ -363,10 +377,7 @@ export default class Driver { } _onGetBrowserConsoleMessagesCommand () { - this._onReady(new DriverStatus({ - isCommandResult: true, - result: this.consoleMessages - })); + this._onReady(new DriverStatus({ isCommandResult: true })); } _onNavigateToCommand (command) { diff --git a/src/client/driver/status.js b/src/client/driver/status.js index 613e6de516b..b805a2180af 100644 --- a/src/client/driver/status.js +++ b/src/client/driver/status.js @@ -12,6 +12,7 @@ export default class DriverStatus extends Assignable { this.pageError = null; this.resent = false; this.result = null; + this.consoleMessages = null; this._assignFrom(obj, true); } @@ -21,7 +22,8 @@ export default class DriverStatus extends Assignable { { name: 'isCommandResult' }, { name: 'executionError' }, { name: 'pageError' }, - { name: 'result' } + { name: 'result' }, + { name: 'consoleMessages' } ]; } } diff --git a/src/test-run/index.js b/src/test-run/index.js index 02029124939..2c2df983f04 100644 --- a/src/test-run/index.js +++ b/src/test-run/index.js @@ -3,6 +3,7 @@ import { readSync as read } from 'read-file-relative'; import promisifyEvent from 'promisify-event'; import Promise from 'pinkie'; import Mustache from 'mustache'; +import { assignIn } from 'lodash'; import debugLogger from '../notifications/debug-logger'; import { Session } from 'testcafe-hammerhead'; import TestRunDebugLog from './debug-log'; @@ -73,6 +74,13 @@ export default class TestRun extends Session { this.speed = this.opts.speed; this.pageLoadTimeout = this.opts.pageLoadTimeout; + this.consoleMessages = { + log: [], + info: [], + warn: [], + error: [] + }; + this.pendingRequest = null; this.pendingPageError = null; @@ -269,6 +277,12 @@ export default class TestRun extends Session { return this.executeCommand(new PrepareBrowserManipulationCommand(command.type), callsite); } + async _enqueueBrowserConsoleMessagesCommand (command, callsite) { + await this._enqueueCommand(command, callsite); + + return assignIn({}, this.consoleMessages); + } + async _enqueueSetBreakpointCommand (callsite, error) { debugLogger.showBreakpoint(this.id, this.browserConnection.userAgent, callsite, error); @@ -344,6 +358,12 @@ export default class TestRun extends Session { var currentTaskRejectedByError = pageError && this._handlePageErrorStatus(pageError); + if (driverStatus.consoleMessages) { + Object.keys(this.consoleMessages).forEach(msgType => { + this.consoleMessages[msgType] = this.consoleMessages[msgType].concat(driverStatus.consoleMessages[msgType]); + }); + } + if (!currentTaskRejectedByError && driverStatus.isCommandResult) { if (this.currentDriverTask.command.type === COMMAND_TYPE.testDone) { this._resolveCurrentDriverTask(); @@ -426,6 +446,9 @@ export default class TestRun extends Session { if (command.type === COMMAND_TYPE.assertion) return this._executeAssertion(command, callsite); + if (command.type === COMMAND_TYPE.getBrowserConsoleMessages) + return await this._enqueueBrowserConsoleMessagesCommand(command, callsite); + return this._enqueueCommand(command, callsite); } diff --git a/test/functional/fixtures/api/es-next/console/testcafe-fixtures/console-test.js b/test/functional/fixtures/api/es-next/console/testcafe-fixtures/console-test.js index 253ebbfac4e..40b034b0a8e 100644 --- a/test/functional/fixtures/api/es-next/console/testcafe-fixtures/console-test.js +++ b/test/functional/fixtures/api/es-next/console/testcafe-fixtures/console-test.js @@ -17,6 +17,10 @@ test // Check the driver keeps the messages between page reloads .click('#reload'); + // Changes in the getBrowserConsoleMessages result object should + // not affect the console messages state in the test run. + messages.log.push('unexpected'); + messages = await t.getBrowserConsoleMessages(); await t