Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

Commit

Permalink
Improved the keep_trying function
Browse files Browse the repository at this point in the history
  • Loading branch information
Chiedo committed May 1, 2015
1 parent 62d9423 commit 602c972
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions tests/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,35 @@ def failed(self, error_message):
self.driver.quit()
exit()

def keep_trying(self, function, attempts=60):
def keep_trying(self, function, attempts=60, fallback=None, unsatisfactory=None):
"""Continues to try the function without errors for a number of attempts before continuing. This solves
The problem of Selenium being inconsistent and erroring out because a browser is slow. Don't use if
you are expecting the function to return None.
The problem of Selenium being inconsistent and erroring out because a browser is slow.
Parameters
----------
function : lambda
assertion : lambda
A lambda function that should at some point execute successfully.
attempts : Integer
The number of attempts to keep trying before letting the test continue
unsatisfactory : Any
Value that is unsatisfactory as a return value
fallback : Any
The fallback return value if the function did return a satisfactory value within the given
number of attempts.
Returns the return value of the function we are trying.
"""
for i in xrange(attempts):
try:
result = function()
if(result is not None): return result # It will only return if the assertion does not throw an error
# It will only return if the assertion does not throw an error
if(result is not unsatisfactory): return result
except:
pass
time.sleep(1) # This makes the function wait a second between attempts

return fallback

def passed(self):
"""Print a generic message when a test has passed
"""
Expand Down

0 comments on commit 602c972

Please sign in to comment.