-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
waitUntil.js
69 lines (61 loc) · 2.16 KB
/
waitUntil.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
*
* This wait command is your universal weapon if you want to wait on something. It expects a condition
* and waits until that condition is fulfilled with a truthy value. If you use the WDIO testrunner the
* commands within the condition are getting executed synchronously like in your test.
*
* A common example is to wait until a certain element contains a certain text (see example).
*
* <example>
:example.html
<div id="someText">I am some text</div>
<script>
setTimeout(() => {
$('#someText').html('I am now different');
}, 1000);
</script>
:waitUntil.js
it('should wait until text has changed', () => {
browser.waitUntil(() => {
return $('#someText').getText() === 'I am now different'
}, 5000, 'expected text to be different after 5s');
});
* </example>
*
*
* @alias browser.waitUntil
* @param {Function} condition condition to wait on
* @param {Number=} timeout timeout in ms (default: 5000)
* @param {String=} timeoutMsg error message to throw when waitUntil times out
* @param {Number=} interval interval between condition checks (default: 500)
* @return {Boolean} true if condition is fulfilled
* @uses utility/pause
* @type utility
*
*/
import Timer from '../../utils/Timer'
export default function (condition, timeout, timeoutMsg, interval) {
if (typeof condition !== 'function') {
throw new Error('Condition is not a function')
}
/*!
* ensure that timeout and interval are set properly
*/
if (typeof timeout !== 'number') {
timeout = this.options.waitforTimeout
}
if (typeof interval !== 'number') {
interval = this.options.waitforInterval
}
const fn = condition.bind(this)
let timer = new Timer(interval, timeout, fn, true)
return timer.catch((e) => {
if (e.message === 'timeout') {
if (typeof timeoutMsg === 'string') {
throw new Error(timeoutMsg)
}
throw new Error(`waitUntil condition timed out after ${timeout}ms`)
}
throw new Error(`waitUntil condition failed with the following reason: ${(e && e.message) || e}`)
})
}