Skip to content

Commit 5f278ba

Browse files
authored
Add library argument and keyword to set sleep between wait until loop (#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
1 parent dba8977 commit 5f278ba

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

AppiumLibrary/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class AppiumLibrary(
7979
ROBOT_LIBRARY_SCOPE = 'GLOBAL'
8080
ROBOT_LIBRARY_VERSION = VERSION
8181

82-
def __init__(self, timeout=5, run_on_failure='Capture Page Screenshot'):
82+
def __init__(self, timeout=5, run_on_failure='Capture Page Screenshot', sleep_between_wait_loop=0.2):
8383
"""AppiumLibrary can be imported with optional arguments.
8484
8585
``timeout`` is the default timeout used to wait for all waiting actions.
@@ -92,12 +92,16 @@ def __init__(self, timeout=5, run_on_failure='Capture Page Screenshot'):
9292
Using the value `No Operation` will disable this feature altogether. See
9393
`Register Keyword To Run On Failure` keyword for more information about this
9494
functionality.
95+
96+
``sleep_between_wait_loop`` is the default sleep used to wait between loop in all wait until keywords
9597
9698
Examples:
9799
| Library | AppiumLibrary | 10 | # Sets default timeout to 10 seconds |
98100
| Library | AppiumLibrary | timeout=10 | run_on_failure=No Operation | # Sets default timeout to 10 seconds and does nothing on failure |
101+
| Library | AppiumLibrary | timeout=10 | sleep_between_wait_loop=0.3 | # Sets default timeout to 10 seconds and sleep 300 ms between wait loop |
99102
"""
100103
for base in AppiumLibrary.__bases__:
101104
base.__init__(self)
102105
self.set_appium_timeout(timeout)
103106
self.register_keyword_to_run_on_failure(run_on_failure)
107+
self.set_sleep_between_wait_loop(sleep_between_wait_loop)

AppiumLibrary/keywords/_waiting.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55

66
class _WaitingKeywords(KeywordGroup):
7+
8+
def __init__(self):
9+
self._sleep_between_wait = 0.2
10+
711
def wait_until_element_is_visible(self, locator, timeout=None, error=None):
812
"""Waits until element specified with `locator` is visible.
913
@@ -111,6 +115,22 @@ def check_present():
111115

112116
self._wait_until_no_error(timeout, check_present)
113117

118+
def set_sleep_between_wait_loop(self, seconds=0.2):
119+
"""Sets the sleep in seconds used by wait until loop.
120+
121+
If you use the remote appium server, the default value is not recommended because
122+
it is another 200ms overhead to the network latency and will slow down your test
123+
execution.
124+
"""
125+
old_sleep = self._sleep_between_wait
126+
self._sleep_between_wait = robot.utils.timestr_to_secs(seconds)
127+
return old_sleep
128+
129+
def get_sleep_between_wait_loop(self):
130+
"""Gets the sleep between wait loop in seconds that is used by wait until keywords.
131+
"""
132+
return robot.utils.secs_to_timestr(self._sleep_between_wait)
133+
114134
# Private
115135

116136
def _wait_until(self, timeout, error, function, *args):
@@ -131,7 +151,7 @@ def _wait_until_no_error(self, timeout, wait_func, *args):
131151
if time.time() > maxtime:
132152
self.log_source()
133153
raise AssertionError(timeout_error)
134-
time.sleep(0.2)
154+
time.sleep(self._sleep_between_wait)
135155

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

0 commit comments

Comments
 (0)