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, an unexpected problem appears in the process of using the Promise.
We are considering the simplest test that finds an element on a page by selector and displays the text of the found element to the console.
Example 1 – code without promise
browser.url('... URL ...')
a = browser.$('... selector ...').getText()
console.log(a)
In this example commands of the Webdriver.io work correctly.
Example 2 - the code is in the constructor of the Promise
p = new Promise((resolve, reject) ->
browser.url('... URL ...')
a = browser.$('... selector ...').getText()
console.log(a)
resolve()
)
return p
If the commands are contained in the constructor of the Promise, then they are correctly executed too.
Example 3 - the code is in the block .then after returning the Promise
p = new Promise((resolve, reject) ->
resolve()
).then(() ->
browser.url('... URL ...')
a = $('... selector ...').getText()
console.log(a)
)
return p
The next error message shows in display: "$ (...). GetText is not a function" (Example 3). Apparently, the commands of the Webdriver.io begin to work asynchronously. Though I can use await keyword to process those Promises but I want to execute the code equally 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 command Await is used.
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 correctly work of program 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. Can I work with commands of the Webdriver.io synchronously even when using the Promise?
If you want to use Promise
in your wdio test script which in sync mode, then you need to use browser.call()
of wdio. More details on call
: v5 and v4 .
Here you can find the sample code and more details on how a call is used: How to use 3rd party method that takes callback in webdriverio
Thanks, Naveen