def assert_not_match_selector(): result = query.resolve_for(self.query_scope) if self in result: raise ExpectationNotMet("Item matched the provided selector") return True
def find_all(): result = query.resolve_for(self) if not result.matches_count: raise ExpectationNotMet(result.failure_message) return result
def assert_not_match_selector(): result = query.resolve_for(self.find_first("xpath", "./parent::*", minimum=0) or self.query_scope) if self in result: raise ExpectationNotMet("Item matched the provided selector") return True
def assert_text(): count = query.resolve_for(self) if not (matches_count(count, query.options) and (count > 0 or expects_none(query.options))): raise ExpectationNotMet(query.failure_message) return True
def assert_no_selector(): result = query.resolve_for(self) if result.matches_count and (len(result) > 0 or expects_none(query.options)): raise ExpectationNotMet(result.negative_failure_message) return True
def reset(self): # Avoid starting the browser just to reset the session. if "browser" in self.__dict__: navigated = False timer = Timer(10) while True: try: # Only trigger a navigation if we haven't done it already, # otherwise it can trigger an endless series of unload modals. if not navigated: self.browser.delete_all_cookies() self._clear_storage() self.browser.get("about:blank") navigated = True while True: try: next(self._find_xpath("/html/body/*")) except StopIteration: break if timer.expired: raise ExpectationNotMet("Timed out waiting for Selenium session reset") sleep(0.05) break except UnexpectedAlertPresentException: # This error is thrown if an unhandled alert is on the page. try: self.browser.switch_to.alert.accept() # Allow time for the modal to be handled. sleep(0.25) except NoAlertPresentException: # The alert is now gone. if self.browser.current_url != "about:blank": # If navigation has not occurred, Firefox may have dismissed the alert # before we could accept it. # Try to navigate again, anticipating the alert this time. try: self.browser.get("about:blank") sleep(0.1) # Wait for the alert. self.browser.switch_to.alert.accept() except NoAlertPresentException: # No alert appeared this time. pass # Try cleaning up the browser again. continue for handle in self.window_handles: if handle != self.current_window_handle: self.close_window(handle)
def reset(self): # Avoid starting the browser just to reset the session. if "browser" in self.__dict__: navigated = False start_time = time() while True: try: # Only trigger a navigation if we haven't done it already, # otherwise it can trigger an endless series of unload modals. if not navigated: self.browser.delete_all_cookies() self.browser.get("about:blank") navigated = True while True: if not len(self._find_xpath("/html/body/*")): break if time() - start_time >= 10: raise ExpectationNotMet( "Timed out waiting for Selenium session reset") sleep(0.05) break except UnexpectedAlertPresentException: # This error is thrown if an unhandled alert is on the page. try: self.browser.switch_to.alert.accept() # Allow time for the modal to be handled. sleep(0.25) except NoAlertPresentException: # The alert is now gone. Nothing to do. pass # Try cleaning up the browser again. continue
def assert_no_title(): if query.resolves_for(self): raise ExpectationNotMet(query.negative_failure_message) return True
def assert_title(): if not query.resolves_for(self): raise ExpectationNotMet(query.failure_message) return True
def assert_no_current_path(): if query.resolves_for(self): raise ExpectationNotMet(query.negative_failure_message)
def assert_current_path(): if not query.resolves_for(self): raise ExpectationNotMet(query.failure_message)