belykh
belykh

Reputation: 1290

Webdriver.io how to send data to custom reporter

I use wdio tool from webdriver.io npm package to run Mocha test-cases.

Here is part of wdio.conf.js:

var htmlReporter = require('./js/reporter/htmlReporter');
htmlReporter.reporterName = 'htmlReporter';

exports.config = {
    specs: [
        './test.js'
    ],
    reporters: [htmlReporter],
    ...
}

test.js: should send custom data

describe('Test suite', function() {
    // is it possible to send some data to the current test-suite?
    // this.customData ?
    it('Test case', function() {
        // is it possible to send some data to the current test-case?
        // this.customData ?
    });
});

});

htmlReporter.js: should receive custom data

var htmlReporter = function(options) {
var self = this;
    this.on('suite:start', function(suite) {
        // how to get a custom data?
        // suite.customData is undefined
    });

    this.on('test:pass', function(test) {
        // how to get a custom data?
        // suite.customData is undefined
    });
    ...
}

Upvotes: 0

Views: 770

Answers (1)

Ponmudi VN
Ponmudi VN

Reputation: 1553

Faced the same problem to send custom message to failed tests. Added message in test error object and re-throw the error

describe('Test suite', function() {
    it('Test case', function() {
       try {
          //test failed due to error!
       } catch(err) {
         err.message.myCustomMessage = "Test failed due to XXX".
         throw err;
       } finally {

       }
    });
});

Then in the custom reporter

this.on('test:fail', function(test) {
    var myCustomMessage = test.err.message.myCustomMessage;
});

Not sure if there is any other offical/standard way, but this serverd the purpose.

Thanks.

Upvotes: 1

Related Questions