从钩子抛出错误后将文本或图像附加到Cucumber报告

时间:2017-11-23 01:55:36

标签: protractor cucumberjs

代码在步骤定义中正常工作,但在钩子之后没有。

下面是我的钩子代码。当我在世界上调用attach函数时它失败了。

var {defineSupportCode} = require('cucumber');

defineSupportCode(function({After, Before, BeforeStep, StepResult}) {
    After(function(scenario,done)
    {
        console.log('after');
        const world = this;
        if (scenario.result.status === 'failed') {
            browser.takeScreenshot().then(function (stream) {
                // writeScreenShot(png, 'exception.png');
                let decodedImage = new Buffer(stream.replace(/^data:image\/(png|gif|jpeg);base64,/, ''), 'base64');
                world.attach(decodedImage, 'image/png');
            });
        }
        if(browser.browserName !== 'firefox')
        {
            browser.manage().logs().get('browser').then(function(browserLog){
                console.log('log: ' + require('util').inspect(browserLog));
                world.attach('Browser Log : ' + JSON.stringify(browserLog),'text/plain');
            });
        }
        done();
    });
});

以下是我执行时收到的错误消息。

E/launcher - Cannot read property 'attachments' of undefined
[12:51:19] E/launcher - TypeError: Cannot read property 'attachments' of undefined
    at EventDataCollector.storeTestStepAttachment (C:\ProtractorTest\node_modules\cucumber\src\formatter\helpers\event_data_collector.js:59:10)
    at emitOne (events.js:96:13)
    at EventEmitter.emit (events.js:188:7)
    at TestCaseRunner.emit (C:\ProtractorTest\node_modules\cucumber\src\runtime\test_case_runner.js:51:27)
    at AttachmentManager.onAttachment (C:\ProtractorTest\node_modules\cucumber\src\runtime\test_case_runner.js:17:12)
    at AttachmentManager.createStringAttachment (C:\ProtractorTest\node_modules\cucumber\src\runtime\attachment_manager\index.js:59:10)
    at AttachmentManager.create (C:\ProtractorTest\node_modules\cucumber\src\runtime\attachment_manager\index.js:24:12)
    at C:\ProtractorTest\feature_files\hooks\hooks.js:31:23
    at ManagedPromise.invokeCallback_ (C:\ProtractorTest\node_modules\selenium-webdriver\lib\promise.js:1376:14)
    at TaskQueue.execute_ (C:\ProtractorTest\node_modules\selenium-webdriver\lib\promise.js:3084:14)
    at TaskQueue.executeNext_ (C:\ProtractorTest\node_modules\selenium-webdriver\lib\promise.js:3067:27)
    at asyncRun (C:\ProtractorTest\node_modules\selenium-webdriver\lib\promise.js:2927:27)
    at C:\ProtractorTest\node_modules\selenium-webdriver\lib\promise.js:668:7
    at process._tickCallback (internal/process/next_tick.js:109:7)
[12:51:19] E/launcher - Process exited with error code 199

Process finished with exit code 199

这一直工作到上周,现在它引发了上述错误,我无法弄清楚它为什么会发生。

如果您需要更多信息,请与我们联系。

1 个答案:

答案 0 :(得分:1)

以下代码正确回调修复了我的问题。不知道为什么,但它解决了这个问题。

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();
    }
});