2013-12-17 15:51:18 +00:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
import collections
|
|
|
|
import errors
|
2014-01-22 17:24:49 +00:00
|
|
|
import sys
|
2013-12-17 15:51:18 +00:00
|
|
|
import time
|
|
|
|
|
|
|
|
DEFAULT_TIMEOUT = 5
|
|
|
|
DEFAULT_INTERVAL = 0.1
|
|
|
|
|
|
|
|
class Wait(object):
|
|
|
|
"""An explicit conditional utility class for waiting until a condition
|
|
|
|
evaluates to true or not null.
|
|
|
|
|
|
|
|
This will repeatedly evaluate a condition in anticipation for a
|
|
|
|
truthy return value, or its timeout to expire, or its waiting
|
|
|
|
predicate to become true.
|
2014-01-27 20:40:51 +00:00
|
|
|
|
2014-01-10 13:48:56 +00:00
|
|
|
A `Wait` instance defines the maximum amount of time to wait for a
|
2013-12-17 15:51:18 +00:00
|
|
|
condition, as well as the frequency with which to check the
|
|
|
|
condition. Furthermore, the user may configure the wait to ignore
|
|
|
|
specific types of exceptions whilst waiting, such as
|
2014-01-10 13:48:56 +00:00
|
|
|
`errors.NoSuchElementException` when searching for an element on
|
|
|
|
the page.
|
2014-01-27 20:40:51 +00:00
|
|
|
|
2013-12-17 15:51:18 +00:00
|
|
|
"""
|
2014-01-27 20:40:51 +00:00
|
|
|
|
2014-01-10 13:48:56 +00:00
|
|
|
def __init__(self, marionette, timeout=None,
|
2013-12-17 15:51:18 +00:00
|
|
|
interval=DEFAULT_INTERVAL, ignored_exceptions=None,
|
|
|
|
clock=None):
|
|
|
|
"""Configure the Wait instance to have a custom timeout, interval, and
|
|
|
|
list of ignored exceptions. Optionally a different time
|
|
|
|
implementation than the one provided by the standard library
|
|
|
|
(time) can also be provided.
|
|
|
|
|
2014-10-10 13:11:00 +00:00
|
|
|
Sample usage::
|
2013-12-17 15:51:18 +00:00
|
|
|
|
|
|
|
# Wait 30 seconds for window to open, checking for its presence once
|
|
|
|
# every 5 seconds.
|
|
|
|
wait = Wait(marionette, timeout=30, interval=5,
|
|
|
|
ignored_exceptions=errors.NoSuchWindowException)
|
2014-01-08 21:06:05 +00:00
|
|
|
window = wait.until(lambda m: m.switch_to_window(42))
|
2013-12-17 15:51:18 +00:00
|
|
|
|
|
|
|
:param marionette: The input value to be provided to
|
|
|
|
conditions, usually a Marionette instance.
|
2014-01-27 20:40:51 +00:00
|
|
|
|
2013-12-17 15:51:18 +00:00
|
|
|
:param timeout: How long to wait for the evaluated condition
|
2014-01-10 13:48:56 +00:00
|
|
|
to become true. The default timeout is the `timeout`
|
|
|
|
property on the `Marionette` object if set, or
|
|
|
|
`wait.DEFAULT_TIMEOUT`.
|
2014-01-27 20:40:51 +00:00
|
|
|
|
2013-12-17 15:51:18 +00:00
|
|
|
:param interval: How often the condition should be evaluated.
|
|
|
|
In reality the interval may be greater as the cost of
|
|
|
|
evaluating the condition function is not factored in. The
|
2014-01-10 13:48:56 +00:00
|
|
|
default polling interval is `wait.DEFAULT_INTERVAL`.
|
2014-01-27 20:40:51 +00:00
|
|
|
|
2013-12-17 15:51:18 +00:00
|
|
|
:param ignored_exceptions: Ignore specific types of exceptions
|
|
|
|
whilst waiting for the condition. Any exceptions not
|
|
|
|
whitelisted will be allowed to propagate, terminating the
|
|
|
|
wait.
|
2014-01-27 20:40:51 +00:00
|
|
|
|
2013-12-17 15:51:18 +00:00
|
|
|
:param clock: Allows overriding the use of the runtime's
|
2014-01-10 13:48:56 +00:00
|
|
|
default time library. See `wait.SystemClock` for
|
2013-12-17 15:51:18 +00:00
|
|
|
implementation details.
|
2014-01-27 20:40:51 +00:00
|
|
|
|
2013-12-17 15:51:18 +00:00
|
|
|
"""
|
2014-01-27 20:40:51 +00:00
|
|
|
|
2013-12-17 15:51:18 +00:00
|
|
|
self.marionette = marionette
|
2014-01-10 13:48:56 +00:00
|
|
|
self.timeout = timeout or (self.marionette.timeout and
|
|
|
|
self.marionette.timeout / 1000.0) or DEFAULT_TIMEOUT
|
2013-12-17 15:51:18 +00:00
|
|
|
self.clock = clock or SystemClock()
|
|
|
|
self.end = self.clock.now + self.timeout
|
|
|
|
self.interval = interval
|
|
|
|
|
|
|
|
exceptions = []
|
|
|
|
if ignored_exceptions is not None:
|
|
|
|
if isinstance(ignored_exceptions, collections.Iterable):
|
|
|
|
exceptions.extend(iter(ignored_exceptions))
|
|
|
|
else:
|
|
|
|
exceptions.append(ignored_exceptions)
|
|
|
|
self.exceptions = tuple(set(exceptions))
|
|
|
|
|
2014-01-27 20:40:51 +00:00
|
|
|
def until(self, condition, is_true=None, message=""):
|
2013-12-17 15:51:18 +00:00
|
|
|
"""Repeatedly runs condition until its return value evaluates to true,
|
|
|
|
or its timeout expires or the predicate evaluates to true.
|
|
|
|
|
|
|
|
This will poll at the given interval until the given timeout
|
|
|
|
is reached, or the predicate or conditions returns true. A
|
|
|
|
condition that returns null or does not evaluate to true will
|
|
|
|
fully elapse its timeout before raising an
|
2014-01-10 13:48:56 +00:00
|
|
|
`errors.TimeoutException`.
|
2014-01-27 20:40:51 +00:00
|
|
|
|
2013-12-17 15:51:18 +00:00
|
|
|
If an exception is raised in the condition function and it's
|
|
|
|
not ignored, this function will raise immediately. If the
|
|
|
|
exception is ignored, it will continue polling for the
|
2014-01-10 13:48:56 +00:00
|
|
|
condition until it returns successfully or a
|
|
|
|
`TimeoutException` is raised.
|
2014-01-27 20:40:51 +00:00
|
|
|
|
2013-12-17 15:51:18 +00:00
|
|
|
:param condition: A callable function whose return value will
|
|
|
|
be returned by this function if it evaluates to true.
|
|
|
|
|
2014-01-27 20:40:51 +00:00
|
|
|
:param is_true: An optional predicate that will terminate and
|
|
|
|
return when it evaluates to False. It should be a
|
|
|
|
function that will be passed clock and an end time. The
|
|
|
|
default predicate will terminate a wait when the clock
|
|
|
|
elapses the timeout.
|
|
|
|
|
|
|
|
:param message: An optional message to include in the
|
|
|
|
exception's message if this function times out.
|
2013-12-17 15:51:18 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
rv = None
|
|
|
|
last_exc = None
|
|
|
|
until = is_true or until_pred
|
2014-01-08 21:06:05 +00:00
|
|
|
start = self.clock.now
|
2013-12-17 15:51:18 +00:00
|
|
|
|
|
|
|
while not until(self.clock, self.end):
|
|
|
|
try:
|
|
|
|
rv = condition(self.marionette)
|
|
|
|
except (KeyboardInterrupt, SystemExit) as e:
|
|
|
|
raise e
|
|
|
|
except self.exceptions as e:
|
2014-01-22 17:24:49 +00:00
|
|
|
last_exc = sys.exc_info()
|
2013-12-17 15:51:18 +00:00
|
|
|
|
2014-11-24 04:44:00 +00:00
|
|
|
if not rv:
|
2013-12-17 15:51:18 +00:00
|
|
|
self.clock.sleep(self.interval)
|
|
|
|
continue
|
|
|
|
|
|
|
|
if rv is not None:
|
|
|
|
return rv
|
|
|
|
|
|
|
|
self.clock.sleep(self.interval)
|
|
|
|
|
2014-01-27 20:40:51 +00:00
|
|
|
if message:
|
|
|
|
message = " with message: %s" % message
|
|
|
|
|
2014-01-08 21:06:05 +00:00
|
|
|
raise errors.TimeoutException(
|
2014-01-27 20:40:51 +00:00
|
|
|
"Timed out after %s seconds%s" %
|
2014-02-28 21:41:03 +00:00
|
|
|
(round((self.clock.now - start), 1), message if message else ""),
|
2014-01-22 17:24:49 +00:00
|
|
|
cause=last_exc)
|
2013-12-17 15:51:18 +00:00
|
|
|
|
|
|
|
def until_pred(clock, end):
|
|
|
|
return clock.now >= end
|
|
|
|
|
|
|
|
class SystemClock(object):
|
|
|
|
def __init__(self):
|
|
|
|
self._time = time
|
|
|
|
|
|
|
|
def sleep(self, duration):
|
|
|
|
self._time.sleep(duration)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def now(self):
|
|
|
|
return self._time.time()
|