0

I am currently writing an integration test with Mocha, Chai, and WebdriverIO. The WebdriverIO syntax requires a chain of promises to navigate through the browser, an example of which is below:

it('sign in and out test', function() {
  return client
    .url(Page.url)
    .pause(20000)
    .waitForVisible(HomePage.signInDropdown, 10000)
    .click(HomePage.signInDropdown)
}

This leads to long blocks of code with every step laid out explicitly. Since some of the steps (such as logging in and logging out) are used frequently throughout different tests, I want to modularize those snippets of code through callbacks. However, the following syntax, which works without a nested callback, fails:

function myCallback(){
  console.log('This is where I''d add promises');
}

it('sign in and out test',function() {
  return client
    .url(Page.url)
    .pause(20000)
    .then(function() {
      myCallback();
    }
    .waitForVisible(HomePage.signInDropdown, 10000)
    .click(HomePage.signInDropdown)
}

The following different syntax in the callback also fails:

function myCallback(){
  return client
    .url(Page.url)
  console.log('This is a callback using return');
}

Given that promises are mostly intended to replace callbacks, is it possible in this special case to continue the promise chain through a callback?

alexmac
  • 19,087
  • 7
  • 58
  • 69
223seneca
  • 1,136
  • 3
  • 19
  • 47
  • Please describe "fails". Are there any errors on the console I (other than unreachable code after return statement)? Versions of `myCallback` which don't return a promise (first syntax) will not hold up promise chain processing. Documentation says `url` returns a string. Is it wrong or misleading? – traktor Sep 29 '17 at 06:28

2 Answers2

0

myCallback() is not returned from .then().

.then(function() {
  // `return` value from `myallback()`
  return myCallback();
})
guest271314
  • 1
  • 15
  • 104
  • 177
  • @JaromandaX Did not state the anything was "wrong" with `.then(myCallback)`. – guest271314 Sep 29 '17 at 05:54
  • @JaromandaX Have used the pattern, following this Question and Answer with additional considerations that had not prior to the answers [Are there differences between .then(functionReference) and .then(function(value){return functionReference(value)})?](https://stackoverflow.com/questions/41191131/are-there-differences-between-thenfunctionreference-and-thenfunctionvalue). Just noticed your second comment at present Question – guest271314 Sep 29 '17 at 05:57
  • 1
    I'd suggest that since myCallback does not accept any arguments, and `.pause(20000)` is probably something whose promise resolves to nothing interesting, that it is safe in this case - not criticizing your code at all, just wondering – Jaromanda X Sep 29 '17 at 05:59
  • @JaromandaX Tried to adjust OP's code as little as possible to return expected result. The answer to the present inquiry can also be found at [Why is value undefined at .then() chained to Promise?](https://stackoverflow.com/questions/44439596/why-is-value-undefined-at-then-chained-to-promise) – guest271314 Sep 29 '17 at 06:02
-1

after logout promise resolve we need to do other works in chain.

Please check this,

function myCallback(){
  return client
    .url(Page.url)
}

it('sign in and out test',function() {
  return client
    .url(Page.url)
    .pause(20000)
    .then(function() {
      myCallback()
         .waitForVisible(HomePage.signInDropdown, 10000)
         .click(HomePage.signInDropdown);
    }
}