Skip to content

Commit 1e46933

Browse files
authored
Merge pull request #432 from gemini-testing/HERMIONE-337.fix-screenshots-saving
fix: wait for test screenshots and page screenshot on error to be saved
2 parents c28c27e + 10f5e01 commit 1e46933

File tree

4 files changed

+72
-73
lines changed

4 files changed

+72
-73
lines changed

hermione.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@ async function prepare(hermione, reportBuilder, pluginConfig) {
3838
actions.push(formattedResult.saveErrorDetails(reportPath));
3939
}
4040

41-
if (formattedResult.screenshot) {
42-
actions.push(formattedResult.saveErrorScreenshot(reportPath));
43-
}
44-
4541
await Promise.all(actions);
4642

4743
return formattedResult;

lib/gui/tool-runner/report-subscriber.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ module.exports = (hermione, reportBuilder, client, reportPath) => {
1616
function failHandler(formattedResult) {
1717
const actions = [formattedResult.saveTestImages(reportPath, workers)];
1818

19-
if (formattedResult.screenshot) {
20-
actions.push(formattedResult.saveErrorScreenshot(reportPath));
21-
}
22-
2319
if (formattedResult.errorDetails) {
2420
actions.push(formattedResult.saveErrorDetails(reportPath));
2521
}

lib/test-adapter.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22

3-
const Promise = require('bluebird');
43
const _ = require('lodash');
54
const fs = require('fs-extra');
65
const path = require('path');
@@ -124,7 +123,7 @@ module.exports = class TestAdapter {
124123
return {};
125124
}
126125

127-
async saveErrorScreenshot(reportPath) {
126+
async _saveErrorScreenshot(reportPath) {
128127
if (!this.screenshot.base64) {
129128
logger.warn('Cannot save screenshot on reject');
130129

@@ -331,7 +330,7 @@ module.exports = class TestAdapter {
331330
}
332331

333332
async saveTestImages(reportPath, workers) {
334-
const result = await Promise.map(this.assertViewResults, async (assertResult) => {
333+
const result = await Promise.all(this.assertViewResults.map(async (assertResult) => {
335334
const {stateName} = assertResult;
336335
const destRefPath = utils.getReferencePath(this, stateName);
337336
const srcRefPath = this.getRefImg(stateName).path;
@@ -362,7 +361,11 @@ module.exports = class TestAdapter {
362361
}
363362

364363
return Promise.all(actions);
365-
});
364+
}));
365+
366+
if (this.screenshot) {
367+
await this._saveErrorScreenshot(reportPath);
368+
}
366369

367370
const htmlReporter = this._hermione.htmlReporter;
368371
htmlReporter.emit(htmlReporter.events.TEST_SCREENSHOTS_SAVED, {

test/unit/lib/test-adapter.js

Lines changed: 65 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,71 @@ describe('hermione test adapter', () => {
322322
imagesInfo: [{test: 123}]
323323
});
324324
});
325+
326+
describe('saving error screenshot', () => {
327+
beforeEach(() => {
328+
sandbox.stub(logger, 'warn');
329+
sandbox.stub(utils, 'makeDirFor').resolves();
330+
sandbox.stub(fs, 'writeFile').resolves();
331+
sandbox.stub(utils, 'copyFileAsync');
332+
});
333+
334+
describe('if screenshot on reject does not exist', () => {
335+
it('should not save screenshot', () => {
336+
const testResult = mkTestResult_({
337+
err: {screenshot: {base64: null}},
338+
assertViewResults: []
339+
});
340+
const hermioneTestAdapter = mkHermioneTestResultAdapter(testResult);
341+
342+
return hermioneTestAdapter.saveTestImages()
343+
.then(() => assert.notCalled(fs.writeFile));
344+
});
345+
346+
it('should warn about it', () => {
347+
const testResult = mkTestResult_({
348+
err: {screenshot: {base64: null}},
349+
assertViewResults: []
350+
});
351+
const hermioneTestAdapter = mkHermioneTestResultAdapter(testResult);
352+
353+
return hermioneTestAdapter.saveTestImages()
354+
.then(() => assert.calledWith(logger.warn, 'Cannot save screenshot on reject'));
355+
});
356+
});
357+
358+
it('should create directory for screenshot', () => {
359+
const testResult = mkTestResult_({
360+
err: {screenshot: {base64: 'base64-data'}},
361+
assertViewResults: []
362+
});
363+
utils.getCurrentPath.returns('dest/path');
364+
const hermioneTestAdapter = mkHermioneTestResultAdapter(testResult);
365+
366+
return hermioneTestAdapter.saveTestImages()
367+
.then(() => assert.calledOnceWith(utils.makeDirFor, sinon.match('dest/path')));
368+
});
369+
370+
it('should save screenshot from base64 format', async () => {
371+
const testResult = mkTestResult_({
372+
err: {screenshot: {base64: 'base64-data'}},
373+
assertViewResults: []
374+
});
375+
utils.getCurrentPath.returns('dest/path');
376+
const bufData = new Buffer('base64-data', 'base64');
377+
const imagesSaver = {saveImg: sandbox.stub()};
378+
const hermioneTestAdapter = mkHermioneTestResultAdapter(testResult, {
379+
htmlReporter: {
380+
imagesSaver
381+
}
382+
});
383+
384+
await hermioneTestAdapter.saveTestImages('report/path');
385+
386+
assert.calledOnceWith(fs.writeFile, sinon.match('dest/path'), bufData, 'base64');
387+
assert.calledWith(imagesSaver.saveImg, sinon.match('dest/path'), {destPath: 'dest/path', reportDir: 'report/path'});
388+
});
389+
});
325390
});
326391

327392
describe('hasDiff()', () => {
@@ -528,67 +593,6 @@ describe('hermione test adapter', () => {
528593
});
529594
});
530595

531-
describe('saveErrorScreenshot', () => {
532-
beforeEach(() => {
533-
sandbox.stub(logger, 'warn');
534-
sandbox.stub(utils, 'makeDirFor').resolves();
535-
sandbox.stub(fs, 'writeFile').resolves();
536-
sandbox.stub(utils, 'copyFileAsync');
537-
});
538-
539-
describe('if screenshot on reject does not exist', () => {
540-
it('should not save screenshot', () => {
541-
const testResult = mkTestResult_({
542-
err: {screenshot: {base64: null}}
543-
});
544-
const hermioneTestAdapter = mkHermioneTestResultAdapter(testResult);
545-
546-
return hermioneTestAdapter.saveErrorScreenshot()
547-
.then(() => assert.notCalled(fs.writeFile));
548-
});
549-
550-
it('should warn about it', () => {
551-
const testResult = mkTestResult_({
552-
err: {screenshot: {base64: null}}
553-
});
554-
const hermioneTestAdapter = mkHermioneTestResultAdapter(testResult);
555-
556-
return hermioneTestAdapter.saveErrorScreenshot()
557-
.then(() => assert.calledWith(logger.warn, 'Cannot save screenshot on reject'));
558-
});
559-
});
560-
561-
it('should create directory for screenshot', () => {
562-
const testResult = mkTestResult_({
563-
err: {screenshot: {base64: 'base64-data'}}
564-
});
565-
utils.getCurrentPath.returns('dest/path');
566-
const hermioneTestAdapter = mkHermioneTestResultAdapter(testResult);
567-
568-
return hermioneTestAdapter.saveErrorScreenshot()
569-
.then(() => assert.calledOnceWith(utils.makeDirFor, sinon.match('dest/path')));
570-
});
571-
572-
it('should save screenshot from base64 format', async () => {
573-
const testResult = mkTestResult_({
574-
err: {screenshot: {base64: 'base64-data'}}
575-
});
576-
utils.getCurrentPath.returns('dest/path');
577-
const bufData = new Buffer('base64-data', 'base64');
578-
const imagesSaver = {saveImg: sandbox.stub()};
579-
const hermioneTestAdapter = mkHermioneTestResultAdapter(testResult, {
580-
htmlReporter: {
581-
imagesSaver
582-
}
583-
});
584-
585-
await hermioneTestAdapter.saveErrorScreenshot('report/path');
586-
587-
assert.calledOnceWith(fs.writeFile, sinon.match('dest/path'), bufData, 'base64');
588-
assert.calledWith(imagesSaver.saveImg, sinon.match('dest/path'), {destPath: 'dest/path', reportDir: 'report/path'});
589-
});
590-
});
591-
592596
describe('timestamp', () => {
593597
it('should return corresponding timestamp of the test result', () => {
594598
const testResult = mkTestResult_({

0 commit comments

Comments
 (0)