Skip to content

Commit d39501e

Browse files
committed
Refactor wait_for_ready_state_complete() and related code
1 parent 8257d32 commit d39501e

File tree

2 files changed

+49
-15
lines changed

2 files changed

+49
-15
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,11 @@ def click(
358358
self.__switch_to_newest_window_if_not_blank()
359359
if settings.WAIT_FOR_RSC_ON_CLICKS:
360360
self.wait_for_ready_state_complete()
361+
else:
362+
# A smaller subset of self.wait_for_ready_state_complete()
363+
self.wait_for_angularjs(timeout=settings.MINI_TIMEOUT)
364+
if self.driver.current_url != pre_action_url:
365+
self.__ad_block_as_needed()
361366
if self.demo_mode:
362367
if self.driver.current_url != pre_action_url:
363368
self.__demo_mode_pause_if_active()
@@ -437,6 +442,11 @@ def double_click(self, selector, by=By.CSS_SELECTOR, timeout=None):
437442
self.safe_execute_script(double_click_script)
438443
if settings.WAIT_FOR_RSC_ON_CLICKS:
439444
self.wait_for_ready_state_complete()
445+
else:
446+
# A smaller subset of self.wait_for_ready_state_complete()
447+
self.wait_for_angularjs(timeout=settings.MINI_TIMEOUT)
448+
if self.driver.current_url != pre_action_url:
449+
self.__ad_block_as_needed()
440450
if self.demo_mode:
441451
if self.driver.current_url != pre_action_url:
442452
self.__demo_mode_pause_if_active()
@@ -1064,7 +1074,7 @@ def click_link_text(self, link_text, timeout=None):
10641074
)
10651075
):
10661076
self.__switch_to_newest_window_if_not_blank()
1067-
if settings.WAIT_FOR_RSC_ON_CLICKS:
1077+
if settings.WAIT_FOR_RSC_ON_PAGE_LOADS:
10681078
self.wait_for_ready_state_complete()
10691079
if self.demo_mode:
10701080
if self.driver.current_url != pre_action_url:
@@ -1196,7 +1206,7 @@ def click_partial_link_text(self, partial_link_text, timeout=None):
11961206
)
11971207
):
11981208
self.__switch_to_newest_window_if_not_blank()
1199-
if settings.WAIT_FOR_RSC_ON_CLICKS:
1209+
if settings.WAIT_FOR_RSC_ON_PAGE_LOADS:
12001210
self.wait_for_ready_state_complete()
12011211
if self.demo_mode:
12021212
if self.driver.current_url != pre_action_url:
@@ -1641,6 +1651,11 @@ def click_active_element(self):
16411651
self.__switch_to_newest_window_if_not_blank()
16421652
if settings.WAIT_FOR_RSC_ON_CLICKS:
16431653
self.wait_for_ready_state_complete()
1654+
else:
1655+
# A smaller subset of self.wait_for_ready_state_complete()
1656+
self.wait_for_angularjs(timeout=settings.MINI_TIMEOUT)
1657+
if self.driver.current_url != pre_action_url:
1658+
self.__ad_block_as_needed()
16441659
if self.demo_mode:
16451660
if self.driver.current_url != pre_action_url:
16461661
self.__demo_mode_pause_if_active()
@@ -2142,6 +2157,9 @@ def __select_option(
21422157
self.__switch_to_newest_window_if_not_blank()
21432158
if settings.WAIT_FOR_RSC_ON_CLICKS:
21442159
self.wait_for_ready_state_complete()
2160+
else:
2161+
# A smaller subset of self.wait_for_ready_state_complete()
2162+
self.wait_for_angularjs(timeout=settings.MINI_TIMEOUT)
21452163
if self.demo_mode:
21462164
if self.driver.current_url != pre_action_url:
21472165
self.__demo_mode_pause_if_active()
@@ -3112,35 +3130,46 @@ def delete_saved_cookies(self, name="cookies.txt"):
31123130
if cookies_file_path.endswith(".txt"):
31133131
os.remove(cookies_file_path)
31143132

3133+
def __ad_block_as_needed(self):
3134+
""" This is an internal method for handling ad-blocking.
3135+
Use "pytest --ad-block" to enable this during tests.
3136+
When not Chromium or in headless mode, use the hack. """
3137+
if self.ad_block_on and (self.headless or not self.is_chromium()):
3138+
# (Chromium browsers in headed mode use the extension instead)
3139+
current_url = self.get_current_url()
3140+
if not current_url == self.__last_page_load_url:
3141+
if page_actions.is_element_present(
3142+
self.driver, "iframe", By.CSS_SELECTOR
3143+
):
3144+
self.ad_block()
3145+
self.__last_page_load_url = current_url
3146+
31153147
def wait_for_ready_state_complete(self, timeout=None):
3148+
""" Waits for the "readyState" of the page to be "complete".
3149+
Returns True when the method completes. """
31163150
self.__check_scope()
31173151
self.__check_browser()
31183152
if not timeout:
31193153
timeout = settings.EXTREME_TIMEOUT
31203154
if self.timeout_multiplier and timeout == settings.EXTREME_TIMEOUT:
31213155
timeout = self.__get_new_timeout(timeout)
3122-
is_ready = js_utils.wait_for_ready_state_complete(self.driver, timeout)
3156+
js_utils.wait_for_ready_state_complete(self.driver, timeout)
31233157
self.wait_for_angularjs(timeout=settings.MINI_TIMEOUT)
31243158
if self.js_checking_on:
31253159
self.assert_no_js_errors()
3126-
if self.ad_block_on and (self.headless or not self.is_chromium()):
3127-
# For Chromium browsers in headed mode, the extension is used
3128-
current_url = self.get_current_url()
3129-
if not current_url == self.__last_page_load_url:
3130-
if page_actions.is_element_present(
3131-
self.driver, "iframe", By.CSS_SELECTOR
3132-
):
3133-
self.ad_block()
3134-
self.__last_page_load_url = current_url
3135-
return is_ready
3160+
self.__ad_block_as_needed()
3161+
return True
31363162

31373163
def wait_for_angularjs(self, timeout=None, **kwargs):
3164+
""" Waits for Angular components of the page to finish loading.
3165+
Returns True when the method completes. """
31383166
self.__check_scope()
31393167
if not timeout:
3140-
timeout = settings.LARGE_TIMEOUT
3141-
if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:
3168+
timeout = settings.MINI_TIMEOUT
3169+
if self.timeout_multiplier and timeout == settings.MINI_TIMEOUT:
31423170
timeout = self.__get_new_timeout(timeout)
31433171
js_utils.wait_for_angularjs(self.driver, timeout, **kwargs)
3172+
return True
31443173

31453174
def sleep(self, seconds):
31463175
self.__check_scope()

seleniumbase/fixtures/js_utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ def wait_for_angularjs(driver, timeout=settings.LARGE_TIMEOUT, **kwargs):
8080
"handler": handler,
8181
"suffix": suffix,
8282
}
83+
try:
84+
# This closes any pop-up alerts (otherwise the next part fails)
85+
driver.execute_script("")
86+
except Exception:
87+
pass
8388
try:
8489
execute_async_script(driver, script, timeout=timeout)
8590
except Exception:

0 commit comments

Comments
 (0)