Skip to content

Commit 893e226

Browse files
committed
Add multithreaded link-checking to save time
1 parent c1adb67 commit 893e226

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,14 +1640,24 @@ def get_link_status_code(self, link, allow_redirects=False, timeout=5):
16401640
link, allow_redirects=allow_redirects, timeout=timeout)
16411641
return status_code
16421642

1643-
def assert_no_404_errors(self):
1643+
def assert_link_status_code_is_not_404(self, link):
1644+
status_code = str(self.get_link_status_code(link))
1645+
bad_link_str = 'Error: "%s" returned a 404!' % link
1646+
self.assert_not_equal(status_code, "404", bad_link_str)
1647+
1648+
def assert_no_404_errors(self, multithreaded=True):
16441649
""" Assert no 404 errors from page links obtained from:
16451650
"a"->"href", "img"->"src", "link"->"href", and "script"->"src". """
16461651
links = self.get_unique_links()
1647-
for link in links:
1648-
status_code = str(self.get_link_status_code(link))
1649-
bad_link_str = 'Error: "%s" returned a 404!' % link
1650-
self.assert_not_equal(status_code, "404", bad_link_str)
1652+
if multithreaded:
1653+
from multiprocessing.dummy import Pool as ThreadPool
1654+
pool = ThreadPool(10)
1655+
pool.map(self.assert_link_status_code_is_not_404, links)
1656+
pool.close()
1657+
pool.join()
1658+
else:
1659+
for link in links:
1660+
self.assert_link_status_code_is_not_404(link)
16511661

16521662
def print_unique_links_with_status_codes(self):
16531663
""" Finds all unique links in the html of the page source

0 commit comments

Comments
 (0)