Skip to content

Commit 0877608

Browse files
committed
Add method to assert that no Javascript errors exist on the current page
1 parent 17d06c7 commit 0877608

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,6 +1514,29 @@ def assert_downloaded_file(self, file):
15141514
""" Asserts that the file exists in the Downloads Folder. """
15151515
assert os.path.exists(self.get_path_of_downloaded_file(file))
15161516

1517+
def assert_no_js_errors(self):
1518+
""" Asserts that there are no Javascript errors on the page.
1519+
Only looks for "SEVERE"-level errors.
1520+
Works best when using Chrome.
1521+
Does NOT work on Firefox:
1522+
* See https://github.com/SeleniumHQ/selenium/issues/1161
1523+
Based on the following Stack Overflow solution:
1524+
* https://stackoverflow.com/a/41150512/7058266 """
1525+
try:
1526+
browser_logs = self.driver.get_log('browser')
1527+
except (ValueError, WebDriverException):
1528+
# If unable to get browser logs, skip the assert and return.
1529+
return
1530+
1531+
errors = []
1532+
for entry in browser_logs:
1533+
if entry['level'] == 'SEVERE':
1534+
errors.append(entry)
1535+
if len(errors) > 0:
1536+
current_url = self.get_current_url()
1537+
raise Exception(
1538+
"Javascript errors found on %s => %s" % (current_url, errors))
1539+
15171540
def get_google_auth_password(self, totp_key=None):
15181541
""" Returns a time-based one-time password based on the
15191542
Google Authenticator password algorithm. Works with Authy.

0 commit comments

Comments
 (0)