def get_search_element(selector): try: return getattr(By, selector.upper()) except AttributeError: raise InvalidElementStateException( 'Please select a valid selector: CSS_SELECTOR, XPATH, ID,' ' LINK_TEXT, PARTIAL_LINK_TEXT, NAME, TAG_NAME, CLASS_NAME')
def get_selector_strategy(selector): selected_strategy = None if selector.upper() == "CSS SELECTOR": selected_strategy = By.CSS_SELECTOR elif selector.upper() == "XPATH": selected_strategy = By.XPATH elif selector.upper() == "ID": selected_strategy = By.ID elif selector.upper() == "LINK TEXT": selected_strategy = By.LINK_TEXT elif selector.upper() == "PARTIAL LINK TEXT": selected_strategy = By.PARTIAL_LINK_TEXT elif selector.upper() == "NAME": selected_strategy = By.NAME elif selector.upper() == "TAG NAME": selected_strategy = By.TAG_NAME elif selector.upper() == "CLASS NAME": selected_strategy = By.CLASS_NAME else: raise InvalidElementStateException( "Please select a valid selector: CSS SELECTOR, XPATH, ID, LINK TEXT, PARTIAL LINK TEXT, NAME, TAG NAME, CLASS NAME" ) return selected_strategy
def check_eyes_region_by_selector(self, selector, value, name, includeEyesLog=False, httpDebugLog=False, force_full_page_screenshot=True): """ Takes a snapshot of the region of the element found by calling find_element(by, value) from the browser using the web driver and matches it with the expected output. With a choice from eight selectors, listed below to check by. Arguments: | Selector (string) | This will decide what element will be located. Supported selectors: CSS SELECTOR, XPATH, ID, LINK TEXT, PARTIAL LINK TEXT, NAME, TAG NAME, CLASS NAME. | | Value (string) | The specific value of the selector. e.g. a CSS SELECTOR value .first.expanded.dropdown | | Name (string) | Name that will be given to region in Eyes. | | Include Eyes Log (default=False) | The Eyes logs will not be included by default. To activate, pass 'True' in the variable. | | Force Full Page Screenshot (default=True) | Will force the browser to check the whole page for the value, rather than only search the viewable viewport. | Example: | *Keywords* | *Parameters* | | Open Browser | http://www.navinet.net/ | gc | | | | | | Open Eyes Session | http://www.navinet.net/ | RobotAppEyes_Test | NaviNet_RobotAppEyes_Test | YourApplitoolsKey | 1024 | 768 | | Check Eyes Region By Selector | CSS SELECTOR | .first.expanded.dropdown | NaviNetCssElement | | | | | Close Eyes Session | False | | | | | | NOTE: Breaks when attempting to find region to screenshot """ if includeEyesLog is True: logger.set_logger(StdoutLogger()) logger.open_() if httpDebugLog is True: http.client.HTTPConnection.debuglevel = 1 eyes.force_full_page_screenshot = force_full_page_screenshot searchElement = None searchByDict = { 'CSS SELECTOR': By.CSS_SELECTOR, 'XPATH': By.XPATH, 'ID': By.ID, 'LINK TEXT': By.LINK_TEXT, 'PARTIAL LINK TEXT': By.PARTIAL_LINK_TEXT, 'NAME': By.NAME, 'TAG NAME': By.TAG_NAME, 'CLASS NAME': By.CLASS_NAME } selector = selector.upper() if selector in searchByDict: searchElement = searchByDict[selector] else: raise InvalidElementStateException( 'Please select a valid selector: CSS SELECTOR, XPATH, ID, LINK TEXT, PARTIAL LINK TEXT, NAME, TAG NAME, CLASS NAME' ) eyes.check_region_by_selector(searchElement, value, name)
def wait_until_selected(self, seconds_to_wait=DEFAULT_TIMEOUT): """ Wait for an element to be selected, and raise the correct exception if it isn't """ try: WebDriverWait(self.driver, seconds_to_wait).until( ec.element_located_selection_state_to_be(self.locator, True)) except TimeoutException: if self.driver.find_element(*self.locator): raise InvalidElementStateException( f"Element '{self.locator}' exists but is not selected") return True
def check_eyes_region_by_selector(self, selector, value, name, includeEyesLog=False, httpDebugLog=False): """ Takes a snapshot of the region of the element found by calling find_element(by, value) from the browser using the web driver and matches it with the expected output. With a choice from eight selectors, listed below to check by. Arguments: | Selector (string) | This will decide what element will be located. The supported selectors include: CSS SELECTOR, XPATH, ID, LINK TEXT, PARTIAL LINK TEXT, NAME, TAG NAME, CLASS NAME. | | Value (string) | The specific value of the selector. e.g. a CSS SELECTOR value .first.expanded.dropdown | | Name (string) | Name that will be given to region in Eyes. | | Include Eyes Log (default=False) | The Eyes logs will not be included by default. To activate, pass 'True' in the variable. | | HTTP Debug Log (default=False) | The HTTP Debug logs will not be included by default. To activate, pass 'True' in the variable. | Example: | *Keywords* | *Parameters* | | Open Browser | http://www.navinet.net/ | gc | | | | | | Open Eyes Session | http://www.navinet.net/ | RobotAppEyes_Test | NaviNet_RobotAppEyes_Test | YourApplitoolsKey | 1024 | 768 | | Check Eyes Region By Selector | CSS SELECTOR | .first.expanded.dropdown | NaviNetCssElement | | | | | Close Eyes Session | False | | | | | | """ if includeEyesLog is True: logger.set_logger(StdoutLogger()) logger.open_() if httpDebugLog is True: httplib.HTTPConnection.debuglevel = 1 searchElement = None if selector.upper() == 'CSS SELECTOR': searchElement = By.CSS_SELECTOR elif selector.upper() == 'XPATH': searchElement = By.XPATH elif selector.upper() == 'ID': searchElement = By.ID elif selector.upper() == 'LINK TEXT': searchElement = By.LINK_TEXT elif selector.upper() == 'PARTIAL LINK TEXT': searchElement = By.PARTIAL_LINK_TEXT elif selector.upper() == 'NAME': searchElement = By.NAME elif selector.upper() == 'TAG NAME': searchElement = By.TAG_NAME elif selector.upper() == 'CLASS NAME': searchElement = By.CLASS_NAME else: raise InvalidElementStateException( 'Please select a valid selector: CSS SELECTOR, XPATH, ID, LINK TEXT, PARTIAL LINK TEXT, NAME, TAG NAME, CLASS NAME' ) eyes.check_region_by_selector(searchElement, value, name)
def check_eyes_window_with_ignore_region_by_selector(self, name, selector, selector_value, includeEyesLog=False, httpDebugLog=False): """ Takes a snapshot from the browser using the web driver and matches it with the expected output. Arguments: | Name (string) | Name that will be given to region in Eyes. | | Include Eyes Log (default=False) | The Eyes logs will not be included by default. To activate, pass 'True' in the variable. | | HTTP Debug Log (default=False) | The HTTP Debug logs will not be included by default. To activate, pass 'True' in the variable. | Example: | *Keywords* | *Parameters* | | Open Browser | http://www.navinet.net/ | gc | | | | | | Open Eyes Session | http://www.navinet.net/ | RobotAppEyes_Test | NaviNet_RobotAppEyes_Test | YourApplitoolsKey | 1024 | 768 | | Check Eyes Window | NaviNet Home | True | | | | | | Close Eyes Session | False | | | | | | """ if includeEyesLog is True: logger.set_logger(StdoutLogger()) logger.open_() if httpDebugLog is True: httplib.HTTPConnection.debuglevel = 1 eyes.check_window(name) searchElement = None if selector.upper() == 'CSS_SELECTOR': searchElement = By.CSS_SELECTOR elif selector.upper() == 'XPATH': searchElement = By.XPATH elif selector.upper() == 'ID': searchElement = By.ID elif selector.upper() == 'LINK TEXT': searchElement = By.LINK_TEXT elif selector.upper() == 'PARTIAL LINK TEXT': searchElement = By.PARTIAL_LINK_TEXT elif selector.upper() == 'NAME': searchElement = By.NAME elif selector.upper() == 'TAG NAME': searchElement = By.TAG_NAME elif selector.upper() == 'CLASS NAME': searchElement = By.CLASS_NAME else: raise InvalidElementStateException( 'Please select a valid selector: CSS SELECTOR, XPATH, ID, LINK TEXT, PARTIAL LINK TEXT, NAME, TAG NAME, CLASS NAME') eyes.check_window(tag=name, target=Target().ignore(IgnoreRegionBySelector(searchElement, selector_value)))
def waitForElementWithPropertyValue(self, css_selector, property_name, property_value, timeout=DEFAULT_WAIT_TIMEOUT): """ Waits for DOM element matching the provided CSS selector to be available and to have the given attribute or property set to the given value Args: - css_selector: CSS selector string - property_name: HTML attribute or DOM Element JS property name - property_value: Unicode string representing expected JS value Kwargs: - timeout: Operation timeout in seconds Returns: a single WebElement """ def get_element_checker(): def find_element_by_selector_and_prop_value(driver): # uncached to give the test a chance to pass el = self.find_uncached_element_by_css_selector(css_selector) # excitingly, WebDriver uses the word "attribute" to mean # "attribute or property"! if self.getElementProperty(el, property_name) == \ property_value: return True return False return find_element_by_selector_and_prop_value message = u"Couldn't find elem matching %s with property '%s' set " \ u"to '%s')" % (css_selector, property_name, property_value) try: WebDriverWait(self, timeout, poll_frequency=.25).until( get_element_checker(), message=message) except TimeoutException: raise InvalidElementStateException(message) return self.find_uncached_element_by_css_selector(css_selector)
def check_eyes_region_by_element(self, selector, value, name, includeEyesLog=False, httpDebugLog=False): """ Takes a snapshot of the region of the given selector and element value from the browser using the web driver and matches it with the expected output. With a choice from four selectors, listed below, to check by. Arguments: | Selector (string) | This will decide what element will be located. The supported selectors include: XPATH, ID, CLASS NAME, CSS SELECTOR | | Value (string) | The specific value of the selector. e.g. an xpath value //*[@id="navbar"]/div/div | | Name (string) | Name that will be given to region in Eyes. | | Include Eyes Log (default=False) | The Eyes logs will not be included by default. To activate, pass 'True' in the variable. | | HTTP Debug Log (default=False) | The HTTP Debug logs will not be included by default. To activate, pass 'True' in the variable. | Example: | *Keywords* | *Parameters* | | Open Browser | http://www.navinet.net/ | gc | | | | | | Open Eyes Session | http://www.navinet.net/ | RobotAppEyes_Test | NaviNet_RobotAppEyes_Test | YourApplitoolsKey | 1024 | 768 | | Check Eyes Region By Element | CLASS NAME | container | NaviNetClassElement | | | | | Close Eyes Session | False | | | | | | """ if includeEyesLog is True: logger.set_logger(StdoutLogger()) logger.open_() if httpDebugLog is True: httplib.HTTPConnection.debuglevel = 1 searchElement = None if selector.upper() == 'XPATH': searchElement = driver.find_element_by_xpath(value) elif selector.upper() == 'ID': searchElement = driver.find_element_by_id(value) elif selector.upper() == 'CLASS NAME': searchElement = driver.find_element_by_class_name(value) elif selector.upper() == 'CSS SELECTOR': searchElement = driver.find_element_by_css_selector(value) else: raise InvalidElementStateException( 'Please select a valid selector: XPATH, ID, CLASS NAME, CSS SELECTOR' ) eyes.check_region_by_element(searchElement, name)
def click_on(self): """ Clicks on web element or returns InvalidElementStateException in case the element is invisible or not clickable. :return: """ try: element = WebDriverWait( self.driver, self.explicit_wait_time).until( EC.element_to_be_clickable(self.locator)) element.click() return None except TimeoutException: raise InvalidElementStateException( '\nERROR: can not find element. The element is invisible or not clickable.\n' )
def wait_disabled(self, timeout, error=None): """ Wait for given element to be disabled. :param timeout: Time in seconds to wait for element. :type timeout: int :param error: Raise InvalidElementStateException on failure. :type error: bool, string :return: Element, None """ if not self.controller.wait(timeout=timeout, condition=self.check.disabled): if error: raise InvalidElementStateException(error if isinstance(error, string_types) else \ 'Element by selector "{}" not found or is enabled'.format(self.selector)) return None return self
def fill_element(driver=None, field=None, value=None): """ Fill the given field with the value. :param driver: a selenium web driver :param field: a dictionary :param value: a string or castable to string :raise AssertionError: driver is not define, field fails validation :raise InvalidElementStateException: if the element is not user-editable :raise Exception: web element not found :return: 0 if success """ try: assert driver is not None and isinstance(driver, web_drivers_tuple()),\ "Driver is expected." assert __field_validation( field=field), "Field '{}' is not a valid field".format(field) elem = find_element(driver=driver, field=field) if value: elem.clear() elem.send_keys(str(value)) else: number_of_backspace_hit = len(elem.get_attribute('value')) elem.send_keys(number_of_backspace_hit * Keys.BACKSPACE) return 0 except AssertionError as assertion: log.error("actions.fill_element raised an assertion with following" " input driver:'{}', field:'{}' and value:'{}'." " Assertion is '{}'".format(driver, field, value, assertion.args)) raise except InvalidElementStateException as invalid_element: log.error(invalid_element) invalid_element.args = ( "Element '{}' must be user-editable".format(field), ) raise InvalidElementStateException( "Element '{}' must be user-editable".format(field)) from None except Exception as exception: logging.error( "actions.fill_element raised an exception. Exception is '{}'". format(exception.args)) raise Exception( "actions.fill_element raised an exception. Exception is '{}'". format(exception.args[0])) from None
def check_eyes_window_ignore_region_by_selector( self, selector, value, name, force_full_page_screenshot=False, includeEyesLog=False, httpDebugLog=False): """This is a new addition to test the latest functionality of ignoring regions""" if includeEyesLog is True: logger.set_logger(StdoutLogger()) logger.open_() if httpDebugLog is True: httplib.HTTPConnection.debuglevel = 1 searchElement = None if selector.upper() == 'CSS SELECTOR': searchElement = By.CSS_SELECTOR elif selector.upper() == 'XPATH': searchElement = By.XPATH elif selector.upper() == 'ID': searchElement = By.ID elif selector.upper() == 'LINK TEXT': searchElement = By.LINK_TEXT elif selector.upper() == 'PARTIAL LINK TEXT': searchElement = By.PARTIAL_LINK_TEXT elif selector.upper() == 'NAME': searchElement = By.NAME elif selector.upper() == 'TAG NAME': searchElement = By.TAG_NAME elif selector.upper() == 'CLASS NAME': searchElement = By.CLASS_NAME else: raise InvalidElementStateException( 'Please select a valid selector: CSS SELECTOR, XPATH, ID, LINK TEXT, PARTIAL LINK TEXT, NAME, TAG NAME, CLASS NAME' ) eyes.force_full_page_screenshot = force_full_page_screenshot eyes.check_window(name, target=Target().ignore( IgnoreRegionBySelector(searchElement, value)))
def wait_invisible(self, timeout, error=None): """ Wait for given element to be invisible. :param timeout: Time in seconds to wait for element. :type timeout: int :param error: Raise InvalidElementStateException on failure. :type error: bool, string :return: Element, None """ if not self._parent.wait(timeout=timeout, condition=not self.is_visible): if error: raise InvalidElementStateException(error if isinstance(error, string_types) else \ 'Element by selector "{}" not found or is visible'.format( self.selector)) return None return self
def wait_disabled(self, timeout, length=1, strict=False, error=None): """ Wait for given length of elements to be available and disabled. :param timeout: Time in seconds to wait for elements. :type timeout: int :param length: Number of elements to wait for. :type length: int :param strict: Expect exactly the length of elements, no more. :type strict: bool :param error: Raise InvalidElementStateException on failure. :type error: bool, string :return: Elements """ def check(): # pylint: disable=line-too-long return self.count() == length if strict else self.count() >= length and self.checks.disabled() if not self.controller.wait(timeout=timeout, condition=check): if error: raise InvalidElementStateException(error if isinstance(error, string_types) else \ '{} elements by selector "{}" not disabled'.format(length, self.selector)) return self