Skip to content

Commit

Permalink
Enable screenshot functionality (closes DevExpress#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderMoskovkin committed Oct 30, 2015
1 parent a0c5dbd commit c804a48
Show file tree
Hide file tree
Showing 25 changed files with 293 additions and 98 deletions.
4 changes: 2 additions & 2 deletions src/client/runner/api/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -698,11 +698,11 @@ export function upload (what, path) {
);
}

export function screenshot () {
export function screenshot (filePath) {
stepIterator.asyncAction(function (iteratorCallback) {
stepIterator.takeScreenshot(function () {
iteratorCallback();
}, false);
}, false, filePath);
});
}

Expand Down
4 changes: 3 additions & 1 deletion src/client/runner/iframe-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ IFrameRunner.prototype._onGetStepsSharedData = function (e) {
IFrameRunner.prototype._onTakeScreenshot = function (e) {
var msg = {
cmd: RunnerBase.IFRAME_TAKE_SCREENSHOT_REQUEST_CMD,
isFailedStep: e.isFailedStep
isFailedStep: e.isFailedStep,
stepName: e.stepName,
filePath: e.filePath
};

messageSandbox.sendServiceMsg(msg, window.top);
Expand Down
2 changes: 2 additions & 0 deletions src/client/runner/runner-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ RunnerBase.prototype._initIFrameBehavior = function () {
case RunnerBase.IFRAME_TAKE_SCREENSHOT_REQUEST_CMD:
runner._onTakeScreenshot({
isFailedStep: message.isFailedStep,
stepName: message.stepName,
filePath: message.filePath,
callback: function () {
msg = {
cmd: RunnerBase.IFRAME_TAKE_SCREENSHOT_RESPONSE_CMD
Expand Down
49 changes: 18 additions & 31 deletions src/client/runner/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ Runner.prototype._onError = function (err) {

this._onTakeScreenshot({
isFailedStep: true,
//TODO:
//withoutStepName: !(ERRORS.hasErrorStepName(err) && ERRORS.hasErrorStepName(err)),
callback: function () {
stepName: this.stepIterator.getCurrentStep(),
callback: function (screenshotPath) {
err.screenshot = screenshotPath;
runner.stopped = true;
transport.fail(err, Runner.checkStatus);
}
Expand Down Expand Up @@ -138,39 +138,26 @@ Runner.prototype._onGetStepsSharedData = function (e) {
};

Runner.prototype._onTakeScreenshot = function (e) {
var savedTitle = document.title,
windowMark = '[tc-' + Date.now() + ']',
browserName = null,
callback = e && e.callback ? e.callback : function () {
var savedTitle = document.title,
pageUrl = window.location.toString(),
callback = e && e.callback ? e.callback : function () {
},
runner = this;

runner.eventEmitter.emit(RunnerBase.SCREENSHOT_CREATING_STARTED_EVENT, {});
runner = this;

if (!SETTINGS.get().TAKE_SCREENSHOTS)
return callback();

if (browserUtils.isMSEdge)
browserName = 'MSEDGE';
else if (browserUtils.isSafari)
browserName = 'SAFARI';
else if (browserUtils.isOpera || browserUtils.isOperaWithWebKit)
browserName = 'OPERA';
else if (browserUtils.isWebKit)
browserName = 'CHROME';
else if (browserUtils.isMozilla)
browserName = 'FIREFOX';
else if (browserUtils.isIE)
browserName = 'IE';
runner.eventEmitter.emit(RunnerBase.SCREENSHOT_CREATING_STARTED_EVENT, {});

var msg = {
cmd: 'CMD_TAKE_SCREENSHOT', //TODO: fix
windowMark: windowMark,
browserName: browserName,
isFailedStep: e.isFailedStep,
withoutStepName: e.withoutStepName,
url: window.location.toString()
cmd: COMMAND.takeScreenshot,
pageUrl: pageUrl,
isFailedStep: e.isFailedStep,
stepName: e.stepName,
filePath: e.filePath
};

var assignedTitle = savedTitle + windowMark,
var assignedTitle = `[ ${pageUrl} ]`,
checkTitleIntervalId = window.setInterval(function () {
if (document.title !== assignedTitle) {
savedTitle = document.title;
Expand All @@ -183,14 +170,14 @@ Runner.prototype._onTakeScreenshot = function (e) {
//NOTE: we should set timeouts to changing of document title
//in any case we are waiting response from server
window.setTimeout(function () {
transport.asyncServiceMsg(msg, function () {
transport.asyncServiceMsg(msg, function (filePath) {
window.clearInterval(checkTitleIntervalId);
checkTitleIntervalId = null;
document.title = savedTitle;
runner.eventEmitter.emit(RunnerBase.SCREENSHOT_CREATING_FINISHED_EVENT, {});

window.setTimeout(function () {
callback();
callback(filePath);
}, 100);
});
}, 500);
Expand Down
8 changes: 7 additions & 1 deletion src/client/runner/step-iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,9 @@ StepIterator.prototype.runLast = function () {
};

StepIterator.prototype.getCurrentStep = function () {
if (!this.initialized)
return '';

return this.state.stepNames ? this.state.stepNames[this.state.step - 1] : SETTINGS.get().CURRENT_TEST_STEP_NAME;
};

Expand All @@ -513,6 +516,7 @@ StepIterator.prototype.onActionRun = function () {
this.eventEmitter.emit(StepIterator.ACTION_RUN_EVENT, {});
};


//Global __waitFor()
StepIterator.prototype.setGlobalWaitFor = function (event, timeout) {
this.globalWaitForEvent = event;
Expand Down Expand Up @@ -545,9 +549,11 @@ StepIterator.prototype.__waitFor = function (callback) {
});
};

StepIterator.prototype.takeScreenshot = function (callback, isFailedStep) {
StepIterator.prototype.takeScreenshot = function (callback, isFailedStep, filePath) {
this.eventEmitter.emit(StepIterator.TAKE_SCREENSHOT_EVENT, {
isFailedStep: isFailedStep,
stepName: this.getCurrentStep(),
filePath: filePath || '',
callback: callback
});
};
Expand Down
1 change: 1 addition & 0 deletions src/client/test-run/index.js.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
testCafeCore.SETTINGS.set({
CURRENT_TEST_STEP_NAME: nextStep ? stepNames[nextStep - 1] : 'Test initialization',
BROWSER_STATUS_URL: '{{{browserStatusUrl}}}',
TAKE_SCREENSHOTS: {{{takeScreenshots}}},
TAKE_SCREENSHOT_ON_FAILS: {{{takeScreenshotOnFails}}},
SKIP_JS_ERRORS: {{{skipJsErrors}}},
ENABLE_SOURCE_INDEX: true,
Expand Down
15 changes: 9 additions & 6 deletions src/reporters/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,16 @@ export default class BaseReporter {
// This happens because the next test can't be completed until the
// previous one frees all browser connections.
// Therefore, tests always get completed sequentially.
var reportItem = this.reportQueue.shift();
var durationMs = new Date() - reportItem.startTime;
var reportItem = this.reportQueue.shift();
var durationMs = new Date() - reportItem.startTime;
var screenshotPath = reportItem.hasScreenshots ? reportItem.screenshotPath : null;

if (!reportItem.errs.length)
this.passed++;

BaseReporter._errorSorter(reportItem.errs);

this._reportTestDone(reportItem.testName, reportItem.errs, durationMs, reportItem.unstable);
this._reportTestDone(reportItem.testName, reportItem.errs, durationMs, reportItem.unstable, screenshotPath);

// NOTE: here we assume that tests are sorted by fixture.
// Therefore, if the next report item has a different
Expand Down Expand Up @@ -120,8 +121,10 @@ export default class BaseReporter {
testRun.errs.forEach(err => err.userAgent = userAgent);

reportItem.pendingRuns--;
reportItem.errs = reportItem.errs.concat(testRun.errs);
reportItem.unstable = reportItem.unstable || testRun.unstable;
reportItem.errs = reportItem.errs.concat(testRun.errs);
reportItem.unstable = reportItem.unstable || testRun.unstable;
reportItem.hasScreenshots = reportItem.hasScreenshots || testRun.hasScreenshots;
reportItem.screenshotPath = reportItem.screenshotPath || testRun.test.screenshotPath;

if (!reportItem.pendingRuns)
this._shiftReportQueue();
Expand Down Expand Up @@ -182,7 +185,7 @@ export default class BaseReporter {
throw new Error('Not implemented');
}

_reportTestDone (name, errs, durationMs, unstable) {
_reportTestDone (name, errs, durationMs, unstable, screenshotPath) {
throw new Error('Not implemented');
}

Expand Down
4 changes: 4 additions & 0 deletions src/reporters/errors/decorators/plain-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export default {

'span user-agent': str => str,

'span screenshot': str => str,

'span screenshot-path': str => str,

'code': str => str,

'code step-source': str => str,
Expand Down
4 changes: 4 additions & 0 deletions src/reporters/errors/decorators/tty.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export default {

'span user-agent': str => chalk.gray(str),

'span screenshot': str => str,

'span screenshot-path': str => chalk.underline(str),

'code': str => chalk.yellow(str),

'code step-source': str => chalk.magenta(str),
Expand Down
Loading

0 comments on commit c804a48

Please sign in to comment.