Skip to content

Commit e0d1062

Browse files
committed
Add custom timeout option for self.assert_no_404_errors()
1 parent 85ce552 commit e0d1062

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ def __init__(self, *args, **kwargs):
8181
self.__called_setup = False
8282
self.__called_teardown = False
8383
self.__start_time_ms = None
84+
self.__requests_timeout = None
8485
self.__screenshot_count = 0
8586
self.__will_be_skipped = False
8687
self.__passed_then_skipped = False
@@ -3208,10 +3209,15 @@ def get_unique_links(self):
32083209

32093210
def get_link_status_code(self, link, allow_redirects=False, timeout=5):
32103211
"""Get the status code of a link.
3211-
If the timeout is exceeded, will return a 404.
3212+
If the timeout is set to less than 1, it becomes 1.
3213+
If the timeout is exceeded by requests.get(), it will return a 404.
32123214
For a list of available status codes, see:
32133215
https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
32143216
"""
3217+
if self.__requests_timeout:
3218+
timeout = self.__requests_timeout
3219+
if timeout < 1:
3220+
timeout = 1
32153221
status_code = page_utils._get_link_status_code(
32163222
link, allow_redirects=allow_redirects, timeout=timeout
32173223
)
@@ -3234,9 +3240,10 @@ def __get_link_if_404_error(self, link):
32343240
else:
32353241
return None
32363242

3237-
def assert_no_404_errors(self, multithreaded=True):
3243+
def assert_no_404_errors(self, multithreaded=True, timeout=None):
32383244
"""Assert no 404 errors from page links obtained from:
32393245
"a"->"href", "img"->"src", "link"->"href", and "script"->"src".
3246+
Timeout is on a per-link basis using the "requests" library.
32403247
(A 404 error represents a broken link on a web page.)
32413248
"""
32423249
all_links = self.get_unique_links()
@@ -3248,6 +3255,12 @@ def assert_no_404_errors(self, multithreaded=True):
32483255
and "data:" not in link
32493256
):
32503257
links.append(link)
3258+
if timeout:
3259+
if not type(timeout) is int and not type(timeout) is float:
3260+
raise Exception('Expecting a numeric value for "timeout"!')
3261+
if timeout < 0:
3262+
raise Exception('The "timeout" cannot be a negative number!')
3263+
self.__requests_timeout = timeout
32513264
broken_links = []
32523265
if multithreaded:
32533266
from multiprocessing.dummy import Pool as ThreadPool
@@ -3264,6 +3277,7 @@ def assert_no_404_errors(self, multithreaded=True):
32643277
for link in links:
32653278
if self.__get_link_if_404_error(link):
32663279
broken_links.append(link)
3280+
self.__requests_timeout = None # Reset the requests.get() timeout
32673281
if len(broken_links) > 0:
32683282
bad_links_str = "\n".join(broken_links)
32693283
if len(broken_links) == 1:

0 commit comments

Comments
 (0)