Skip to content

Commit e50ab0f

Browse files
author
Nikolai Markov
committed
feat: add TEST_SCREENSHOTS_SAVED event
1 parent ab66df6 commit e50ab0f

File tree

4 files changed

+69
-4
lines changed

4 files changed

+69
-4
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,7 @@ Property name | Description
534534
Event | Description
535535
------------------------- | -------------
536536
`DATABASE_CREATED` | Will be triggered after sqlite database is created. The handler accepts a database instance. The event is synchronous.
537+
`TEST_SCREENSHOTS_SAVED` | Will be triggered after test screenshots were saved. The handler accepts test id and screenshots info. The event is synchronous.
537538
538539
### events
539540
@@ -554,6 +555,29 @@ module.exports = (hermione, opts) => {
554555
};
555556
```
556557
558+
Example of a subscription to an event `TEST_SCREENSHOTS_SAVED`:
559+
560+
```js
561+
hermione.htmlReporter.on(hermione.htmlReporter.events.TEST_SCREENSHOTS_SAVED, ({testId, attempt, imagesInfo}) => {
562+
console.log(`Screenshots for test "${testId}" (attempt #${attempt}) were saved:`, imagesInfo);
563+
/* Expected output:
564+
Screenshots for test "Feature Test.chrome-desktop" (attempt #0) were saved:
565+
[
566+
{
567+
stateName: 'plain',
568+
refImg: { path: '...', size: { width: 400, height: 200 } },
569+
status: 'fail',
570+
error: undefined,
571+
diffClusters: [...],
572+
expectedImg: { path: '...', size: { width: 400, height: 200 } }
573+
actualImg: { path: '...', size: { width: 400, height: 200 } }
574+
diffImg: { path: '...', size: { width: 400, height: 200 } }
575+
}
576+
]
577+
*/
578+
});
579+
```
580+
557581
### addExtraItem
558582
559583
Adds item to html report as link:

lib/constants/plugin-events.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use strict';
22

33
const getSyncEvents = () => ({
4-
DATABASE_CREATED: 'databaseCreated'
4+
DATABASE_CREATED: 'databaseCreated',
5+
TEST_SCREENSHOTS_SAVED: 'testScreenshotsSaved'
56
});
67

78
const events = getSyncEvents();

lib/test-adapter.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,8 @@ module.exports = class TestAdapter {
330330
await fs.writeFile(detailsFilePath, detailsData);
331331
}
332332

333-
saveTestImages(reportPath, workers) {
334-
return Promise.map(this.assertViewResults, async (assertResult) => {
333+
async saveTestImages(reportPath, workers) {
334+
const result = await Promise.map(this.assertViewResults, async (assertResult) => {
335335
const {stateName} = assertResult;
336336
const destRefPath = utils.getReferencePath(this, stateName);
337337
const srcRefPath = this.getRefImg(stateName).path;
@@ -363,6 +363,15 @@ module.exports = class TestAdapter {
363363

364364
return Promise.all(actions);
365365
});
366+
367+
const htmlReporter = this._hermione.htmlReporter;
368+
htmlReporter.emit(htmlReporter.events.TEST_SCREENSHOTS_SAVED, {
369+
testId: this._testId,
370+
attempt: this.attempt,
371+
imagesInfo: this.getImagesInfo()
372+
});
373+
374+
return result;
366375
}
367376

368377
//parallelize and cache of 'gemini-core.Image.buildDiff' (because it is very slow)

test/unit/lib/test-adapter.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ describe('hermione test adapter', () => {
2929
stubConfig(config),
3030
{},
3131
{ImageDiffError, NoRefImageError},
32-
Object.assign({imagesSaver: {saveImg: sandbox.stub()}}, htmlReporter)
32+
Object.assign({
33+
imagesSaver: {saveImg: sandbox.stub()},
34+
events: {TEST_SCREENSHOTS_SAVED: 'testScreenshotsSaved'},
35+
emit: sinon.stub()
36+
}, htmlReporter)
3337
);
3438

3539
return new HermioneTestResultAdapter(testResult, tool, pluginConfig, status);
@@ -291,6 +295,33 @@ describe('hermione test adapter', () => {
291295
{destPath: 'diff/report/path', reportDir: 'html-report/path'}
292296
);
293297
});
298+
299+
it('should emit TEST_SCREENSHOTS_SAVED event', async () => {
300+
tmp.tmpdir = 'tmp/dir';
301+
const testResult = mkTestResult_({
302+
id: () => '',
303+
browserId: 'chrome',
304+
assertViewResults: [err]
305+
});
306+
utils.getDiffPath.returns('diff/report/path');
307+
308+
const htmlReporterEmitStub = sinon.stub();
309+
const hermioneTestAdapter = mkHermioneTestResultAdapter(testResult, {
310+
htmlReporter: {
311+
emit: htmlReporterEmitStub
312+
}
313+
});
314+
sinon.stub(hermioneTestAdapter, 'getImagesInfo').returns([{test: 123}]);
315+
const workers = {saveDiffTo: sandbox.stub()};
316+
317+
await hermioneTestAdapter.saveTestImages('', workers);
318+
319+
assert.calledOnceWith(htmlReporterEmitStub, 'testScreenshotsSaved', {
320+
attempt: 0,
321+
testId: 'default-title.chrome',
322+
imagesInfo: [{test: 123}]
323+
});
324+
});
294325
});
295326

296327
describe('hasDiff()', () => {

0 commit comments

Comments
 (0)