Skip to content

Commit b9b6758

Browse files
committed
Simplify output associated with "NoSuchWindowException"
1 parent 9257879 commit b9b6758

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def test_anything(self):
4343
ElementClickInterceptedException as ECI_Exception,
4444
ElementNotInteractableException as ENI_Exception,
4545
MoveTargetOutOfBoundsException,
46+
NoSuchWindowException,
4647
StaleElementReferenceException,
4748
WebDriverException,
4849
)
@@ -132,6 +133,7 @@ def __init__(self, *args, **kwargs):
132133
def open(self, url):
133134
""" Navigates the current browser window to the specified page. """
134135
self.__check_scope()
136+
self.__check_browser()
135137
if type(url) is str:
136138
url = url.strip() # Remove leading and trailing whitespace
137139
if (type(url) is not str) or not self.__looks_like_a_page_url(url):
@@ -737,11 +739,13 @@ def get_title(self):
737739

738740
def get_user_agent(self):
739741
self.__check_scope()
742+
self.__check_browser()
740743
user_agent = self.driver.execute_script("return navigator.userAgent;")
741744
return user_agent
742745

743746
def get_locale_code(self):
744747
self.__check_scope()
748+
self.__check_browser()
745749
locale_code = self.driver.execute_script(
746750
"return navigator.language || navigator.languages[0];"
747751
)
@@ -2341,10 +2345,12 @@ def open_html_file(self, html_file):
23412345

23422346
def execute_script(self, script, *args, **kwargs):
23432347
self.__check_scope()
2348+
self.__check_browser()
23442349
return self.driver.execute_script(script, *args, **kwargs)
23452350

23462351
def execute_async_script(self, script, timeout=None):
23472352
self.__check_scope()
2353+
self.__check_browser()
23482354
if not timeout:
23492355
timeout = settings.EXTREME_TIMEOUT
23502356
return js_utils.execute_async_script(self.driver, script, timeout)
@@ -2354,6 +2360,7 @@ def safe_execute_script(self, script, *args, **kwargs):
23542360
it's important that the jQuery library has been loaded first.
23552361
This method will load jQuery if it wasn't already loaded."""
23562362
self.__check_scope()
2363+
self.__check_browser()
23572364
if not js_utils.is_jquery_activated(self.driver):
23582365
self.activate_jquery()
23592366
return self.driver.execute_script(script, *args, **kwargs)
@@ -2583,6 +2590,7 @@ def set_content_to_default(self, nested=True):
25832590
def open_new_window(self, switch_to=True):
25842591
""" Opens a new browser tab/window and switches to it by default. """
25852592
self.__check_scope()
2593+
self.__check_browser() # Current window must exist to open a new one
25862594
self.driver.execute_script("window.open('');")
25872595
time.sleep(0.01)
25882596
if switch_to:
@@ -3085,6 +3093,7 @@ def delete_saved_cookies(self, name="cookies.txt"):
30853093

30863094
def wait_for_ready_state_complete(self, timeout=None):
30873095
self.__check_scope()
3096+
self.__check_browser()
30883097
if not timeout:
30893098
timeout = settings.EXTREME_TIMEOUT
30903099
if self.timeout_multiplier and timeout == settings.EXTREME_TIMEOUT:
@@ -6330,26 +6339,32 @@ def start_tour(self, name=None, interval=0):
63306339

63316340
def add_css_link(self, css_link):
63326341
self.__check_scope()
6342+
self.__check_browser()
63336343
js_utils.add_css_link(self.driver, css_link)
63346344

63356345
def add_js_link(self, js_link):
63366346
self.__check_scope()
6347+
self.__check_browser()
63376348
js_utils.add_js_link(self.driver, js_link)
63386349

63396350
def add_css_style(self, css_style):
63406351
self.__check_scope()
6352+
self.__check_browser()
63416353
js_utils.add_css_style(self.driver, css_style)
63426354

63436355
def add_js_code_from_link(self, js_link):
63446356
self.__check_scope()
6357+
self.__check_browser()
63456358
js_utils.add_js_code_from_link(self.driver, js_link)
63466359

63476360
def add_js_code(self, js_code):
63486361
self.__check_scope()
6362+
self.__check_browser()
63496363
js_utils.add_js_code(self.driver, js_code)
63506364

63516365
def add_meta_tag(self, http_equiv=None, content=None):
63526366
self.__check_scope()
6367+
self.__check_browser()
63536368
js_utils.add_meta_tag(
63546369
self.driver, http_equiv=http_equiv, content=content
63556370
)
@@ -8121,6 +8136,7 @@ def export_tour(self, name=None, filename="my_tour.js", url=None):
81218136
def activate_jquery_confirm(self):
81228137
""" See https://craftpip.github.io/jquery-confirm/ for usage. """
81238138
self.__check_scope()
8139+
self.__check_browser()
81248140
js_utils.activate_jquery_confirm(self.driver)
81258141
self.wait_for_ready_state_complete()
81268142

@@ -8449,6 +8465,7 @@ def get_jqc_form_inputs(self, message, buttons, options=None):
84498465

84508466
def activate_messenger(self):
84518467
self.__check_scope()
8468+
self.__check_browser()
84528469
js_utils.activate_messenger(self.driver)
84538470
self.wait_for_ready_state_complete()
84548471

@@ -8462,6 +8479,7 @@ def set_messenger_theme(
84628479
max_messages is the limit of concurrent messages to display.
84638480
"""
84648481
self.__check_scope()
8482+
self.__check_browser()
84658483
if not theme:
84668484
theme = "default" # "flat"
84678485
if not location:
@@ -8489,6 +8507,7 @@ def post_message(self, message, duration=None, pause=True, style="info"):
84898507
self.execute_script('Messenger().post("My Message")')
84908508
"""
84918509
self.__check_scope()
8510+
self.__check_browser()
84928511
if style not in ["info", "success", "error"]:
84938512
style = "info"
84948513
if not duration:
@@ -8524,6 +8543,7 @@ def post_success_message(self, message, duration=None, pause=True):
85248543
pause: If True, the program waits until the message completes.
85258544
"""
85268545
self.__check_scope()
8546+
self.__check_browser()
85278547
if not duration:
85288548
if not self.message_duration:
85298549
duration = settings.DEFAULT_MESSAGE_DURATION
@@ -8547,6 +8567,7 @@ def post_error_message(self, message, duration=None, pause=True):
85478567
pause: If True, the program waits until the message completes.
85488568
"""
85498569
self.__check_scope()
8570+
self.__check_browser()
85508571
if not duration:
85518572
if not self.message_duration:
85528573
duration = settings.DEFAULT_MESSAGE_DURATION
@@ -9753,6 +9774,18 @@ def __check_scope(self):
97539774

97549775
############
97559776

9777+
def __check_browser(self):
9778+
"""This method raises an exception if the window was already closed."""
9779+
active_window = None
9780+
try:
9781+
active_window = self.driver.current_window_handle # Fails if None
9782+
except Exception:
9783+
pass
9784+
if not active_window:
9785+
raise NoSuchWindowException("Active window was already closed!")
9786+
9787+
############
9788+
97569789
def __get_exception_message(self):
97579790
"""This method extracts the message from an exception if there
97589791
was an exception that occurred during the test, assuming

0 commit comments

Comments
 (0)