I write tests on CoffeeScript using Webdriver.io framework (Wdio testrunner) with sync mode enabled. According to the documentation, Webdriver.io commands should be executed in synchronous mode. However, in the process of using the Promise, an unexpected problem arose.

As an example, we will consider the simplest test that finds an element on a page according to a given selector and displays the text of the found element to the console.

Example 1 - code without using Promise

browser.url('... URL ...') a = browser.$('... selector ...').getText() console.log(a) 

In this simplest case, the Webdriver.io commands work correctly.

Example 2 - the code is in the Promise constructor

 p = new Promise((resolve, reject) -> browser.url('... URL ...') a = browser.$('... selector ...').getText() console.log(a) resolve() ) return p 

If the commands are placed in the Promise constructor, then they are still correctly executed.

Example 3 - the code is in the block .then after returning Promise

 p = new Promise((resolve, reject) -> resolve() ).then(() -> browser.url('... URL ...') a = $('... selector ...').getText() console.log(a) ) return p 

The console displays the following error message: "$(...).getText is not a function" (Example 3). Apparently, in this case, the commands Webdriver.io begin to work asynchronously. You can wait and process the Promise with the await construction, but we need to execute the code in the same way (synchronously) regardless of the location of the code (in the Promise or outside it).

Also switching to asynchronous mode occurs when using the keyword Await.

Example 4 (Example 1 code using the await keyword)

 await console.log('123') browser.url('... URL ...') a = browser.$('... selector ...').getText() console.log(a) 

In this case, for the program to work correctly, it will be necessary to redo all the code, taking into account asynchronous processing.

As a solution, I can write all the tests asynchronously, but the code will become more complicated and more. Can I work with Webdriver.io commands synchronously even when using Promise?

    1 answer 1

    The answer was suggested on the English language stackoverflow.com

    To ensure synchronous execution of functions in wdio, you can use the browser.call () command. Link to command description: https://webdriver.io/docs/api/browser/call.html