@@ -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 )
@@ -7656,6 +7681,12 @@ def reset_default_timeout(self):
76567681 sb_config._is_timeout_changed = False
76577682 self.__overrided_default_timeouts = False
76587683
7684+ def fail(self, msg=None):
7685+ """Fail immediately, with the given message."""
7686+ unittest.has_exception = True
7687+ super().fail(msg)
7688+ raise self.failureException(msg)
7689+
76597690 def skip(self, reason=""):
76607691 """Mark the test as Skipped."""
76617692 self.__check_scope()
@@ -9806,6 +9837,7 @@ def __get_new_timeout(self, timeout):
98069837
98079838 def __check_scope(self):
98089839 if hasattr(self, "browser"): # self.browser stores the type of browser
9840+ unittest.has_exception = False
98099841 return # All good: setUp() already initialized variables in "self"
98109842 else:
98119843 message = (
@@ -9821,6 +9853,7 @@ def __check_scope(self):
98219853 "\n variables, which are initialized during the setUp() method"
98229854 "\n that runs automatically before all tests called by pytest."
98239855 )
9856+ unittest.has_exception = True
98249857 raise OutOfScopeException(message)
98259858
98269859 ############
@@ -10076,6 +10109,7 @@ def process_deferred_asserts(self, print_only=False):
1007610109 if print_only:
1007710110 print(exception_output)
1007810111 else:
10112+ unittest.has_exception = True
1007910113 raise Exception(exception_output.replace("\\n", "\n"))
1008010114
1008110115 ############
@@ -13868,6 +13902,8 @@ def setUp(self, masterqa_mode=False):
1386813902 # Some actions such as hover-clicking are different on mobile.
1386913903 self.mobile_emulator = False
1387013904
13905+ unittest.has_exception = False
13906+
1387113907 # Configure the test time limit (if used).
1387213908 self.set_time_limit(self.time_limit)
1387313909
@@ -14152,6 +14188,8 @@ def __has_exception(self):
1415214188 has_exception = sys.exc_info()[1] is not None
1415314189 if self.__will_be_skipped and hasattr(self, "_using_sb_fixture"):
1415414190 has_exception = False
14191+ if python3_11_or_newer and unittest.has_exception:
14192+ has_exception = True
1415514193 return has_exception
1415614194
1415714195 def __get_test_id(self):
0 commit comments