Sync vs. Async Mode
WebdriverIO runs a set of asynchronous commands to interact with the browser or mobile device. In JavaScript asynchronous operations are handled via async/await
. This however can be a confusing concept for people unfamiliar with the language. In addition to that it can make tests very verbose as almost every operation is asynchronous. To simplify its usage WebdriverIO provides the ability to run commands synchronous through node-fibers
.
#
How to enable/disable sync modeTo enable sync mode you only need to add the @wdio/sync
package to your dev dependencies:
- npm
- Yarn
The package will be automatically detected by the framework and the environment properly set up to run synchronous.
#
Sync modeIf you're using @wdio/sync
then you can avoid awaiting for command calls. It is still required to deal with Promises from 3rd-party libraries, you should use browser.call for this to wrap them and make them synchronous too.
#
Common issues in sync modefibers
failed to install properly. The package usually comes with pre-built binaries but if your environment doesn't support it these need to be compiled which require node-gyp and Python.
#
Async ModeIf you decide to run in async mode all WebdriverIO commands return a Promise and need to be awaited to get the result, e.g.:
#
Common issues in async modeThere can be quite some confusion when handling asynchronous commands manually. The usual problems are:
chaining of element commands:
You can't chain element calls as you have to await multiple asynchronous functions. To fix this, first await element then trigger the click, like so:
previous command was not awaited: