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

Attaching text or image to Cucumber report from after hook throws error

代码在步骤定义中运行良好,但在 after hook 中运行不正常。

下面是我的钩子代码。当我在世界上调用附加功能时失败。

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

这一直工作到上周,现在它抛出上述错误,我不知道为什么会这样。

如果您需要更多信息,请告诉我。

下面带有适当回调的代码解决了我的问题。不知道为什么,但它解决了问题。

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