function to take screenshots for cucumber-html-reporter generates “function timed out after 5000..” error

时光怂恿深爱的人放手 提交于 2019-12-04 10:59:51
Naveen Kattimani

Below code is working for me. I have added this in step definition js file. At the end of scenario in the report, it adds the screenshot.

defineSupportCode(({After}) => {
    After(function(scenario) {
        if (scenario.isFailed()) {
            var attach = this.attach; 
                return browser.takeScreenshot().then(function(png) {
            var decodedImage = new Buffer(png, "base64");
                return attach(decodedImage, "image/png");
            });
            }
        });
    });

Your code seems absolutely fine. Perhaps it just needs longer?

You can set a timeout on hooks like this:

this.After({ timeout: 20 * 1000 }, function (scenario, callback) {
  if (scenario.isFailed()) {
    browser.takeScreenshot().then(function(buffer) {
        return scenario.attach(new Buffer(buffer, 'base64'), function(err) {
            callback(err);
        });
    });
  }
  else {
    callback();
  }
});

I had similar problem and it failed even after waiting for 60 seconds. The issue was that i didn't had proper callback implemented.

The below code worked for me. ( I am new to JavaScript and so my callback usage might be the right way. Please feel free to educate me if there is better way to do it. :))

After(function(scenario,done)
{
    const world = this;
    if (scenario.result.status === 'failed') {
        browser.takeScreenshot().then(function (stream) {
            let decodedImage = new Buffer(stream.replace(/^data:image\/(png|gif|jpeg);base64,/, ''), 'base64');
            world.attach(decodedImage, 'image/png');
        }).then(function () {
            done();
        });
    }else {
        done();
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!