Skip to content

Commit 07308f1

Browse files
committed
Add arg for excluding URLs from assert_no_js_errors()
1 parent 4f20c34 commit 07308f1

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

help_docs/method_summary.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ self.assert_title(title)
460460

461461
self.assert_title_contains(substring)
462462

463-
self.assert_no_js_errors()
463+
self.assert_no_js_errors(exclude=[])
464464

465465
self.inspect_html()
466466

seleniumbase/fixtures/base_case.py

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6521,15 +6521,31 @@ def assert_title_contains(self, substring):
65216521
self.__extra_actions.append(action)
65226522
return True
65236523

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.
65266526
Works ONLY on Chromium browsers (Chrome or Edge).
65276527
Does NOT work on Firefox, IE, Safari, or some other browsers:
65286528
* See https://github.com/SeleniumHQ/selenium/issues/1161
65296529
Based on the following Stack Overflow solution:
65306530
* 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")
65316541
"""
65326542
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(",")
65336549
time.sleep(0.1) # May take a moment for errors to appear after loads.
65346550
try:
65356551
browser_logs = self.driver.get_log("browser")
@@ -6546,7 +6562,29 @@ def assert_no_js_errors(self):
65466562
and underscore_library not in entry["message"]
65476563
):
65486564
# 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)
65506588
if len(errors) > 0:
65516589
for n in range(len(errors)):
65526590
f_t_l_r = " - Failed to load resource"

0 commit comments

Comments
 (0)