def wait_for_attribute(self, locator, attribute, value, params=None, timeout=5): """ Waits for an element attribute to get a certain value. Note: This function re-get's the element in a loop to avoid caching or stale element issues. :Example: Wait for the class attribute to get 'board-hidden' value :param locator: locator tuple :param attribute: element attribute :param value: attribute value to wait for :param params: (optional) locator params :param timeout: (optional) maximum waiting time (in seconds) (default: 5) :return: None """ def _do_wait(): element = self.get_present_element(locator, params) return value in self.get_attribute(element, attribute) ActionWait(timeout).until(_do_wait, "Attribute never set!")
def wait_for_element_text(self, locator, text, params=None, timeout=10): def _do_wait(): elements = self.get_present_elements(locator, params, timeout=0) for element in elements: txt = self.get_text(element) if txt is not None and text in txt: return True return False return ActionWait(timeout).until(_do_wait, "Element text was never populated!")
def wait_for_non_empty_text(self, locator, params=None, timeout=5): """ Wait and get elements when they're populated with any text. :param locator: locator tuple :param params: (optional) locator params :param timeout: (optional) maximum waiting time (in seconds) (default: 5) :return: list of WebElements """ def _do_wait(): elements = self.get_present_elements(locator, params, timeout=0) for element in elements: if not self.get_text(element): return False return elements return ActionWait(timeout).until(_do_wait, "Element text was never populated!")
def perform_hover_action(self, locator, func, error_msg='', exceptions=None, params=None, alt_loc=None, alt_params=None, **kwargs): """ Hovers an element and performs whatever action is specified in the supplied function. NOTE: Specified function MUST return a non-false value upon success! :param locator: locator tuple or WebElement instance :param func: action to perform while hovering :param error_msg: error message to display if hovering failed :param exceptions: list of exceptions (default: StaleElementReferenceException) :param params: (optional) locator parameters :param alt_loc: alternate locator tuple or WebElement instance to move to with offset :param alt_params: (optional) alternate locator parameters :param kwargs: keyword arguments to the function func :return: result of performed action """ def _do_hover(): try: with self.hover(locator, params, alt_loc, alt_params): return func(**kwargs) except exc: return False exc = [StaleElementReferenceException] if exceptions is not None: try: exc.extend(iter(exceptions)) except TypeError: # exceptions is not iterable exc.append(exceptions) exc = tuple(exc) msg = error_msg or "Performing hover actions failed!" return ActionWait().until(_do_hover, msg)