Example #1
0
 def __init__(self, context=False, describe="undefined", **kwargs):
     self.describe = describe
     if not kwargs:
         raise ValueError("Please specify a locator")
     if len(kwargs) > 1:
         raise ValueError("Please specify only one locator")
     self.k, self.v = next(iter(kwargs.items()))
     try:
         self.locator = (LOCATOR_LIST[self.k], self.v)
     except KeyError:
         raise FindElementTypesError("Element positioning of type '{}' is not supported. ".format(self.k))
     self.has_context = bool(context)
Example #2
0
    def __init__(self, timeout=5, describe="undefined", index=0, **kwargs):
        self.timeout = timeout
        self.index = index
        self.desc = describe
        if not kwargs:
            raise ValueError("Please specify a locator")
        if len(kwargs) > 1:
            raise ValueError("Please specify only one locator")
        self.kwargs = kwargs
        self.k, self.v = next(iter(kwargs.items()))

        if self.k not in LOCATOR_LIST.keys():
            raise FindElementTypesError("Element positioning of type '{}' is not supported.".format(self.k))
Example #3
0
 def __init__(self, context=False, timeout=4, log=False, describe="", **kwargs):
     warnings.warn("use NewPageElement instead", DeprecationWarning, stacklevel=2)
     self.time_out = timeout
     self.log = log
     self.describe = describe
     if not kwargs:
         raise ValueError("Please specify a locator")
     if len(kwargs) > 1:
         raise ValueError("Please specify only one locator")
     self.k, self.v = next(iter(kwargs.items()))
     try:
         self.locator = (LOCATOR_LIST[self.k], self.v)
     except KeyError:
         raise FindElementTypesError("Element positioning of type '{}' is not supported. ".format(self.k))
     self.has_context = bool(context)
Example #4
0
    def find(self, parent_elem=None, multi=False, retry=3, **kwargs):
        """
        通过locator方式查找元素,支持父级节点继承查找,支持page查找,只返回可见元素
        :param parent_elem: 待查找元素的父节点元素
        :param multi: 是否返回多个目标元素
        :param retry: 重试次数
        :return:
        """
        if not kwargs:
            raise ValueError("""请定义一个locator,格式如xpath='//*[@id="name"]'""")
        if len(kwargs) > 1:
            raise ValueError("只能定义一个locator")
        by, loc = next(iter(kwargs.items()))
        try:
            self.locator = (LOCATOR_LIST[by], loc)
        except KeyError:
            raise FindElementTypesError('不支持该定位方式%s' % str(by))

        # 如果有父节点,从父节点找元素,否则通过page查找
        try:
            if parent_elem:
                elems = self._re_find(parent_elem.find_elements, retry,
                                      *self.locator)
            else:
                elems = self._re_find(self.driver.find_elements, retry,
                                      *self.locator)
        except:
            return self

        # 将可见元素放入列表,根据multi返回1个或多个
        displayed_list = []
        for elem in elems:
            if elem.is_displayed():
                displayed_list.append(elem)
        if displayed_list and multi:
            return displayed_list
        elif displayed_list and not multi:
            return displayed_list[0]
        else:
            return self
Example #5
0
    def __get_element(self, by, value):
        """
        Judge element positioning way, and returns the element.
        """

        # selenium
        if by == "id_":
            self.__find_element((By.ID, value))
            elem = Browser.driver.find_elements_by_id(value)[self.index]
        elif by == "name":
            self.__find_element((By.NAME, value))
            elem = Browser.driver.find_elements_by_name(value)[self.index]
        elif by == "class_name":
            self.__find_element((By.CLASS_NAME, value))
            elem = Browser.driver.find_elements_by_class_name(value)[
                self.index]
        elif by == "tag":
            self.__find_element((By.TAG_NAME, value))
            elem = Browser.driver.find_elements_by_tag_name(value)[self.index]
        elif by == "link_text":
            self.__find_element((By.LINK_TEXT, value))
            elem = Browser.driver.find_elements_by_link_text(value)[self.index]
        elif by == "partial_link_text":
            self.__find_element((By.PARTIAL_LINK_TEXT, value))
            elem = Browser.driver.find_elements_by_partial_link_text(value)[
                self.index]
        elif by == "xpath":
            self.__find_element((By.XPATH, value))
            elem = Browser.driver.find_elements_by_xpath(value)[self.index]
        elif by == "css":
            self.__find_element((By.CSS_SELECTOR, value))
            elem = Browser.driver.find_elements_by_css_selector(value)[
                self.index]

        # appium
        elif by == "ios_uiautomation":
            self.__find_element((MobileBy.IOS_UIAUTOMATION, value))
            elem = Browser.driver.find_elements_by_ios_uiautomation(value)[
                self.index]
        elif by == "ios_predicate":
            self.__find_element((MobileBy.IOS_PREDICATE, value))
            elem = Browser.driver.find_elements_by_ios_predicate(value)[
                self.index]
        elif by == "ios_class_chain":
            self.__find_element((MobileBy.IOS_CLASS_CHAIN, value))
            elem = Browser.driver.find_elements_by_ios_class_chain(value)[
                self.index]
        elif by == "android_uiautomator":
            self.__find_element((MobileBy.ANDROID_UIAUTOMATOR, value))
            elem = Browser.driver.find_elements_by_android_uiautomator(value)[
                self.index]
        elif by == "android_viewtag":
            self.__find_element((MobileBy.ANDROID_VIEWTAG, value))
            elem = Browser.driver.find_elements_by_android_viewtag(value)[
                self.index]
        elif by == "android_data_matcher":
            self.__find_element((MobileBy.ANDROID_DATA_MATCHER, value))
            elem = Browser.driver.find_elements_by_android_data_matcher(value)[
                self.index]
        elif by == "accessibility_id":
            self.__find_element((MobileBy.ACCESSIBILITY_ID, value))
            elem = Browser.driver.find_elements_by_accessibility_id(value)[
                self.index]
        elif by == "android_view_matcher":
            self.__find_element((MobileBy.ANDROID_VIEW_MATCHER, value))
            elem = Browser.driver.find_elements_by_android_view_matcher(value)[
                self.index]
        elif by == "windows_uiautomation":
            self.__find_element((MobileBy.WINDOWS_UI_AUTOMATION, value))
            elem = Browser.driver.find_elements_by_windows_uiautomation(value)[
                self.index]
        elif by == "image":
            self.__find_element((MobileBy.IMAGE, value))
            elem = Browser.driver.find_elements_by_image(value)[self.index]
        elif by == "custom":
            self.__find_element((MobileBy.CUSTOM, value))
            elem = Browser.driver.find_elements_by_custom(value)[self.index]
        else:
            raise FindElementTypesError(
                "Please enter the correct targeting elements")
        try:
            style_red = 'arguments[0].style.border="2px solid #FF0000"'
            style_blue = 'arguments[0].style.border="2px solid #00FF00"'
            style_null = 'arguments[0].style.border=""'

            for _ in range(2):
                Browser.driver.execute_script(style_red, elem)
                sleep(0.1)
                Browser.driver.execute_script(style_blue, elem)
                sleep(0.1)
            Browser.driver.execute_script(style_blue, elem)
            sleep(0.5)
            Browser.driver.execute_script(style_null, elem)
        except WebDriverException:
            pass

        return elem