Skip to content

Commit

Permalink
Store messages on the server
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderMoskovkin committed Oct 3, 2017
1 parent 9889217 commit e970116
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 12 deletions.
33 changes: 22 additions & 11 deletions src/client/driver/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -172,6 +167,15 @@ export default class Driver {
}

// Console messages
static _getDefaultConsoleMessages () {
return {
log: [],
info: [],
error: [],
warn: []
};
}

_onConsoleMessage (e) {
const meth = e.meth;

Expand All @@ -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(' '));

Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion src/client/driver/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -21,7 +22,8 @@ export default class DriverStatus extends Assignable {
{ name: 'isCommandResult' },
{ name: 'executionError' },
{ name: 'pageError' },
{ name: 'result' }
{ name: 'result' },
{ name: 'consoleMessages' }
];
}
}
23 changes: 23 additions & 0 deletions src/test-run/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e970116

Please sign in to comment.