Skip to content

Commit

Permalink
Add library argument and keyword to set sleep between wait until loop (
Browse files Browse the repository at this point in the history
…#313)

* Make sleep between wait loop configurable via keyword

* add get sleep_between_wait keyword

* rename keyword

* add sleep_between_wait_loop argument in library import
  • Loading branch information
idxn authored Dec 27, 2022
1 parent dba8977 commit 5f278ba
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
6 changes: 5 additions & 1 deletion AppiumLibrary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class AppiumLibrary(
ROBOT_LIBRARY_SCOPE = 'GLOBAL'
ROBOT_LIBRARY_VERSION = VERSION

def __init__(self, timeout=5, run_on_failure='Capture Page Screenshot'):
def __init__(self, timeout=5, run_on_failure='Capture Page Screenshot', sleep_between_wait_loop=0.2):
"""AppiumLibrary can be imported with optional arguments.
``timeout`` is the default timeout used to wait for all waiting actions.
Expand All @@ -92,12 +92,16 @@ def __init__(self, timeout=5, run_on_failure='Capture Page Screenshot'):
Using the value `No Operation` will disable this feature altogether. See
`Register Keyword To Run On Failure` keyword for more information about this
functionality.
``sleep_between_wait_loop`` is the default sleep used to wait between loop in all wait until keywords
Examples:
| Library | AppiumLibrary | 10 | # Sets default timeout to 10 seconds |
| Library | AppiumLibrary | timeout=10 | run_on_failure=No Operation | # Sets default timeout to 10 seconds and does nothing on failure |
| Library | AppiumLibrary | timeout=10 | sleep_between_wait_loop=0.3 | # Sets default timeout to 10 seconds and sleep 300 ms between wait loop |
"""
for base in AppiumLibrary.__bases__:
base.__init__(self)
self.set_appium_timeout(timeout)
self.register_keyword_to_run_on_failure(run_on_failure)
self.set_sleep_between_wait_loop(sleep_between_wait_loop)
22 changes: 21 additions & 1 deletion AppiumLibrary/keywords/_waiting.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@


class _WaitingKeywords(KeywordGroup):

def __init__(self):
self._sleep_between_wait = 0.2

def wait_until_element_is_visible(self, locator, timeout=None, error=None):
"""Waits until element specified with `locator` is visible.
Expand Down Expand Up @@ -111,6 +115,22 @@ def check_present():

self._wait_until_no_error(timeout, check_present)

def set_sleep_between_wait_loop(self, seconds=0.2):
"""Sets the sleep in seconds used by wait until loop.
If you use the remote appium server, the default value is not recommended because
it is another 200ms overhead to the network latency and will slow down your test
execution.
"""
old_sleep = self._sleep_between_wait
self._sleep_between_wait = robot.utils.timestr_to_secs(seconds)
return old_sleep

def get_sleep_between_wait_loop(self):
"""Gets the sleep between wait loop in seconds that is used by wait until keywords.
"""
return robot.utils.secs_to_timestr(self._sleep_between_wait)

# Private

def _wait_until(self, timeout, error, function, *args):
Expand All @@ -131,7 +151,7 @@ def _wait_until_no_error(self, timeout, wait_func, *args):
if time.time() > maxtime:
self.log_source()
raise AssertionError(timeout_error)
time.sleep(0.2)
time.sleep(self._sleep_between_wait)

def _format_timeout(self, timeout):
timeout = robot.utils.timestr_to_secs(timeout) if timeout is not None else self._timeout_in_secs
Expand Down

0 comments on commit 5f278ba

Please sign in to comment.