@@ -81,6 +81,7 @@ def __init__(self, *args, **kwargs):
81
81
self .__called_setup = False
82
82
self .__called_teardown = False
83
83
self .__start_time_ms = None
84
+ self .__requests_timeout = None
84
85
self .__screenshot_count = 0
85
86
self .__will_be_skipped = False
86
87
self .__passed_then_skipped = False
@@ -3208,10 +3209,15 @@ def get_unique_links(self):
3208
3209
3209
3210
def get_link_status_code (self , link , allow_redirects = False , timeout = 5 ):
3210
3211
"""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.
3212
3214
For a list of available status codes, see:
3213
3215
https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
3214
3216
"""
3217
+ if self .__requests_timeout :
3218
+ timeout = self .__requests_timeout
3219
+ if timeout < 1 :
3220
+ timeout = 1
3215
3221
status_code = page_utils ._get_link_status_code (
3216
3222
link , allow_redirects = allow_redirects , timeout = timeout
3217
3223
)
@@ -3234,9 +3240,10 @@ def __get_link_if_404_error(self, link):
3234
3240
else :
3235
3241
return None
3236
3242
3237
- def assert_no_404_errors (self , multithreaded = True ):
3243
+ def assert_no_404_errors (self , multithreaded = True , timeout = None ):
3238
3244
"""Assert no 404 errors from page links obtained from:
3239
3245
"a"->"href", "img"->"src", "link"->"href", and "script"->"src".
3246
+ Timeout is on a per-link basis using the "requests" library.
3240
3247
(A 404 error represents a broken link on a web page.)
3241
3248
"""
3242
3249
all_links = self .get_unique_links ()
@@ -3248,6 +3255,12 @@ def assert_no_404_errors(self, multithreaded=True):
3248
3255
and "data:" not in link
3249
3256
):
3250
3257
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
3251
3264
broken_links = []
3252
3265
if multithreaded :
3253
3266
from multiprocessing .dummy import Pool as ThreadPool
@@ -3264,6 +3277,7 @@ def assert_no_404_errors(self, multithreaded=True):
3264
3277
for link in links :
3265
3278
if self .__get_link_if_404_error (link ):
3266
3279
broken_links .append (link )
3280
+ self .__requests_timeout = None # Reset the requests.get() timeout
3267
3281
if len (broken_links ) > 0 :
3268
3282
bad_links_str = "\n " .join (broken_links )
3269
3283
if len (broken_links ) == 1 :
0 commit comments