@@ -209,7 +209,7 @@ def test_example(self):
209209 def open(self, url):
210210 """Navigates the current browser window to the specified page."""
211211 self.__check_scope()
212- self.__check_browser ()
212+ self._check_browser ()
213213 if self.__needs_minimum_wait():
214214 time.sleep(0.03)
215215 if self.undetectable:
@@ -255,7 +255,7 @@ def open(self, url):
255255 or "ERR_NAME_NOT_RESOLVED" in e.msg
256256 ):
257257 shared_utils.check_if_time_limit_exceeded()
258- self.__check_browser ()
258+ self._check_browser ()
259259 time.sleep(0.8)
260260 self.driver.get(url)
261261 elif (
@@ -264,7 +264,7 @@ def open(self, url):
264264 or "ERR_PROXY_CONNECTION_FAILED" in e.msg
265265 ):
266266 shared_utils.check_if_time_limit_exceeded()
267- self.__check_browser ()
267+ self._check_browser ()
268268 time.sleep(1.05)
269269 try:
270270 self.driver.get(url)
@@ -3205,17 +3205,17 @@ def open_html_file(self, html_file):
32053205
32063206 def execute_script(self, script, *args, **kwargs):
32073207 self.__check_scope()
3208- self.__check_browser ()
3208+ self._check_browser ()
32093209 return self.driver.execute_script(script, *args, **kwargs)
32103210
32113211 def execute_cdp_cmd(self, script, *args, **kwargs):
32123212 self.__check_scope()
3213- self.__check_browser ()
3213+ self._check_browser ()
32143214 return self.driver.execute_cdp_cmd(script, *args, **kwargs)
32153215
32163216 def execute_async_script(self, script, timeout=None):
32173217 self.__check_scope()
3218- self.__check_browser ()
3218+ self._check_browser ()
32193219 if not timeout:
32203220 timeout = settings.EXTREME_TIMEOUT
32213221 return js_utils.execute_async_script(self.driver, script, timeout)
@@ -3225,26 +3225,26 @@ def safe_execute_script(self, script, *args, **kwargs):
32253225 it's important that the jQuery library has been loaded first.
32263226 This method will load jQuery if it wasn't already loaded."""
32273227 self.__check_scope()
3228- self.__check_browser ()
3228+ self._check_browser ()
32293229 if not js_utils.is_jquery_activated(self.driver):
32303230 self.activate_jquery()
32313231 return self.driver.execute_script(script, *args, **kwargs)
32323232
32333233 def set_window_rect(self, x, y, width, height):
32343234 self.__check_scope()
3235- self.__check_browser ()
3235+ self._check_browser ()
32363236 self.driver.set_window_rect(x, y, width, height)
32373237 self.__demo_mode_pause_if_active()
32383238
32393239 def set_window_size(self, width, height):
32403240 self.__check_scope()
3241- self.__check_browser ()
3241+ self._check_browser ()
32423242 self.driver.set_window_size(width, height)
32433243 self.__demo_mode_pause_if_active()
32443244
32453245 def maximize_window(self):
32463246 self.__check_scope()
3247- self.__check_browser ()
3247+ self._check_browser ()
32483248 self.driver.maximize_window()
32493249 self.__demo_mode_pause_if_active()
32503250
@@ -4208,7 +4208,7 @@ def wait_for_ready_state_complete(self, timeout=None):
42084208 """Waits for the "readyState" of the page to be "complete".
42094209 Returns True when the method completes."""
42104210 self.__check_scope()
4211- self.__check_browser ()
4211+ self._check_browser ()
42124212 if not timeout:
42134213 timeout = settings.EXTREME_TIMEOUT
42144214 if self.timeout_multiplier and timeout == settings.EXTREME_TIMEOUT:
@@ -6191,7 +6191,7 @@ def disable_beforeunload(self):
61916191 on Chromium browsers (Chrome or Edge).
61926192 SB already sets "dom.disable_beforeunload" for Firefox options."""
61936193 self.__check_scope()
6194- self.__check_browser ()
6194+ self._check_browser ()
61956195 if (
61966196 self.is_chromium()
61976197 and self.driver.current_url.startswith("http")
@@ -8468,6 +8468,17 @@ def block_ads(self):
84688468 """Same as self.ad_block()"""
84698469 self.ad_block()
84708470
8471+ def _check_browser(self):
8472+ """This method raises an exception if the active window is closed.
8473+ (This provides a much cleaner exception message in this situation.)"""
8474+ active_window = None
8475+ try:
8476+ active_window = self.driver.current_window_handle # Fails if None
8477+ except Exception:
8478+ pass
8479+ if not active_window:
8480+ raise NoSuchWindowException("Active window was already closed!")
8481+
84718482 def _print(self, msg):
84728483 """Same as Python's print(), but also prints during multithreaded runs.
84738484 Normally, Python's print() command won't print for multithreaded tests.
@@ -8490,32 +8501,32 @@ def _print(self, msg):
84908501
84918502 def add_css_link(self, css_link):
84928503 self.__check_scope()
8493- self.__check_browser ()
8504+ self._check_browser ()
84948505 js_utils.add_css_link(self.driver, css_link)
84958506
84968507 def add_js_link(self, js_link):
84978508 self.__check_scope()
8498- self.__check_browser ()
8509+ self._check_browser ()
84998510 js_utils.add_js_link(self.driver, js_link)
85008511
85018512 def add_css_style(self, css_style):
85028513 self.__check_scope()
8503- self.__check_browser ()
8514+ self._check_browser ()
85048515 js_utils.add_css_style(self.driver, css_style)
85058516
85068517 def add_js_code_from_link(self, js_link):
85078518 self.__check_scope()
8508- self.__check_browser ()
8519+ self._check_browser ()
85098520 js_utils.add_js_code_from_link(self.driver, js_link)
85108521
85118522 def add_js_code(self, js_code):
85128523 self.__check_scope()
8513- self.__check_browser ()
8524+ self._check_browser ()
85148525 js_utils.add_js_code(self.driver, js_code)
85158526
85168527 def add_meta_tag(self, http_equiv=None, content=None):
85178528 self.__check_scope()
8518- self.__check_browser ()
8529+ self._check_browser ()
85198530 js_utils.add_meta_tag(
85208531 self.driver, http_equiv=http_equiv, content=content
85218532 )
@@ -8524,7 +8535,7 @@ def add_meta_tag(self, http_equiv=None, content=None):
85248535
85258536 def activate_messenger(self):
85268537 self.__check_scope()
8527- self.__check_browser ()
8538+ self._check_browser ()
85288539 js_utils.activate_messenger(self.driver)
85298540 self.wait_for_ready_state_complete()
85308541
@@ -8537,7 +8548,7 @@ def set_messenger_theme(
85378548 "bottom_left", "bottom_center", "bottom_right"]
85388549 max_messages: The limit of concurrent messages to display."""
85398550 self.__check_scope()
8540- self.__check_browser ()
8551+ self._check_browser ()
85418552 if not theme:
85428553 theme = "default" # "flat"
85438554 if not location:
@@ -8564,7 +8575,7 @@ def post_message(self, message, duration=None, pause=True, style="info"):
85648575 You can also post messages by using =>
85658576 self.execute_script('Messenger().post("My Message")') """
85668577 self.__check_scope()
8567- self.__check_browser ()
8578+ self._check_browser ()
85688579 if style not in ["info", "success", "error"]:
85698580 style = "info"
85708581 if not duration:
@@ -8601,7 +8612,7 @@ def post_success_message(self, message, duration=None, pause=True):
86018612 duration: The time until the message vanishes. (Default: 2.55s)
86028613 pause: If True, the program waits until the message completes."""
86038614 self.__check_scope()
8604- self.__check_browser ()
8615+ self._check_browser ()
86058616 if not duration:
86068617 if not self.message_duration:
86078618 duration = settings.DEFAULT_MESSAGE_DURATION
@@ -8629,7 +8640,7 @@ def post_error_message(self, message, duration=None, pause=True):
86298640 duration: The time until the message vanishes. (Default: 2.55s)
86308641 pause: If True, the program waits until the message completes."""
86318642 self.__check_scope()
8632- self.__check_browser ()
8643+ self._check_browser ()
86338644 if not duration:
86348645 if not self.message_duration:
86358646 duration = settings.DEFAULT_MESSAGE_DURATION
@@ -9781,7 +9792,7 @@ def quit_extra_driver(self, driver=None):
97819792 if driver == self.driver:
97829793 self.switch_to_default_driver()
97839794 try:
9784- self.__check_browser ()
9795+ self._check_browser ()
97859796 except Exception:
97869797 self._default_driver = self._drivers_list[-1]
97879798 self.switch_to_default_driver()
@@ -10231,18 +10242,6 @@ def __check_scope(self):
1023110242
1023210243 ############
1023310244
10234- def __check_browser(self):
10235- """This method raises an exception if the window was already closed."""
10236- active_window = None
10237- try:
10238- active_window = self.driver.current_window_handle # Fails if None
10239- except Exception:
10240- pass
10241- if not active_window:
10242- raise NoSuchWindowException("Active window was already closed!")
10243-
10244- ############
10245-
1024610245 def __get_exception_message(self):
1024710246 """This method extracts the message from an exception if there
1024810247 was an exception that occurred during the test, assuming
@@ -12298,7 +12297,7 @@ def export_tour(self, name=None, filename="my_tour.js", url=None):
1229812297 def activate_jquery_confirm(self):
1229912298 """See https://craftpip.github.io/jquery-confirm/ for usage."""
1230012299 self.__check_scope()
12301- self.__check_browser ()
12300+ self._check_browser ()
1230212301 js_utils.activate_jquery_confirm(self.driver)
1230312302 self.wait_for_ready_state_complete()
1230412303
0 commit comments