capture screenshots only on failure with allure reporter

时间:2017-06-15 09:59:49

标签: node.js jasmine allure

I fallowed tutorial on https://github.com/allure-framework/allure-jasmine I get a screenshot every spec I run even if pass. I want to achieve, that screenshots will be captured only when test fails.

adding screenshot to test(this is how my conf.js looks like)

  onPrepare: function () {
    var AllureReporter = require('jasmine-allure-reporter');
    jasmine.getEnv().addReporter(new AllureReporter());
    jasmine.getEnv().afterEach(function(done){
      browser.takeScreenshot().then(function (png) {
        allure.createAttachment('Screenshot', function () {
          return new Buffer(png, 'base64')
        }, 'image/png')();
        done();
      })
    });
  }

1 个答案:

答案 0 :(得分:0)

以下脚本可能会对您有所帮助,您需要在protractor.conf.js上检查specDone时的状态,如下所示:

let addScreenShots = new function () {
    this.specDone = function (result) {        
        if (result.status === "failed") {
            browser.takeScreenshot().then(function (png) {
                allure.createAttachment('Screenshot', function () {
                    return new Buffer(png, 'base64')
                }, 'image/png')();
            });
        }
    };
}

exports.config = {
    ...
    jasmine.getEnv().addReporter(addScreenShots);
    jasmine.getEnv().addReporter(new AllureReporter({
        allureReport: {
            resultsDir: './test-results/results/'
        }
    }));
    ...
}