@@ -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.
@@ -2127,6 +2150,8 @@ def wait_for_ready_state_complete(self, timeout=settings.EXTREME_TIMEOUT):
21272150 timeout = self .__get_new_timeout (timeout )
21282151 is_ready = js_utils .wait_for_ready_state_complete (self .driver , timeout )
21292152 self .wait_for_angularjs (timeout = settings .MINI_TIMEOUT )
2153+ if self .js_checking_on :
2154+ self .assert_no_js_errors ()
21302155 if self .ad_block_on :
21312156 # If the ad_block feature is enabled, then block ads for new URLs
21322157 current_url = self .get_current_url ()
@@ -2640,6 +2665,7 @@ def setUp(self):
26402665 self .demo_sleep = pytest .config .option .demo_sleep
26412666 self .highlights = pytest .config .option .highlights
26422667 self .message_duration = pytest .config .option .message_duration
2668+ self .js_checking_on = pytest .config .option .js_checking_on
26432669 self .ad_block_on = pytest .config .option .ad_block_on
26442670 self .verify_delay = pytest .config .option .verify_delay
26452671 self .timeout_multiplier = pytest .config .option .timeout_multiplier
0 commit comments