call
You can use call
to execute any async action within your test spec. The command itself
is treated like a synchronous function. It accepts promises and stops the execution until
the promise has resolved.
This command helps to run asynchronous code within a synchronous context. With WebdriverIO depcrecating synchronous usage (see RFC) this command is not very useful anymore.
Usage
browser.call(callback)
Parameters
Name | Type | Details |
---|---|---|
callback | Function | function to be called |
Example
- Asynchronous Mode
- Synchronous Mode
call.js
it('some testing here', async () => {
await browser.url('http://google.com')
// make an asynchronous call using any 3rd party library supporting promises
// e.g. call to backend or db to inject fixture data
await browser.call(() => {
return somePromiseLibrary.someMethod().then(() => {
// ...
})
})
// example for async call to 3rd party library that doesn't support promises
const result = await browser.call(() => {
return new Promise((resolve, reject) => {
someOtherNodeLibrary.someMethod(param1, (err, res) => {
if (err) {
return reject(err)
}
resolve(res)
})
})
})
});
call.js
it('some testing here', () => {
browser.url('http://google.com')
// make an asynchronous call using any 3rd party library supporting promises
// e.g. call to backend or db to inject fixture data
browser.call(() => {
return somePromiseLibrary.someMethod().then(() => {
// ...
})
})
// example for call to 3rd party library that doesn't support promises
const result = browser.call(() => {
return new Promise((resolve, reject) => {
someOtherNodeLibrary.someMethod(param1, (err, res) => {
if (err) {
return reject(err)
}
resolve(res)
})
})
})
});
caution
Synchronous Mode will depcrecated with Node.js v16. With an update to the underlying Chromium version it became technically impossible to provide the same synchronous behavior. We recommend to start transition to asynchronous command execution. For more information, see our RFC.