webdriverio-boneyard / wdio-mocha-framework

A WebdriverIO v4 plugin. Adapter for Mocha testing framework.
MIT License
39 stars 30 forks source link

Mocha not passing done #17

Closed SuperSephy closed 8 years ago

SuperSephy commented 8 years ago

It appears the mocha test files aren't passing the "done" callback parameter in the frame work. Example below:

package.json

"dependencies": {
    "wdio-mocha-framework": "^0.3.1",
    "wdio-selenium-standalone-service": "0.0.5",
    "webdriverio": "^4.0.9"
}

sample.conf.js

exports.config = {
    specs: [
        './tests/login.js'
    ],
    maxInstances: 10,
    capabilities: [{
       maxInstances: 1,
        browserName: 'chrome'
    }],
    sync:true,
    services: ['selenium-standalone'],
    reporters: ['spec'],
    mochaOpts: {
        ui: 'bdd'
    }
}

tests/login.js

var assert      = require('assert');

describe('sample login page', function() {

    it('Should load the correct page', function () {
        browser.url('https://www.sample.com');
        assert.equal(browser.getTitle(), 'Sample Title');
    });

    it('Log in to sample.com', function() {
        browser
            .setValue('input#Username', 'sample')
            .setValue('input#Password', 'sample')
            .submitForm('form[name=sample]')
            .waitForVisible('input[name=somethingOnNextPage]', 10000);

        assert.equal(browser.getTitle(), 'Sample Title');

    });

    it('Send slack notification if error', function(done) {

        //webhook(channel, message, cb)
        slack_api.webhook('@sampleuser', 'Sample Message', function(err, resp){
            if (err) return assert(false, 'Slack notification failed: '+err);
            done();
        });
    });
});

But the slack notification (and several other attempted variants with page interaction) error out with the following:

TypeError: done is not a function
    at Context.<anonymous> (/Sample/Path/App_Repo/tests/login.js:26:13)
    at Context.executeSync (/Sample/Path/App_Repo/node_modules/wdio-sync/build/index.js:620:12)
    at /Sample/Path/App_Repo/node_modules/wdio-sync/build/index.js:814:34

Running these tests directly with Mocha runs fine, so something is happening in the wdio import of the test files. I should note I've tried setting the sample.conf.js sync value to false as well, but this just means that it finishes without waiting for anything and the mocha tests still don't have a defined done function. Similar results setting the async-only: true mochaOpts in the sample.conf.js

christian-bromann commented 8 years ago

@SuperSephy we don't pass the callback variable through because all commands are running synchronously anyway so you don't need to handle async with done. In cases where you want to use 3rd party libraries I would advise you to either wrap your 3rd party command call in a webdriverio custom command (see http://webdriver.io/guide/usage/customcommands.html bottom) or run the test block async and return a promise like

it("...", function async () {
    return new Promise(function(resolve, reject) {
        slack_api.webhook('@sampleuser', 'Sample Message', function(err, resp){
            if (err) return reject(err);
            resolve();
        });
    });
});

Using a callback is not really a good way to handle async as it won't come with proper stack traces.

SuperSephy commented 8 years ago

Appreciate the response, I'll try that. Thank you.

oliviertassinari commented 8 years ago

I have faced the same issue when upgrading from wdio-mocha-framework@0.2.12 to wdio-mocha-framework@0.3.0. I had to change my tests from:

    it('should show the account debts when we navigate to the route', (done) => {
      global.browser
        .url(accountDetailDebtsUrl)
        .getCssProperty('[data-test="AccountDetailTabDebts"]', 'color')
        .then((color) => {
          assert.strictEqual(color.value, 'rgba(255,255,255,1)');
        })
        .call(done);
    });

to


    it('should show the account debts when we navigate to the route', () => {
      return global.browser
        .url(accountDetailDebtsUrl)
        .getCssProperty('[data-test="AccountDetailTabDebts"]', 'color')
        .then((color) => {
          assert.strictEqual(color.value, 'rgba(255,255,255,1)');
        });
    });
christian-bromann commented 8 years ago

@oliviertassinari well help you to debug your test way better 😉

oliviertassinari commented 8 years ago

@christian-bromann I'm pointing it out, in case someone if having the same issue.

At least now mocha also get notified when the promise fails. I'm wondering how he is handling it. I guess you are right, that's more information he can take advantage of 👍 .