Skip to content

Commit 0270cc7

Browse files
authored
Merge pull request #250 from seleniumbase/js-error-checking
Add method/option to check for Javascript errors on page loads
2 parents 17d06c7 + 66b722e commit 0270cc7

File tree

5 files changed

+42
-1
lines changed

5 files changed

+42
-1
lines changed

help_docs/method_summary.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ self.is_downloaded_file_present(file)
174174

175175
self.assert_downloaded_file(file)
176176

177+
self.assert_no_js_errors()
178+
177179
self.get_google_auth_password(totp_key=None)
178180

179181
self.convert_xpath_to_css(xpath)

seleniumbase/fixtures/base_case.py

Lines changed: 26 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.
@@ -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

seleniumbase/plugins/pytest_plugin.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ def pytest_addoption(parser):
125125
help="""Setting this overrides the default time that
126126
messenger notifications remain visible when reaching
127127
assert statements during Demo Mode.""")
128+
parser.addoption('--check_js', action="store_true",
129+
dest='js_checking_on',
130+
default=False,
131+
help="""The option to check for Javascript errors after
132+
every page load.""")
128133
parser.addoption('--ad_block', action="store_true",
129134
dest='ad_block_on',
130135
default=False,

seleniumbase/plugins/selenium_plugin.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class SeleniumBrowser(Plugin):
2424
self.options.demo_sleep -- Selenium action delay in DemoMode (--demo_sleep)
2525
self.options.highlights -- # of highlight animations shown (--highlights)
2626
self.options.message_duration -- Messenger alert time (--message_duration)
27+
self.options.js_checking_on -- option to check for js errors (--check_js)
2728
self.options.ad_block -- the option to block some display ads (--ad_block)
2829
self.options.verify_delay -- delay before MasterQA checks (--verify_delay)
2930
self.options.timeout_multiplier -- increase defaults (--timeout_multiplier)
@@ -100,6 +101,12 @@ def options(self, parser, env):
100101
help="""Setting this overrides the default time that
101102
messenger notifications remain visible when reaching
102103
assert statements during Demo Mode.""")
104+
parser.add_option(
105+
'--check_js', action="store_true",
106+
dest='js_checking_on',
107+
default=False,
108+
help="""The option to check for Javascript errors after
109+
every page load.""")
103110
parser.add_option(
104111
'--ad_block', action="store_true",
105112
dest='ad_block_on',
@@ -137,6 +144,7 @@ def beforeTest(self, test):
137144
test.test.demo_sleep = self.options.demo_sleep
138145
test.test.highlights = self.options.highlights
139146
test.test.message_duration = self.options.message_duration
147+
test.test.js_checking_on = self.options.js_checking_on
140148
test.test.ad_block_on = self.options.ad_block_on
141149
test.test.verify_delay = self.options.verify_delay # MasterQA
142150
test.test.timeout_multiplier = self.options.timeout_multiplier

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
setup(
1919
name='seleniumbase',
20-
version='1.17.7',
20+
version='1.17.8',
2121
description='All-in-One Test Automation Framework',
2222
long_description=long_description,
2323
long_description_content_type='text/markdown',

0 commit comments

Comments
 (0)