Bug 1416284: Have Wait allow expected exceptions when polling. r=maja_zf
authorDavid Burns <dburns@mozilla.com>
Tue, 21 Nov 2017 22:00:08 +0000
changeset 437731 2541054da0cb9f23cfa5fdf3ef08379a4696baad
parent 437730 572a54e7cbb77c8696fc8bd20938e7c2dc4fe86a
child 437732 257de1de1304548c1dcb2c7d8c66a20d26c021e6
push id117
push userfmarier@mozilla.com
push dateTue, 28 Nov 2017 20:17:16 +0000
reviewersmaja_zf
bugs1416284
milestone59.0a1
Bug 1416284: Have Wait allow expected exceptions when polling. r=maja_zf MozReview-Commit-ID: BPHVGOOjUaQ
testing/web-platform/tests/webdriver/tests/support/wait.py
--- a/testing/web-platform/tests/webdriver/tests/support/wait.py
+++ b/testing/web-platform/tests/webdriver/tests/support/wait.py
@@ -1,28 +1,39 @@
+import sys
 import time
 
+
 class TimeoutException(Exception):
     pass
 
 
-def wait(session, condition, message, interval=0.1, timeout=5):
+def wait(session, condition, message,
+         interval=0.1, timeout=5, ignored_exceptions=Exception):
     """ Poll a condition until it's true or the timeout ellapses.
 
     :param session: WebDriver session to use with `condition`
     :param condition: function that accepts a WebDriver session and returns a boolean
     :param message: failure description to display in case the timeout is reached
     :param interval: seconds between each call to `condition`. Default: 0.1
     :param timeout: seconds until we stop polling. Default: 5
+    :param ignored_exceptions: Exceptions that are expected and can be ignored.
+        Default: Exception
     """
 
     start = time.time()
     end = start + timeout
 
     while not (time.time() >= end):
         next_step = time.time() + interval
-        success = condition(session)
+        try:
+            success = condition(session)
+        except ignored_exceptions:
+            last_exc = sys.exc_info()[0]
+            success = False
         next_interval = max(next_step - time.time(), 0)
         if not success:
             time.sleep(next_interval)
             continue
         return success
+
+    print "Last exception encountered was {}".format(last_exc)
     raise TimeoutException("Timed out after %d seconds: %s" % (timeout, message))