forked from robotframework/SeleniumLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwaiting.py
More file actions
281 lines (228 loc) · 11.1 KB
/
waiting.py
File metadata and controls
281 lines (228 loc) · 11.1 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# Copyright 2008-2011 Nokia Networks
# Copyright 2011-2016 Ryan Tomac, Ed Manlove and contributors
# Copyright 2016- Robot Framework Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import time
from selenium.common.exceptions import StaleElementReferenceException
from SeleniumLibrary.base import LibraryComponent, keyword
from SeleniumLibrary.errors import ElementNotFound
from SeleniumLibrary.utils import is_noney, secs_to_timestr
class WaitingKeywords(LibraryComponent):
@keyword
def wait_for_condition(self, condition, timeout=None, error=None):
"""Waits until ``condition`` is true or ``timeout`` expires.
The condition can be arbitrary JavaScript expression but it
must return a value to be evaluated. See `Execute JavaScript` for
information about accessing content on pages.
Fails if the timeout expires before the condition becomes true. See
the `Timeouts` section for more information about using timeouts
and their default value.
``error`` can be used to override the default error message.
Examples:
| `Wait For Condition` | return document.title == "New Title" |
| `Wait For Condition` | return jQuery.active == 0 |
| `Wait For Condition` | style = document.querySelector('h1').style; return style.background == "red" && style.color == "white" |
"""
if 'return' not in condition:
raise ValueError("Condition '%s' did not have mandatory 'return'."
% condition)
self._wait_until(
lambda: self.driver.execute_script(condition) is True,
"Condition '%s' did not become true in <TIMEOUT>." % condition,
timeout, error
)
@keyword
def wait_until_location_is(self, expected, timeout=None, message=None):
"""Wait until that current URL is ``expected``.
The ``expected`` argument is the expected value in url.
Fails if ``timeout`` expires before the location is. See
the `Timeouts` section for more information about using timeouts
and their default value.
The ``message`` argument can be used to override the default error
message.
New in SeleniumLibrary 4.0
"""
expected = str(expected)
self._wait_until(lambda: expected == self.driver.current_url,
"Location did not is '%s' in <TIMEOUT>." % expected,
timeout, message)
@keyword
def wait_until_location_contains(self, expected, timeout=None, message=None):
"""Wait until that current URL contains ``expected``.
The ``expected`` argument contains the expected value in url.
Fails if ``timeout`` expires before the location contains. See
the `Timeouts` section for more information about using timeouts
and their default value.
The ``message`` argument can be used to override the default error
message.
New in SeleniumLibrary 4.0
"""
expected = str(expected)
self._wait_until(lambda: expected in self.driver.current_url,
"Location did not contain '%s' in <TIMEOUT>." % expected,
timeout, message)
@keyword
def wait_until_page_contains(self, text, timeout=None, error=None):
"""Waits until ``text`` appears on current page.
Fails if ``timeout`` expires before the text appears. See
the `Timeouts` section for more information about using timeouts
and their default value.
``error`` can be used to override the default error message.
"""
self._wait_until(lambda: self.is_text_present(text),
"Text '%s' did not appear in <TIMEOUT>." % text,
timeout, error)
@keyword
def wait_until_page_does_not_contain(self, text, timeout=None,
error=None):
"""Waits until ``text`` disappears from current page.
Fails if ``timeout`` expires before the text disappears. See
the `Timeouts` section for more information about using timeouts
and their default value.
``error`` can be used to override the default error message.
"""
self._wait_until(lambda: not self.is_text_present(text),
"Text '%s' did not disappear in <TIMEOUT>." % text,
timeout, error)
@keyword
def wait_until_page_contains_element(self, locator, timeout=None,
error=None):
"""Waits until element ``locator`` appears on current page.
Fails if ``timeout`` expires before the element appears. See
the `Timeouts` section for more information about using timeouts and
their default value and the `Locating elements` section for details
about the locator syntax.
``error`` can be used to override the default error message.
"""
self._wait_until(
lambda: self.find_element(locator, required=False) is not None,
"Element '%s' did not appear in <TIMEOUT>." % locator,
timeout, error
)
@keyword
def wait_until_page_does_not_contain_element(self, locator, timeout=None,
error=None):
"""Waits until element ``locator`` disappears from current page.
Fails if ``timeout`` expires before the element disappears. See
the `Timeouts` section for more information about using timeouts and
their default value and the `Locating elements` section for details
about the locator syntax.
``error`` can be used to override the default error message.
"""
self._wait_until(
lambda: self.find_element(locator, required=False) is None,
"Element '%s' did not disappear in <TIMEOUT>." % locator,
timeout, error
)
@keyword
def wait_until_element_is_visible(self, locator, timeout=None,
error=None):
"""Waits until element ``locator`` is visible.
Fails if ``timeout`` expires before the element is visible. See
the `Timeouts` section for more information about using timeouts and
their default value and the `Locating elements` section for details
about the locator syntax.
``error`` can be used to override the default error message.
"""
self._wait_until(
lambda: self.is_visible(locator),
"Element '%s' not visible after <TIMEOUT>." % locator,
timeout, error
)
@keyword
def wait_until_element_is_not_visible(self, locator, timeout=None,
error=None):
"""Waits until element ``locator`` is not visible.
Fails if ``timeout`` expires before the element is not visible. See
the `Timeouts` section for more information about using timeouts and
their default value and the `Locating elements` section for details
about the locator syntax.
``error`` can be used to override the default error message.
"""
self._wait_until(
lambda: not self.is_visible(locator),
"Element '%s' still visible after <TIMEOUT>." % locator,
timeout, error
)
@keyword
def wait_until_element_is_enabled(self, locator, timeout=None,
error=None):
"""Waits until element ``locator`` is enabled.
Element is considered enabled if it is not disabled nor read-only.
Fails if ``timeout`` expires before the element is enabled. See
the `Timeouts` section for more information about using timeouts and
their default value and the `Locating elements` section for details
about the locator syntax.
``error`` can be used to override the default error message.
Considering read-only elements to be disabled is a new feature
in SeleniumLibrary 3.0.
"""
self._wait_until(
lambda: self.is_element_enabled(locator),
"Element '%s' was not enabled in <TIMEOUT>." % locator,
timeout, error
)
@keyword
def wait_until_element_contains(self, locator, text, timeout=None,
error=None):
"""Waits until element ``locator`` contains ``text``.
Fails if ``timeout`` expires before the text appears. See
the `Timeouts` section for more information about using timeouts and
their default value and the `Locating elements` section for details
about the locator syntax.
``error`` can be used to override the default error message.
"""
self._wait_until(
lambda: text in self.find_element(locator).text,
"Element '%s' did not get text '%s' in <TIMEOUT>." % (locator, text),
timeout, error
)
@keyword
def wait_until_element_does_not_contain(self, locator, text, timeout=None,
error=None):
"""Waits until element ``locator`` does not contain ``text``.
Fails if ``timeout`` expires before the text disappears. See
the `Timeouts` section for more information about using timeouts and
their default value and the `Locating elements` section for details
about the locator syntax.
``error`` can be used to override the default error message.
"""
self._wait_until(
lambda: text not in self.find_element(locator).text,
"Element '%s' still had text '%s' after <TIMEOUT>." % (locator, text),
timeout, error
)
def _wait_until(self, condition, error, timeout=None, custom_error=None):
timeout = self.get_timeout(timeout)
if is_noney(custom_error):
error = error.replace('<TIMEOUT>', secs_to_timestr(timeout))
else:
error = custom_error
self._wait_until_worker(condition, timeout, error)
def _wait_until_worker(self, condition, timeout, error):
max_time = time.time() + timeout
not_found = None
while time.time() < max_time:
try:
if condition():
return
except ElementNotFound as err:
not_found = str(err)
except StaleElementReferenceException as err:
self.info('Suppressing StaleElementReferenceException from Selenium.')
not_found = err
else:
not_found = None
time.sleep(0.2)
raise AssertionError(not_found or error)