Skip to content

Refactor wait_for_ready_state_complete() and related code #1108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion seleniumbase/__version__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# seleniumbase package
__version__ = "2.2.6"
__version__ = "2.2.7"
59 changes: 44 additions & 15 deletions seleniumbase/fixtures/base_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,11 @@ def click(
self.__switch_to_newest_window_if_not_blank()
if settings.WAIT_FOR_RSC_ON_CLICKS:
self.wait_for_ready_state_complete()
else:
# A smaller subset of self.wait_for_ready_state_complete()
self.wait_for_angularjs(timeout=settings.MINI_TIMEOUT)
if self.driver.current_url != pre_action_url:
self.__ad_block_as_needed()
if self.demo_mode:
if self.driver.current_url != pre_action_url:
self.__demo_mode_pause_if_active()
Expand Down Expand Up @@ -437,6 +442,11 @@ def double_click(self, selector, by=By.CSS_SELECTOR, timeout=None):
self.safe_execute_script(double_click_script)
if settings.WAIT_FOR_RSC_ON_CLICKS:
self.wait_for_ready_state_complete()
else:
# A smaller subset of self.wait_for_ready_state_complete()
self.wait_for_angularjs(timeout=settings.MINI_TIMEOUT)
if self.driver.current_url != pre_action_url:
self.__ad_block_as_needed()
if self.demo_mode:
if self.driver.current_url != pre_action_url:
self.__demo_mode_pause_if_active()
Expand Down Expand Up @@ -1064,7 +1074,7 @@ def click_link_text(self, link_text, timeout=None):
)
):
self.__switch_to_newest_window_if_not_blank()
if settings.WAIT_FOR_RSC_ON_CLICKS:
if settings.WAIT_FOR_RSC_ON_PAGE_LOADS:
self.wait_for_ready_state_complete()
if self.demo_mode:
if self.driver.current_url != pre_action_url:
Expand Down Expand Up @@ -1196,7 +1206,7 @@ def click_partial_link_text(self, partial_link_text, timeout=None):
)
):
self.__switch_to_newest_window_if_not_blank()
if settings.WAIT_FOR_RSC_ON_CLICKS:
if settings.WAIT_FOR_RSC_ON_PAGE_LOADS:
self.wait_for_ready_state_complete()
if self.demo_mode:
if self.driver.current_url != pre_action_url:
Expand Down Expand Up @@ -1641,6 +1651,11 @@ def click_active_element(self):
self.__switch_to_newest_window_if_not_blank()
if settings.WAIT_FOR_RSC_ON_CLICKS:
self.wait_for_ready_state_complete()
else:
# A smaller subset of self.wait_for_ready_state_complete()
self.wait_for_angularjs(timeout=settings.MINI_TIMEOUT)
if self.driver.current_url != pre_action_url:
self.__ad_block_as_needed()
if self.demo_mode:
if self.driver.current_url != pre_action_url:
self.__demo_mode_pause_if_active()
Expand Down Expand Up @@ -2142,6 +2157,9 @@ def __select_option(
self.__switch_to_newest_window_if_not_blank()
if settings.WAIT_FOR_RSC_ON_CLICKS:
self.wait_for_ready_state_complete()
else:
# A smaller subset of self.wait_for_ready_state_complete()
self.wait_for_angularjs(timeout=settings.MINI_TIMEOUT)
if self.demo_mode:
if self.driver.current_url != pre_action_url:
self.__demo_mode_pause_if_active()
Expand Down Expand Up @@ -3112,35 +3130,46 @@ def delete_saved_cookies(self, name="cookies.txt"):
if cookies_file_path.endswith(".txt"):
os.remove(cookies_file_path)

def __ad_block_as_needed(self):
""" This is an internal method for handling ad-blocking.
Use "pytest --ad-block" to enable this during tests.
When not Chromium or in headless mode, use the hack. """
if self.ad_block_on and (self.headless or not self.is_chromium()):
# (Chromium browsers in headed mode use the extension instead)
current_url = self.get_current_url()
if not current_url == self.__last_page_load_url:
if page_actions.is_element_present(
self.driver, "iframe", By.CSS_SELECTOR
):
self.ad_block()
self.__last_page_load_url = current_url

def wait_for_ready_state_complete(self, timeout=None):
""" Waits for the "readyState" of the page to be "complete".
Returns True when the method completes. """
self.__check_scope()
self.__check_browser()
if not timeout:
timeout = settings.EXTREME_TIMEOUT
if self.timeout_multiplier and timeout == settings.EXTREME_TIMEOUT:
timeout = self.__get_new_timeout(timeout)
is_ready = js_utils.wait_for_ready_state_complete(self.driver, timeout)
js_utils.wait_for_ready_state_complete(self.driver, timeout)
self.wait_for_angularjs(timeout=settings.MINI_TIMEOUT)
if self.js_checking_on:
self.assert_no_js_errors()
if self.ad_block_on and (self.headless or not self.is_chromium()):
# For Chromium browsers in headed mode, the extension is used
current_url = self.get_current_url()
if not current_url == self.__last_page_load_url:
if page_actions.is_element_present(
self.driver, "iframe", By.CSS_SELECTOR
):
self.ad_block()
self.__last_page_load_url = current_url
return is_ready
self.__ad_block_as_needed()
return True

def wait_for_angularjs(self, timeout=None, **kwargs):
""" Waits for Angular components of the page to finish loading.
Returns True when the method completes. """
self.__check_scope()
if not timeout:
timeout = settings.LARGE_TIMEOUT
if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:
timeout = settings.MINI_TIMEOUT
if self.timeout_multiplier and timeout == settings.MINI_TIMEOUT:
timeout = self.__get_new_timeout(timeout)
js_utils.wait_for_angularjs(self.driver, timeout, **kwargs)
return True

def sleep(self, seconds):
self.__check_scope()
Expand Down
5 changes: 5 additions & 0 deletions seleniumbase/fixtures/js_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ def wait_for_angularjs(driver, timeout=settings.LARGE_TIMEOUT, **kwargs):
"handler": handler,
"suffix": suffix,
}
try:
# This closes any pop-up alerts (otherwise the next part fails)
driver.execute_script("")
except Exception:
pass
try:
execute_async_script(driver, script, timeout=timeout)
except Exception:
Expand Down