Assert no 404 errors from page links obtained from: "a"->"href", "img"->"src", "link"->"href", and "script"->"src". Timeout is on a per-link basis using the "requests" library. If timeout is None, uses the one set in get_link_status_code(). (That timeout value is curr
(self, multithreaded=True, timeout=None)
| 7615 | return None |
| 7616 | |
| 7617 | def assert_no_404_errors(self, multithreaded=True, timeout=None): |
| 7618 | """Assert no 404 errors from page links obtained from: |
| 7619 | "a"->"href", "img"->"src", "link"->"href", and "script"->"src". |
| 7620 | Timeout is on a per-link basis using the "requests" library. |
| 7621 | If timeout is None, uses the one set in get_link_status_code(). |
| 7622 | (That timeout value is currently set to 5 seconds per link.) |
| 7623 | (A 404 error represents a broken link on a web page.)""" |
| 7624 | all_links = self.get_unique_links() |
| 7625 | links = [] |
| 7626 | for link in all_links: |
| 7627 | if ( |
| 7628 | "data:" not in link |
| 7629 | and "tel:" not in link |
| 7630 | and "mailto:" not in link |
| 7631 | and "javascript:" not in link |
| 7632 | and "://fonts.gstatic.com" not in link |
| 7633 | and "://fonts.googleapis.com" not in link |
| 7634 | and "://googleads.g.doubleclick.net" not in link |
| 7635 | ): |
| 7636 | links.append(link) |
| 7637 | if timeout: |
| 7638 | if not isinstance(timeout, (int, float)): |
| 7639 | raise Exception('Expecting a numeric value for "timeout"!') |
| 7640 | if timeout < 0: |
| 7641 | raise Exception('The "timeout" cannot be a negative number!') |
| 7642 | self.__requests_timeout = timeout |
| 7643 | broken_links = [] |
| 7644 | if multithreaded: |
| 7645 | from multiprocessing.dummy import Pool as ThreadPool |
| 7646 | |
| 7647 | pool = ThreadPool(10) |
| 7648 | results = pool.map(self.__get_link_if_404_error, links) |
| 7649 | pool.close() |
| 7650 | pool.join() |
| 7651 | for result in results: |
| 7652 | if result: |
| 7653 | broken_links.append(result) |
| 7654 | else: |
| 7655 | broken_links = [] |
| 7656 | for link in links: |
| 7657 | if self.__get_link_if_404_error(link): |
| 7658 | broken_links.append(link) |
| 7659 | self.__requests_timeout = None # Reset the requests.head() timeout |
| 7660 | if len(broken_links) > 0: |
| 7661 | broken_links = sorted(broken_links) |
| 7662 | bad_links_str = "\n".join(broken_links) |
| 7663 | if len(broken_links) == 1: |
| 7664 | self.fail("Broken link detected:\n%s" % bad_links_str) |
| 7665 | elif len(broken_links) > 1: |
| 7666 | self.fail("Broken links detected:\n%s" % bad_links_str) |
| 7667 | if self.demo_mode: |
| 7668 | a_t = "ASSERT NO 404 ERRORS" |
| 7669 | if self._language != "English": |
| 7670 | from seleniumbase.fixtures.words import SD |
| 7671 | |
| 7672 | a_t = SD.translate_assert_no_404_errors(self._language) |
| 7673 | messenger_post = "<b>%s</b>" % a_t |
| 7674 | self.__highlight_with_assert_success(messenger_post, "html") |