cucumberjs: finding if a step Result is failed
What you need is an after hook
Create a file features/support/after_hooks.js
module.exports = function() { this.After(function (scenario, callback) { if (scenario.isFailed()) { // Do your after stuff here } callback(); });};
Note that this is only executed after every feature
You can get more details in (cucumberjs 1.x) https://github.com/cucumber/cucumber-js/blob/1.x/lib/cucumber/api/scenario.js#L27
getKeyword: function getKeyword() { return astScenario.getKeyword();},getName: function getName() { return astScenario.getName();},getDescription: function getDescription() { return astScenario.getDescription();},getUri: function getUri() { return astScenario.getUri();},getLine: function getLine() { return astScenario.getLine();},getTags: function getTags() { return astScenario.getTags();},isSuccessful: function isSuccessful() { return scenarioResult.getStatus() === Cucumber.Status.PASSED;},isFailed: function isFailed() { return scenarioResult.getStatus() === Cucumber.Status.FAILED;},isPending: function isPending() { return scenarioResult.getStatus() === Cucumber.Status.PENDING;},isUndefined: function isUndefined() { return scenarioResult.getStatus() === Cucumber.Status.UNDEFINED;},isSkipped: function isSkipped() { return scenarioResult.getStatus() === Cucumber.Status.SKIPPED;},getException: function getException() { return scenarioResult.getFailureException();},getAttachments: function getAttachments() { return attachments;},clearAttachments: function clearAttachments() { attachments = [];},
Create afterhook.js and check for failing scenario You can use browser.saveScreenshot for saving the screenshot . While saving screenshot use a timestamp to the file name and also exclude the screenshots from version control
module.exports = function() { this.After((scenario, callback) => { if (scenario.isFailed()) { const d = new Date(); browser.saveScreenshot( `./e2e/screenshots/screenshot-${d.toISOString()}.png` ); } callback(); });};