1
- import json
2
1
import time
3
2
4
3
@@ -14,16 +13,21 @@ def wrap():
14
13
return wrap
15
14
16
15
17
-
18
16
class WaitForTimeout (Exception ):
19
17
"""This should only be raised inside the `wait_for` function."""
20
18
pass
21
19
22
20
23
- def wait_for (condition_function , get_message = lambda : '' , * args , ** kwargs ):
21
+ def wait_for (condition_function , expected_value = None , timeout = TIMEOUT ,
22
+ get_message = None , * args , ** kwargs ):
24
23
"""
25
- Waits for condition_function to return True or raises WaitForTimeout.
26
- :param (function) condition_function: Should return True on success.
24
+ Waits for condition_function to return truthy or raises WaitForTimeout.
25
+ :param (function) condition_function: Should return truthy or
26
+ expected_value on success.
27
+ :param (function) get_message: Optional failure message function
28
+ :param expected_value: Optional return value to wait for. If omitted,
29
+ success is any truthy value.
30
+ :param (float) timeout: max seconds to wait. Defaults to 5
27
31
:param args: Optional args to pass to condition_function.
28
32
:param kwargs: Optional kwargs to pass to condition_function.
29
33
if `timeout` is in kwargs, it will be used to override TIMEOUT
@@ -48,19 +52,24 @@ def wrapped_condition_function():
48
52
return condition_function (** kwargs )
49
53
return condition_function ()
50
54
51
- if 'timeout' in kwargs :
52
- timeout = kwargs ['timeout' ]
53
- del kwargs ['timeout' ]
54
- else :
55
- timeout = TIMEOUT
56
-
57
55
start_time = time .time ()
58
56
while time .time () < start_time + timeout :
59
- if wrapped_condition_function ():
57
+ condition_val = wrapped_condition_function ()
58
+ if expected_value is None :
59
+ if condition_val :
60
+ return True
61
+ elif condition_val == expected_value :
60
62
return True
61
63
time .sleep (0.5 )
62
64
63
- raise WaitForTimeout (get_message ())
65
+ if get_message :
66
+ message = get_message ()
67
+ elif expected_value :
68
+ message = 'Final value: {}' .format (condition_val )
69
+ else :
70
+ message = ''
71
+
72
+ raise WaitForTimeout (message )
64
73
65
74
66
75
def assert_clean_console (TestClass ):
0 commit comments