Robot Framework SeleniumLibrary
waiting.py
Go to the documentation of this file.
1 # Copyright 2008-2011 Nokia Networks
2 # Copyright 2011-2016 Ryan Tomac, Ed Manlove and contributors
3 # Copyright 2016- Robot Framework Foundation
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 
17 import time
18 
19 from selenium.common.exceptions import StaleElementReferenceException
20 
21 from SeleniumLibrary.base import LibraryComponent, keyword
22 from SeleniumLibrary.errors import ElementNotFound
23 from SeleniumLibrary.utils import is_noney, secs_to_timestr
24 
25 
27 
28  @keyword
29 
46  def wait_for_condition(self, condition, timeout=None, error=None):
47  if 'return' not in condition:
48  raise ValueError("Condition '%s' did not have mandatory 'return'."
49  % condition)
50  self._wait_until_wait_until(
51  lambda: self.driverdriverdriver.execute_script(condition) is True,
52  "Condition '%s' did not become true in <TIMEOUT>." % condition,
53  timeout, error
54  )
55 
56  @keyword
57 
70  def wait_until_location_is(self, expected, timeout=None, message=None):
71 
72  expected = str(expected)
73  self._wait_until_wait_until(lambda: expected == self.driverdriverdriver.current_url,
74  "Location did not is '%s' in <TIMEOUT>." % expected,
75  timeout, message)
76 
77  @keyword
78 
91  def wait_until_location_contains(self, expected, timeout=None, message=None):
92  expected = str(expected)
93  self._wait_until_wait_until(lambda: expected in self.driverdriverdriver.current_url,
94  "Location did not contain '%s' in <TIMEOUT>." % expected,
95  timeout, message)
96 
97 
98  @keyword
99 
107  def wait_until_page_contains(self, text, timeout=None, error=None):
108  self._wait_until_wait_until(lambda: self.is_text_presentis_text_present(text),
109  "Text '%s' did not appear in <TIMEOUT>." % text,
110  timeout, error)
111 
112  @keyword
113 
121  def wait_until_page_does_not_contain(self, text, timeout=None,
122  error=None):
123  self._wait_until_wait_until(lambda: not self.is_text_presentis_text_present(text),
124  "Text '%s' did not disappear in <TIMEOUT>." % text,
125  timeout, error)
126 
127  @keyword
128 
137  def wait_until_page_contains_element(self, locator, timeout=None,
138  error=None):
139  self._wait_until_wait_until(
140  lambda: self.find_elementfind_element(locator, required=False) is not None,
141  "Element '%s' did not appear in <TIMEOUT>." % locator,
142  timeout, error
143  )
144 
145  @keyword
146 
155  def wait_until_page_does_not_contain_element(self, locator, timeout=None,
156  error=None):
157  self._wait_until_wait_until(
158  lambda: self.find_elementfind_element(locator, required=False) is None,
159  "Element '%s' did not disappear in <TIMEOUT>." % locator,
160  timeout, error
161  )
162 
163  @keyword
164 
173  def wait_until_element_is_visible(self, locator, timeout=None,
174  error=None):
175  self._wait_until_wait_until(
176  lambda: self.is_visibleis_visible(locator),
177  "Element '%s' not visible after <TIMEOUT>." % locator,
178  timeout, error
179  )
180 
181  @keyword
182 
191  def wait_until_element_is_not_visible(self, locator, timeout=None,
192  error=None):
193  self._wait_until_wait_until(
194  lambda: not self.is_visibleis_visible(locator),
195  "Element '%s' still visible after <TIMEOUT>." % locator,
196  timeout, error
197  )
198 
199  @keyword
200 
214  def wait_until_element_is_enabled(self, locator, timeout=None,
215  error=None):
216  self._wait_until_wait_until(
217  lambda: self.is_element_enabledis_element_enabled(locator),
218  "Element '%s' was not enabled in <TIMEOUT>." % locator,
219  timeout, error
220  )
221 
222  @keyword
223 
232  def wait_until_element_contains(self, locator, text, timeout=None,
233  error=None):
234  self._wait_until_wait_until(
235  lambda: text in self.find_elementfind_element(locator).text,
236  "Element '%s' did not get text '%s' in <TIMEOUT>." % (locator, text),
237  timeout, error
238  )
239 
240  @keyword
241 
250  def wait_until_element_does_not_contain(self, locator, text, timeout=None,
251  error=None):
252  self._wait_until_wait_until(
253  lambda: text not in self.find_elementfind_element(locator).text,
254  "Element '%s' still had text '%s' after <TIMEOUT>." % (locator, text),
255  timeout, error
256  )
257 
258  def _wait_until(self, condition, error, timeout=None, custom_error=None):
259  timeout = self.get_timeoutget_timeout(timeout)
260  if is_noney(custom_error):
261  error = error.replace('<TIMEOUT>', secs_to_timestr(timeout))
262  else:
263  error = custom_error
264  self._wait_until_worker_wait_until_worker(condition, timeout, error)
265 
266  def _wait_until_worker(self, condition, timeout, error):
267  max_time = time.time() + timeout
268  not_found = None
269  while time.time() < max_time:
270  try:
271  if condition():
272  return
273  except ElementNotFound as err:
274  not_found = str(err)
275  except StaleElementReferenceException as err:
276  self.infoinfo('Suppressing StaleElementReferenceException from Selenium.')
277  not_found = err
278  else:
279  not_found = None
280  time.sleep(0.2)
281  raise AssertionError(not_found or error)
def find_element(self, locator, tag=None, required=True, parent=None)
Find element matching locator.
Definition: context.py:72
def is_element_enabled(self, locator, tag=None)
Definition: context.py:95
def _wait_until_worker(self, condition, timeout, error)
Definition: waiting.py:266
def _wait_until(self, condition, error, timeout=None, custom_error=None)
Definition: waiting.py:258
def wait_until_element_does_not_contain(self, locator, text, timeout=None, error=None)
Waits until element locator does not contain text.
Definition: waiting.py:251
def wait_until_page_contains_element(self, locator, timeout=None, error=None)
Waits until element locator appears on current page.
Definition: waiting.py:138
def wait_until_element_is_not_visible(self, locator, timeout=None, error=None)
Waits until element locator is not visible.
Definition: waiting.py:192
def wait_until_page_contains(self, text, timeout=None, error=None)
Waits until text appears on current page.
Definition: waiting.py:107
def wait_until_element_is_enabled(self, locator, timeout=None, error=None)
Waits until element locator is enabled.
Definition: waiting.py:215
def wait_until_page_does_not_contain(self, text, timeout=None, error=None)
Waits until text disappears from current page.
Definition: waiting.py:122
def wait_for_condition(self, condition, timeout=None, error=None)
Waits until condition is true or timeout expires.
Definition: waiting.py:46
def wait_until_page_does_not_contain_element(self, locator, timeout=None, error=None)
Waits until element locator disappears from current page.
Definition: waiting.py:156
def wait_until_location_contains(self, expected, timeout=None, message=None)
Wait until that current URL contains expected.
Definition: waiting.py:91
def wait_until_location_is(self, expected, timeout=None, message=None)
Wait until that current URL is expected.
Definition: waiting.py:70
def wait_until_element_contains(self, locator, text, timeout=None, error=None)
Waits until element locator contains text.
Definition: waiting.py:233
def wait_until_element_is_visible(self, locator, timeout=None, error=None)
Waits until element locator is visible.
Definition: waiting.py:174