There might be a situation where our script fails or taking long time if the application doesn’t load properly.
Other factors could be application response, network speed or the layers that webdriverIO uses to send requests which causes out of sync between script run and application actions.
Example – Test is looking for welcome screen after login, but application takes some time to process the authentication. in this case our script might fail, so we need to wait for the application to display the welcome screen.
There are certain situations where we don’t want to wait for infinite time if the application doesn’t respond, we need to fail the test so we can identify if it’s application having some functionality issue (element not present or screen not loading) or having performance issue, in that case we have to limit the time of waiting and better to fail the test.
Timeout types –
Page load timeout –
Some times the page takes some time to load completely, so we can wait certain amount of time before performing any actions on screen.
Let’s say you wan to navigate to another page by clicking Next button, then wdio will wait for the specified time (in milliseconds) to load the page else it will fail the test.
By default wdio waits for 30sec max for a page to loa. but we can change the time as below.
you can specify below code line to set the page load timeout for that particular browser session.
await browser.setTimeout({ 'pageLoad': 10000 })
or we can specify in wdio.conf.ts file to make it available as global page load wait time
//wdio.conf.ts - under capabilities section ... capabilities: [{ browserName: 'chrome', timeouts: { pageLoad: 10000 }, ... }] ...
Script timeout –
Let’s say we want to restrict an asynchronoss script to wait certain amount time, after which the script will fail.
Let’s say certain steps takes more time, we want to synchronise with the application but to certain amount of time.
By default wdio waits for 30sec max for script to run, else it fails the script
you can specify below code line to set the script timeout for that particular browser session.
await browser.setTimeout({ 'script': 10000 })
or we can specify in wdio.conf.ts file to make it available as global page load wait time
//wdio.conf.ts - under capabilities section ... capabilities: [{ browserName: 'chrome', timeouts: { pageLoad: 10000, script: 10000 }, ... }] ...
Framework timeout –
wdio allows to set the timeout for a unit test framework to wait, you can have either mocha / jasmine / cucumber to write tests.
By default the timeout is set to 20sec, means each test will wait for 20sec to completly run all the steps. It will fail the test if exceeds the time.
But you can change it under wdio.conf.ts file –
Example – think about a scenario where you want to debug your test and want to go in deep to each of the underlying methods, then it might take more time as human interaction to run the steps, in that case we can have a conditional based wait time (will see in future topics).
For Jasmine framework –
// wdio.conf.js exports.config = { // ... framework: 'jasmine', jasmineOpts: { defaultTimeoutInterval: 20000 }, // ... }
For Mocha framework –
// wdio.conf.js exports.config = { // ... framework: 'mocha', mochaOpts: { timeout: 20000 }, // ... }
For cucumber framework –
// wdio.conf.js exports.config = { // ... framework: 'cucumber', cucumberOpts: { timeout: 20000 }, // ... }
Implicit vs explicit timeout –
Implicit –
This will wait for all the elements to locate/identify, defaults to 0ms
This can be changed under capabilities section of wdio.config.ts
// wdio.conf.js capabilities: [{ ... browserName: 'chrome', timeouts: { implicit: 10000}, ... }]
Note – this above is applicable only for single element identification, for multiple elements we need to use explicit wait or waitUntil for all the elements to exist/appear on the DOM.
Implicit waits are not useful when we want to wait for an element to disappear.
Explicit –
We can apply wait to Individual elements waitFor* commands
Note – Use wdio.config.ts waitforTimeout to set the timeout globally for all waitFor* commands
waitUntil timeout –
Wait until a condition satisfies.
Examples –
Wait for an alert to appear
Wait for a browser URL/title to match certain String
Wait for an element till the text matches
Reference –
https://webdriver.io/docs/timeouts/
https://webdriver.io/docs/api/browser/waitUntil/