Skip to content

Commit c1b355d

Browse files
committed
Fix issue with self.has_exception()
1 parent 1081508 commit c1b355d

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ def open(self, url):
307307
pass # Odd issue where the open did happen. Continue.
308308
else:
309309
raise
310+
unittest.has_exception = False
310311
if (
311312
self.undetectable
312313
or (
@@ -4119,6 +4120,7 @@ def wait_for_ready_state_complete(self, timeout=None):
41194120
and settings.SKIP_JS_WAITS
41204121
):
41214122
time.sleep(0.05)
4123+
unittest.has_exception = False
41224124
return True
41234125

41244126
def wait_for_angularjs(self, timeout=None, **kwargs):
@@ -6360,12 +6362,14 @@ def assert_pdf_text(
63606362
)
63616363
if type(page) is int:
63626364
if text not in pdf_text:
6365+
unittest.has_exception = True
63636366
raise Exception(
63646367
"PDF [%s] is missing expected text [%s] on "
63656368
"page [%s]!" % (pdf, text, page)
63666369
)
63676370
else:
63686371
if text not in pdf_text:
6372+
unittest.has_exception = True
63696373
raise Exception(
63706374
"PDF [%s] is missing expected text [%s]!" % (pdf, text)
63716375
)
@@ -6740,40 +6744,52 @@ def assert_downloaded_file(self, file, timeout=None, browser=False):
67406744
def assert_true(self, expr, msg=None):
67416745
"""Asserts that the expression is True.
67426746
Will raise an exception if the statement if False."""
6747+
unittest.has_exception = True
67436748
self.assertTrue(expr, msg=msg)
6749+
unittest.has_exception = False
67446750

67456751
def assert_false(self, expr, msg=None):
67466752
"""Asserts that the expression is False.
67476753
Will raise an exception if the statement if True."""
6754+
unittest.has_exception = True
67486755
self.assertFalse(expr, msg=msg)
6756+
unittest.has_exception = False
67496757

67506758
def assert_equal(self, first, second, msg=None):
67516759
"""Asserts that the two values are equal.
67526760
Will raise an exception if the values are not equal."""
6761+
unittest.has_exception = True
67536762
self.assertEqual(first, second, msg=msg)
6763+
unittest.has_exception = False
67546764

67556765
def assert_not_equal(self, first, second, msg=None):
67566766
"""Asserts that the two values are not equal.
67576767
Will raise an exception if the values are equal."""
6768+
unittest.has_exception = True
67586769
self.assertNotEqual(first, second, msg=msg)
6770+
unittest.has_exception = False
67596771

67606772
def assert_in(self, first, second, msg=None):
67616773
"""Asserts that the first string is in the second string.
67626774
Will raise an exception if the first string is not in the second."""
6775+
unittest.has_exception = True
67636776
self.assertIn(first, second, msg=msg)
6777+
unittest.has_exception = False
67646778

67656779
def assert_not_in(self, first, second, msg=None):
67666780
"""Asserts that the first string is not in the second string.
67676781
Will raise an exception if the first string is in the second string."""
6782+
unittest.has_exception = True
67686783
self.assertNotIn(first, second, msg=msg)
6784+
unittest.has_exception = False
67696785

67706786
def assert_raises(self, *args, **kwargs):
67716787
"""Asserts that the following block of code raises an exception.
67726788
Will raise an exception if the block of code has no exception.
67736789
Usage Example =>
6774-
# Verify that the expected exception is raised.
6775-
with self.assert_raises(Exception):
6776-
raise Exception("Expected Exception!") """
6790+
# Verify that the expected exception is raised.
6791+
with self.assert_raises(Exception):
6792+
raise Exception("Expected Exception!") """
67776793
return self.assertRaises(*args, **kwargs)
67786794

67796795
def wait_for_attribute(
@@ -6884,9 +6900,11 @@ def assert_title(self, title):
68846900
self.wait_for_ready_state_complete()
68856901
time.sleep(2)
68866902
actual = self.get_page_title().strip()
6903+
unittest.has_exception = True
68876904
self.assertEqual(
68886905
expected, actual, error % (expected, actual)
68896906
)
6907+
unittest.has_exception = False
68906908
if self.demo_mode and not self.recorder_mode:
68916909
a_t = "ASSERT TITLE"
68926910
if self._language != "English":
@@ -6931,9 +6949,11 @@ def assert_title_contains(self, substring):
69316949
self.wait_for_ready_state_complete()
69326950
time.sleep(2)
69336951
actual = self.get_page_title().strip()
6952+
unittest.has_exception = True
69346953
self.assertIn(
69356954
expected, actual, error % (expected, actual)
69366955
)
6956+
unittest.has_exception = False
69376957
if self.demo_mode and not self.recorder_mode:
69386958
a_t = "ASSERT TITLE CONTAINS"
69396959
if self._language != "English":
@@ -6968,7 +6988,9 @@ def assert_url(self, url):
69686988
self.wait_for_ready_state_complete()
69696989
time.sleep(2)
69706990
actual = self.get_current_url().strip()
6991+
unittest.has_exception = True
69716992
self.assertEqual(expected, actual, error % (expected, actual))
6993+
unittest.has_exception = False
69726994
if self.demo_mode and not self.recorder_mode:
69736995
a_u = "ASSERT URL"
69746996
if self._language != "English":
@@ -7006,7 +7028,9 @@ def assert_url_contains(self, substring):
70067028
self.wait_for_ready_state_complete()
70077029
time.sleep(2)
70087030
actual = self.get_current_url().strip()
7031+
unittest.has_exception = True
70097032
self.assertIn(expected, actual, error % (expected, actual))
7033+
unittest.has_exception = False
70107034
if self.demo_mode and not self.recorder_mode:
70117035
a_u = "ASSERT URL CONTAINS"
70127036
if self._language != "English":
@@ -7103,6 +7127,7 @@ def assert_no_js_errors(self, exclude=[]):
71037127
er_str = str(errors)
71047128
er_str = er_str.replace("[{", "[\n{").replace("}, {", "},\n{")
71057129
current_url = self.get_current_url()
7130+
unittest.has_exception = True
71067131
raise Exception(
71077132
"JavaScript errors found on %s => %s" % (current_url, er_str)
71087133
)
@@ -9812,6 +9837,7 @@ def __get_new_timeout(self, timeout):
98129837

98139838
def __check_scope(self):
98149839
if hasattr(self, "browser"): # self.browser stores the type of browser
9840+
unittest.has_exception = False
98159841
return # All good: setUp() already initialized variables in "self"
98169842
else:
98179843
message = (
@@ -9827,6 +9853,7 @@ def __check_scope(self):
98279853
"\n variables, which are initialized during the setUp() method"
98289854
"\n that runs automatically before all tests called by pytest."
98299855
)
9856+
unittest.has_exception = True
98309857
raise OutOfScopeException(message)
98319858

98329859
############
@@ -10082,6 +10109,7 @@ def process_deferred_asserts(self, print_only=False):
1008210109
if print_only:
1008310110
print(exception_output)
1008410111
else:
10112+
unittest.has_exception = True
1008510113
raise Exception(exception_output.replace("\\n", "\n"))
1008610114

1008710115
############
@@ -13874,6 +13902,8 @@ def setUp(self, masterqa_mode=False):
1387413902
# Some actions such as hover-clicking are different on mobile.
1387513903
self.mobile_emulator = False
1387613904

13905+
unittest.has_exception = False
13906+
1387713907
# Configure the test time limit (if used).
1387813908
self.set_time_limit(self.time_limit)
1387913909

@@ -14158,6 +14188,8 @@ def __has_exception(self):
1415814188
has_exception = sys.exc_info()[1] is not None
1415914189
if self.__will_be_skipped and hasattr(self, "_using_sb_fixture"):
1416014190
has_exception = False
14191+
if python3_11_or_newer and unittest.has_exception:
14192+
has_exception = True
1416114193
return has_exception
1416214194

1416314195
def __get_test_id(self):

seleniumbase/fixtures/page_actions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import codecs
2424
import os
2525
import time
26+
import unittest
2627
from selenium.common.exceptions import ElementNotInteractableException
2728
from selenium.common.exceptions import ElementNotVisibleException
2829
from selenium.common.exceptions import NoAlertPresentException
@@ -224,6 +225,7 @@ def hover_element(driver, element):
224225

225226

226227
def timeout_exception(exception, message):
228+
unittest.has_exception = True
227229
exc, msg = shared_utils.format_exc(exception, message)
228230
raise exc(msg)
229231

0 commit comments

Comments
 (0)