Skip to content

Commit

Permalink
fix: screenshot error in beforeSuite/AfterSuite (#4385)
Browse files Browse the repository at this point in the history
  • Loading branch information
kobenguyent committed Jun 8, 2024
1 parent 2469f24 commit 1040494
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
4 changes: 4 additions & 0 deletions lib/plugin/screenshotOnFail.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ module.exports = function (config) {
}

event.dispatcher.on(event.test.failed, (test) => {
if (test.ctx?._runnable.title.includes('hook: ')) {
output.plugin('screenshotOnFail', 'BeforeSuite/AfterSuite do not have any access to the browser, hence it could not take screenshot.');
return;
}
recorder.add('screenshot of failed test', async () => {
let fileName = clearString(test.title);
const dataType = 'image/png';
Expand Down
12 changes: 7 additions & 5 deletions lib/plugin/stepByStepReport.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,23 @@ module.exports = function (config) {
currentTest = test;
});

event.dispatcher.on(event.step.failed, persistStep);

event.dispatcher.on(event.step.after, (step) => {
event.dispatcher.on(event.step.failed, (step) => {
recorder.add('screenshot of failed test', async () => persistStep(step), true);
});

event.dispatcher.on(event.step.after, persistStep);

event.dispatcher.on(event.test.passed, (test) => {
if (!config.deleteSuccessful) return persist(test);
// cleanup
deleteDir(dir);
});

event.dispatcher.on(event.test.failed, (test, err) => {
// BeforeSuite/AfterSuite don't have any access to the browser, hence it could not take screenshot.
if (test.ctx._runnable.title.includes('hook: BeforeSuite')) return;
if (test.ctx._runnable.title.includes('hook: ')) {
output.plugin('stepByStepReport', 'BeforeSuite/AfterSuite do not have any access to the browser, hence it could not take screenshot.');
return;
}
persist(test, err);
});

Expand Down
14 changes: 14 additions & 0 deletions test/unit/plugin/screenshotOnFail_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,19 @@ describe('screenshotOnFail', () => {
const regexpFileName = /test1_[0-9]{10}.failed.png/;
expect(fileName.match(regexpFileName).length).is.equal(1);
});

it('should not save screenshot in BeforeSuite', async () => {
screenshotOnFail({ uniqueScreenshotNames: true });
event.dispatcher.emit(event.test.failed, { title: 'test1', ctx: { _runnable: { title: 'hook: BeforeSuite' } } });
await recorder.promise();
expect(!screenshotSaved.called).is.ok;
});

it('should not save screenshot in AfterSuite', async () => {
screenshotOnFail({ uniqueScreenshotNames: true });
event.dispatcher.emit(event.test.failed, { title: 'test1', ctx: { _runnable: { title: 'hook: AfterSuite' } } });
await recorder.promise();
expect(!screenshotSaved.called).is.ok;
});
// TODO: write more tests for different options
});

0 comments on commit 1040494

Please sign in to comment.