webdriverio-boneyard / wdio-sync

A WebdriverIO v4 plugin. Helper module to run WebdriverIO commands synchronously.
http://v4.webdriver.io
MIT License
17 stars 31 forks source link

Error in custom function causes it to be run a second time #17

Closed georgecrawford closed 8 years ago

georgecrawford commented 8 years ago

As mentioned in https://github.com/webdriverio/wdio-sync/pull/12:

        browser.addCommand('mycustomfunc', function () {
            console.log('mycustomfunc running now', Error().stack);
            throw Error('mycustomfunc error');
        });

prints:

mycustomfunc running now Error
    at Error (native)
    at Object.<anonymous> (/Users/george/Shell/sandboxes/ft-app-webdriverio/build/test/specs/startup.js:43:37)
    at /Users/george/Sites/Git/wdio-sync/build/index.js:187:29
mycustomfunc running now Error
    at Error (native)
    at Object.<anonymous> (/Users/george/Shell/sandboxes/ft-app-webdriverio/build/test/specs/startup.js:43:37)
    at /Users/george/Sites/Git/wdio-sync/build/index.js:187:29

(two calls here: https://github.com/webdriverio/wdio-sync/blob/master/index.js#L60). Also note the stack trace isn't very useful here, and should be improved if possible.

An async custom function is similarly called twice:

mycustomfunc running now Error
    at Error (native)
    at Object.async (/Users/george/Shell/sandboxes/ft-app-webdriverio/build/test/specs/startup.js:43:37)
    at Object.<anonymous> (/Users/george/Sites/Git/wdio-sync/build/index.js:348:30)
    at /Users/george/Sites/Git/wdio-sync/build/index.js:250:23
    at process._tickCallback (node.js:379:9)
mycustomfunc running now Error
    at Error (native)
    at Object.async (/Users/george/Shell/sandboxes/ft-app-webdriverio/build/test/specs/startup.js:43:37)
    at Object.<anonymous> (/Users/george/Sites/Git/wdio-sync/build/index.js:348:30)
    at Object.mycustomfunc (/Users/george/Sites/Git/wdio-sync/build/index.js:273:23)
    at Context.<anonymous> (/Users/george/Shell/sandboxes/ft-app-webdriverio/build/test/specs/startup.js:78:11)
    at /Users/george/Sites/Git/wdio-sync/build/index.js:401:24

(first call here: https://github.com/webdriverio/wdio-sync/blob/master/index.js#L115, second call here: https://github.com/webdriverio/wdio-sync/blob/master/index.js#L143).

Whichever way we handle failures when calling a future, I don't think the custom function should be called twice - I think that's unexpected from a user's point of view.

christian-bromann commented 8 years ago

Good catch. We should check the error message here to only run it twice when the future call breaks.

georgecrawford commented 8 years ago

What kind of code would cause the future call to break?

christian-bromann commented 8 years ago

Of you call Future.wait() not within the same event loop where you call Fiber(...).run() it breaks. This happens for instance when you do this:

browser.someCommand(); // works
process.nextTick(function() {
    browser.someCommand(); // breaks
})
georgecrawford commented 8 years ago

OK, I'll try that