Navin
Navin

Reputation: 298

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

The code works fine inside step definitions but not in after hook.

Below is my after hook code. It fails when I call attach function on world.

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

Below is the error message I get when I executing it.

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

This was working till last week and now it throws the above error and I couldn't figure out why it is happening.

Please let me know if you need more info.

Upvotes: 1

Views: 5350

Answers (1)

Navin
Navin

Reputation: 298

The below code with proper callback fixed my problem. Not sure why but it resolved the problem.

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

Upvotes: 3

Related Questions