Improve this doc

Timeouts

Each command in WebdriverIO is an asynchronous operation where a request is fired to the Selenium server (or a cloud service like Sauce Labs), and its response contains the result once the action has completed or failed. Therefore time is a crucial component in the whole testing process. When a certain action depends on the state of a different action, you need to make sure that they get executed in the right order. Timeouts play an important role when dealing with these issues.

Selenium timeouts

Session Script Timeout

A session has an associated session script timeout that specifies a time to wait for asynchronous scripts to run. Unless stated otherwise it is 30 seconds. You can set this timeout via:

1
2
3
4
5
browser.timeouts('script', 60000);
browser.executeAsync(function (done) {
console.log('this should not fail');
setTimeout(done, 59000);
});

Session Page Load Timeout

A session has an associated session page load timeout that specifies a time to wait for the page loading to complete. Unless stated otherwise it is 300,000 milliseconds. You can set this timeout via:

1
browser.timeouts('pageLoad', 10000);

The pageLoad keyword is a part of the official WebDriver specification, but might not be supported for your browser (the previous name is page load).

Session Implicit Wait Timeout

A session has an associated session implicit wait timeout that specifies a time to wait for the implicit element location strategy when locating elements using the element or elements commands. Unless stated otherwise it is zero milliseconds. You can set this timeout via:

1
browser.timeouts('implicit', 5000);

WaitForXXX timeout

WebdriverIO provides multiple commands to wait on elements to reach a certain state (e.g. enabled, visible, existing). These commands take a selector argument and a timeout number which declares how long the instance should wait for that element to reach the state. The waitforTimeout option allows you to set the global timeout for all waitFor commands so you don’t need to set the same timeout over and over again. Note the lowercase f.

1
2
3
4
5
6
// wdio.conf.js
exports.config = {
// ...
waitforTimeout: 5000,
// ...
};

In your test you now can do this:

1
2
3
4
5
6
7
8
var myElem = browser.element('#myElem');
myElem.waitForVisible();

// which is the same as
browser.waitForVisible('#myElem');

// which is the same as
browser.waitForVisible('#myElem', 5000);

Also the testing framework you use with WebdriverIO has to deal with timeouts especially since everything is asynchronous. It ensures that the test process don’t get stuck if something went wrong. By default the timeout is set to 10 seconds which means that a single test should not take longer than that. A single test in Mocha looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
it('should login into the application', function () {
browser.url('/login');

var form = browser.element('form');
var username = browser.element('#username');
var password = browser.element('#password');

username.setValue('userXY');
password.setValue('******');
form.submit();

expect(browser.getTitle()).to.be.equal('Admin Area');
});

In Cucumber the timeout applies to a single step definition. However if you want to increase the timeout because your test takes longer than the default value you need to set it in the framework options. This is for Mocha:

1
2
3
4
5
6
7
8
9
// wdio.conf.js
exports.config = {
// ...
framework: 'mocha',
mochaOpts: {
timeout: 20000
},
// ...
}

For Jasmine:

1
2
3
4
5
6
7
8
9
// wdio.conf.js
exports.config = {
// ...
framework: 'jasmine',
jasmineNodeOpts: {
defaultTimeoutInterval: 20000
},
// ...
}

and for Cucumber:

1
2
3
4
5
6
7
8
9
// wdio.conf.js
exports.config = {
// ...
framework: 'cucumber',
cucumberOpts: {
timeout: 20000
},
// ...
}