Skip to content

Commit

Permalink
Integrating with Roles. Refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderMoskovkin committed Oct 4, 2017
1 parent e970116 commit 2dc0753
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 34 deletions.
19 changes: 6 additions & 13 deletions src/client/driver/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import {
CurrentIframeNotFoundError,
CurrentIframeIsInvisibleError
} from '../../errors/test-run';

import BrowserConsoleMessages from '../../test-run/browser-console-messages';
import NativeDialogTracker from './native-dialog-tracker';

import { SetNativeDialogHandlerMessage, TYPE as MESSAGE_TYPE } from './driver-link/messages';
Expand Down Expand Up @@ -127,11 +129,11 @@ export default class Driver {
}

get consoleMessages () {
return this.contextStorage.getItem(CONSOLE_MESSAGES);
return new BrowserConsoleMessages(this.contextStorage.getItem(CONSOLE_MESSAGES));
}

set consoleMessages (messages) {
return this.contextStorage.setItem(CONSOLE_MESSAGES, messages);
return this.contextStorage.setItem(CONSOLE_MESSAGES, messages ? messages.getCopy() : null);
}

// Error handling
Expand Down Expand Up @@ -167,15 +169,6 @@ export default class Driver {
}

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

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

Expand All @@ -189,9 +182,9 @@ export default class Driver {
return arg.toString();
});

const messages = this.consoleMessages || Driver._getDefaultConsoleMessages();
const messages = this.consoleMessages;

messages[meth].push(Array.prototype.slice.call(args).join(' '));
messages.addMessage(meth, Array.prototype.slice.call(args).join(' '));

this.consoleMessages = messages;
}
Expand Down
6 changes: 4 additions & 2 deletions src/test-run/bookmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default class TestRunBookmark {
this.pageLoadTimeout = testRun.pageLoadTimeout;
this.ctx = testRun.ctx;
this.fixtureCtx = testRun.fixtureCtx;
this.consoleMessages = testRun.consoleMessages;
}

