钩子后的 Protractor 和 Cucumber Js 无法按预期工作

Protractor & Cucumberjs after hook doesn't work as expected

出于某种原因,我在 cucumberjs 中编写了一个基本的 after hook,它不起作用,因为 expected.It 应该在场景失败时附加屏幕截图并写入浏览器控制台日志。但它在 html 报告中的功能之后附上了屏幕截图,并在第二个场景 enter image description here 之后打印了浏览器控制台日志。任何线索有什么问题吗??

     this.After(function(scenario, callback) {       
        if (scenario.isFailed()) {
            global.browser.takeScreenshot().then(function(base64png) {
            var decodedImage = new Buffer(base64png,'base64').toString('binary');
            scenario.attach(decodedImage, 'image/png');
       });
        global.browser.manage().logs().get('browser').then(function (browserlog){         
        browserlog.forEach(function (log) {
          if (log.level.value > 900) {
             console.error(log.message.substring(log.message.indexOf('Error'),log.message.indexOf('\n')))
           }
         })           
        });
        callback();
    } else {
        callback();
    }

});

根据 cucumberjs github 页面 https://github.com/cucumber/cucumber-js#attachments

Images and other binary data can be attached using a stream.Readable. In that case, passing a callback to attach() becomes mandatory:

您可以将单个 after 钩子拆分为两个单独的钩子:

this.After(function(scenario, next) {
  browser.takeScreenshot().then(function(png) {
    var decodedImage = new Buffer(png, 'base64').toString('binary');
    scenario.attach(decodedImage, 'image/png', next);
  }, function(err) {
    next(err);
  });
});

this.After(function(scenario, next) {
  global.browser.manage().logs().get('browser').then(function (browserlog){
    browserlog.forEach(function (log) {
      if (log.level.value > 900) {
        console.error(log.message.substring(log.message.indexOf('Error'),log.message.indexOf('\n')))
      }
    });
  });
});