@@ -6521,15 +6521,31 @@ def assert_title_contains(self, substring):
6521
6521
self.__extra_actions.append(action)
6522
6522
return True
6523
6523
6524
- def assert_no_js_errors(self):
6525
- """Asserts that there are no JavaScript "SEVERE"-level page errors.
6524
+ def assert_no_js_errors(self, exclude=[] ):
6525
+ """Asserts current URL has no "SEVERE"-level JavaScript errors.
6526
6526
Works ONLY on Chromium browsers (Chrome or Edge).
6527
6527
Does NOT work on Firefox, IE, Safari, or some other browsers:
6528
6528
* See https://github.com/SeleniumHQ/selenium/issues/1161
6529
6529
Based on the following Stack Overflow solution:
6530
6530
* https://stackoverflow.com/a/41150512/7058266
6531
+ @Params
6532
+ exclude -->
6533
+ A list of substrings or a single comma-separated string of
6534
+ substrings for filtering out error URLs that contain them.
6535
+ URLs that contain any excluded substring will get excluded
6536
+ from the final errors list that's used with the assertion.
6537
+ Examples:
6538
+ self.assert_no_js_errors()
6539
+ self.assert_no_js_errors(exclude=["/api.", "/analytics."])
6540
+ self.assert_no_js_errors(exclude="//api.go,/analytics.go")
6531
6541
"""
6532
6542
self.__check_scope()
6543
+ if (
6544
+ exclude
6545
+ and not type(exclude) is list
6546
+ and not type(exclude) is tuple
6547
+ ):
6548
+ exclude = str(exclude).replace(" ", "").split(",")
6533
6549
time.sleep(0.1) # May take a moment for errors to appear after loads.
6534
6550
try:
6535
6551
browser_logs = self.driver.get_log("browser")
@@ -6546,7 +6562,29 @@ def assert_no_js_errors(self):
6546
6562
and underscore_library not in entry["message"]
6547
6563
):
6548
6564
# Add errors if not caused by SeleniumBase dependencies
6549
- errors.append(entry)
6565
+ if not exclude:
6566
+ errors.append(entry)
6567
+ else:
6568
+ found = False
6569
+ message = entry["message"]
6570
+ if message.count(" - Failed to load resource") == 1:
6571
+ message = message.split(
6572
+ " - Failed to load resource"
6573
+ )[0]
6574
+ elif message.count(" Uncaught TypeError: ") == 1:
6575
+ message = message.split(
6576
+ " Uncaught TypeError: "
6577
+ )[0]
6578
+ for substring in exclude:
6579
+ substring = str(substring)
6580
+ if (
6581
+ len(substring) > 0
6582
+ and substring in message
6583
+ ):
6584
+ found = True
6585
+ break
6586
+ if not found:
6587
+ errors.append(entry)
6550
6588
if len(errors) > 0:
6551
6589
for n in range(len(errors)):
6552
6590
f_t_l_r = " - Failed to load resource"
0 commit comments