Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: screenshot error in beforeSuite/AfterSuite #4385

Merged
merged 2 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
});
Loading