Skip to content

Commit 35d563e

Browse files
committed
Add methods for setting and resetting default timeout values
1 parent efcab33 commit 35d563e

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

help_docs/method_summary.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,10 @@ self.jquery_update_text(selector, text, by=By.CSS_SELECTOR, timeout=None)
353353

354354
self.set_time_limit(time_limit)
355355

356+
self.set_default_timeout(timeout)
357+
358+
self.reset_default_timeout()
359+
356360
self.skip(reason="")
357361

358362
############

seleniumbase/fixtures/base_case.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def __init__(self, *args, **kwargs):
7474
self.__last_page_url = None
7575
self.__last_page_source = None
7676
self.__skip_reason = None
77+
self.__overrided_default_timeouts = False
7778
self.__added_pytest_html_extra = None
7879
self.__deferred_assert_count = 0
7980
self.__deferred_assert_failures = []
@@ -3648,6 +3649,46 @@ def set_time_limit(self, time_limit):
36483649
sb_config.time_limit = None
36493650
sb_config.time_limit_ms = None
36503651

3652+
def set_default_timeout(self, timeout):
3653+
""" This method changes the default timeout values of test methods
3654+
for the duration of the current test.
3655+
Effected timeouts: (used by methods that wait for elements)
3656+
* settings.SMALL_TIMEOUT - (default value: 6 seconds)
3657+
* settings.LARGE_TIMEOUT - (default value: 10 seconds)
3658+
The minimum allowable default timeout is: 0.5 seconds.
3659+
The maximum allowable default timeout is: 60.0 seconds.
3660+
(Test methods can still override timeouts outside that range.)
3661+
"""
3662+
self.__check_scope()
3663+
if not type(timeout) is int and not type(timeout) is float:
3664+
raise Exception('Expecting a numeric value for "timeout"!')
3665+
if timeout < 0:
3666+
raise Exception('The "timeout" cannot be a negative number!')
3667+
timeout = float(timeout)
3668+
# Min default timeout: 0.5 seconds. Max default timeout: 60.0 seconds.
3669+
min_timeout = 0.5
3670+
max_timeout = 60.0
3671+
if timeout < min_timeout:
3672+
logging.info("Minimum default timeout = %s" % min_timeout)
3673+
timeout = min_timeout
3674+
elif timeout > max_timeout:
3675+
logging.info("Maximum default timeout = %s" % max_timeout)
3676+
timeout = max_timeout
3677+
self.__overrided_default_timeouts = True
3678+
sb_config._is_timeout_changed = True
3679+
settings.SMALL_TIMEOUT = timeout
3680+
settings.LARGE_TIMEOUT = timeout
3681+
3682+
def reset_default_timeout(self):
3683+
""" Reset default timeout values to the original from settings.py
3684+
This method reverts the changes made by set_default_timeout() """
3685+
if self.__overrided_default_timeouts:
3686+
if sb_config._SMALL_TIMEOUT and sb_config._LARGE_TIMEOUT:
3687+
settings.SMALL_TIMEOUT = sb_config._SMALL_TIMEOUT
3688+
settings.LARGE_TIMEOUT = sb_config._LARGE_TIMEOUT
3689+
sb_config._is_timeout_changed = False
3690+
self.__overrided_default_timeouts = False
3691+
36513692
def skip(self, reason=""):
36523693
""" Mark the test as Skipped. """
36533694
self.__check_scope()
@@ -7383,6 +7424,17 @@ def setUp(self, masterqa_mode=False):
73837424
""" >>> "pip install -r requirements.txt"\n"""
73847425
""" >>> "python setup.py install" """)
73857426

7427+
if not hasattr(sb_config, '_is_timeout_changed'):
7428+
# Should only be reachable from pure Python runs
7429+
sb_config._is_timeout_changed = False
7430+
sb_config._SMALL_TIMEOUT = settings.SMALL_TIMEOUT
7431+
sb_config._LARGE_TIMEOUT = settings.LARGE_TIMEOUT
7432+
7433+
if sb_config._is_timeout_changed:
7434+
if sb_config._SMALL_TIMEOUT and sb_config._LARGE_TIMEOUT:
7435+
settings.SMALL_TIMEOUT = sb_config._SMALL_TIMEOUT
7436+
settings.LARGE_TIMEOUT = sb_config._LARGE_TIMEOUT
7437+
73867438
# Parse the settings file
73877439
if self.settings_file:
73887440
from seleniumbase.core import settings_parser
@@ -8037,6 +8089,14 @@ def tearDown(self):
80378089
# *** Start tearDown() officially ***
80388090
self.__slow_mode_pause_if_active()
80398091
has_exception = self.__has_exception()
8092+
if self.__overrided_default_timeouts:
8093+
# Reset default timeouts in case there are more tests
8094+
# These were changed in set_default_timeout()
8095+
if sb_config._SMALL_TIMEOUT and sb_config._LARGE_TIMEOUT:
8096+
settings.SMALL_TIMEOUT = sb_config._SMALL_TIMEOUT
8097+
settings.LARGE_TIMEOUT = sb_config._LARGE_TIMEOUT
8098+
sb_config._is_timeout_changed = False
8099+
self.__overrided_default_timeouts = False
80408100
if self.__deferred_assert_failures:
80418101
print(
80428102
"\nWhen using self.deferred_assert_*() methods in your tests, "

seleniumbase/plugins/pytest_plugin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,9 @@ def pytest_configure(config):
740740
sb_config.save_screenshot = config.getoption('save_screenshot')
741741
sb_config.visual_baseline = config.getoption('visual_baseline')
742742
sb_config.timeout_multiplier = config.getoption('timeout_multiplier')
743+
sb_config._is_timeout_changed = False
744+
sb_config._SMALL_TIMEOUT = settings.SMALL_TIMEOUT
745+
sb_config._LARGE_TIMEOUT = settings.LARGE_TIMEOUT
743746
sb_config.pytest_html_report = config.getoption('htmlpath') # --html=FILE
744747
sb_config._sb_node = {} # sb node dictionary (Used with the sb fixture)
745748
# Dashboard-specific variables

seleniumbase/plugins/selenium_plugin.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import sys
55
from nose.plugins import Plugin
6+
from seleniumbase import config as sb_config
7+
from seleniumbase.config import settings
68
from seleniumbase.core import proxy_helper
79
from seleniumbase.fixtures import constants
810

@@ -495,6 +497,9 @@ def beforeTest(self, test):
495497
# pyvirtualdisplay might not be necessary anymore because
496498
# Chrome and Firefox now have built-in headless displays
497499
pass
500+
sb_config._is_timeout_changed = False
501+
sb_config._SMALL_TIMEOUT = settings.SMALL_TIMEOUT
502+
sb_config._LARGE_TIMEOUT = settings.LARGE_TIMEOUT
498503
# The driver will be received later
499504
self.driver = None
500505
test.test.driver = self.driver

0 commit comments

Comments
 (0)