Skip to content

Commit 1938f8f

Browse files
committed
Fix assert_no_404_errors()
1 parent dbfb173 commit 1938f8f

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2634,23 +2634,43 @@ def assert_link_status_code_is_not_404(self, link):
26342634
bad_link_str = 'Error: "%s" returned a 404!' % link
26352635
self.assertNotEqual(status_code, "404", bad_link_str)
26362636

2637+
def __get_link_if_404_error(self, link):
2638+
status_code = str(self.get_link_status_code(link))
2639+
if status_code == "404":
2640+
return link
2641+
else:
2642+
return None
2643+
26372644
def assert_no_404_errors(self, multithreaded=True):
26382645
""" Assert no 404 errors from page links obtained from:
2639-
"a"->"href", "img"->"src", "link"->"href", and "script"->"src". """
2646+
"a"->"href", "img"->"src", "link"->"href", and "script"->"src".
2647+
(A 404 error represents a broken link on a web page.) """
26402648
all_links = self.get_unique_links()
26412649
links = []
26422650
for link in all_links:
26432651
if "javascript:" not in link and "mailto:" not in link:
26442652
links.append(link)
2653+
broken_links = []
26452654
if multithreaded:
26462655
from multiprocessing.dummy import Pool as ThreadPool
26472656
pool = ThreadPool(10)
2648-
pool.map(self.assert_link_status_code_is_not_404, links)
2657+
results = pool.map(self.__get_link_if_404_error, links)
26492658
pool.close()
26502659
pool.join()
2660+
for result in results:
2661+
if result:
2662+
broken_links.append(result)
26512663
else:
2664+
broken_links = []
26522665
for link in links:
2653-
self.assert_link_status_code_is_not_404(link)
2666+
if self.__get_link_if_404_error(link):
2667+
broken_links.append(link)
2668+
if len(broken_links) > 0:
2669+
bad_links_str = "\n".join(broken_links)
2670+
if len(broken_links) == 1:
2671+
self.fail("Broken link detected:\n%s" % bad_links_str)
2672+
elif len(broken_links) > 1:
2673+
self.fail("Broken links detected:\n%s" % bad_links_str)
26542674
if self.demo_mode:
26552675
a_t = "ASSERT NO 404 ERRORS"
26562676
if self._language != "English":

0 commit comments

Comments
 (0)