async init () {
Expand Down Expand Up @@ -94,8 +95,9 @@ export default class TestRunBookmark {

this.testRun.phase = TEST_RUN_PHASE.inBookmarkRestore;

this.testRun.ctx = this.ctx;
this.testRun.fixtureCtx = this.fixtureCtx;
this.testRun.ctx = this.ctx;
this.testRun.fixtureCtx = this.fixtureCtx;
this.testRun.consoleMessages = this.consoleMessages;

try {
await this._restoreSpeed();
Expand Down
46 changes: 46 additions & 0 deletions src/test-run/browser-console-messages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// -------------------------------------------------------------
// WARNING: this file is used by both the client and the server.
// Do not use any browser or node-specific API!
// -------------------------------------------------------------
import { assignIn } from 'lodash';
import Assignable from '../utils/assignable';


export default class BrowserConsoleMessages extends Assignable {
constructor (obj) {
super();

this.log = [];
this.info = [];
this.warn = [];
this.error = [];

this._assignFrom(obj);
}

_getAssignableProperties () {
return [
{ name: 'log' },
{ name: 'info' },
{ name: 'warn' },
{ name: 'error' }
];
}

concat (consoleMessages) {
this.log = this.log.concat(consoleMessages.log);
this.info = this.info.concat(consoleMessages.info);
this.warn = this.warn.concat(consoleMessages.warn);
this.error = this.error.concat(consoleMessages.error);
}

addMessage (type, msg) {
this[type].push(msg);
}

getCopy () {
const { log, info, warn, error } = this;

return assignIn({}, { log, info, warn, error });
}
}
24 changes: 8 additions & 16 deletions src/test-run/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ 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 All @@ -23,7 +22,7 @@ import ROLE_PHASE from '../role/phase';
import TestRunBookmark from './bookmark';
import ClientFunctionBuilder from '../client-functions/client-function-builder';
import ReporterPluginHost from '../reporter/plugin-host';

import BrowserConsoleMessages from './browser-console-messages';

import { TakeScreenshotOnFailCommand } from './commands/browser-manipulation';
import { SetNativeDialogHandlerCommand, SetTestSpeedCommand, SetPageLoadTimeoutCommand } from './commands/actions';
Expand Down Expand Up @@ -74,12 +73,7 @@ export default class TestRun extends Session {
this.speed = this.opts.speed;
this.pageLoadTimeout = this.opts.pageLoadTimeout;

this.consoleMessages = {
log: [],
info: [],
warn: [],
error: []
};
this.consoleMessages = new BrowserConsoleMessages();

this.pendingRequest = null;
this.pendingPageError = null;
Expand Down Expand Up @@ -280,7 +274,7 @@ export default class TestRun extends Session {
async _enqueueBrowserConsoleMessagesCommand (command, callsite) {
await this._enqueueCommand(command, callsite);

return assignIn({}, this.consoleMessages);
return this.consoleMessages.getCopy();
}

async _enqueueSetBreakpointCommand (callsite, error) {
Expand Down Expand Up @@ -358,11 +352,8 @@ 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 (driverStatus.consoleMessages)
this.consoleMessages.concat(driverStatus.consoleMessages);

if (!currentTaskRejectedByError && driverStatus.isCommandResult) {
if (this.currentDriverTask.command.type === COMMAND_TYPE.testDone) {
Expand Down Expand Up @@ -471,8 +462,9 @@ export default class TestRun extends Session {
}

async switchToCleanRun () {
this.ctx = Object.create(null);
this.fixtureCtx = Object.create(null);
this.ctx = Object.create(null);
this.fixtureCtx = Object.create(null);
this.consoleMessages = new BrowserConsoleMessages();

this.useStateSnapshot(null);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Selector, Role, t } from 'testcafe';
import { noop } from 'lodash';

const iframeElement = Selector('#element-in-iframe');
const pageElement = Selector('#element-on-page');
Expand All @@ -12,12 +11,15 @@ async function initConfiguration () {

const history = await t.getNativeDialogHistory();

/* eslint-disable no-console */
await t
.expect(history[0].text).eql('Hey!')
.switchToIframe('#iframe')
.expect(iframeElement.exists).ok()
.setTestSpeed(0.95)
.setPageLoadTimeout(95);
.setPageLoadTimeout(95)
.eval(() => console.log('init-configuration'));
/* eslint-enable no-console */

t.ctx.someVal = 'ctxVal';
t.fixtureCtx.someVal = 'fixtureCtxVal';
Expand All @@ -32,7 +34,15 @@ const role1 = Role('http://localhost:3000/fixtures/api/es-next/roles/pages/index
await t.click(showAlertBtn);
});

const role2 = Role('http://localhost:3000/fixtures/api/es-next/roles/pages/index.html', noop);
const role2 = Role('http://localhost:3000/fixtures/api/es-next/roles/pages/index.html', async () => {
/* eslint-disable no-console */
await t.eval(() => console.log('init-role'));
/* eslint-enable no-console */

const { log } = await t.getBrowserConsoleMessages();

await t.expect(log).eql(['init-role']);
});

fixture `Configuration management`
.page `http://localhost:3000/fixtures/api/es-next/roles/pages/index.html`;
Expand All @@ -46,7 +56,10 @@ test('Clear configuration', async () => {
test('Restore configuration', async () => {
await initConfiguration();

let { log } = await t.getBrowserConsoleMessages();

await t
.expect(log).eql(['init-configuration'])
.useRole(role2)
.expect(iframeElement.exists).ok()
.expect(t.ctx.someVal).eql('ctxVal')
Expand All @@ -59,4 +72,8 @@ test('Restore configuration', async () => {
const history = await t.getNativeDialogHistory();

await t.expect(history[0].text).eql('Hey!');

log = (await t.getBrowserConsoleMessages()).log;

await t.expect(log).eql(['init-configuration']);
});

0 comments on commit 2dc0753

Please sign in to comment.