通用技术 Appium-Python-Client 源码剖析 (一) driver 的元素查找方法

Archer · 2014年07月23日 · 最后由 JennyHui 回复于 2016年08月26日 · 2856 次阅读
本帖已被设为精华帖!

前言

Appium 的实用方法都藏在 Client 的源码里,我尝试在这里剖析一下 Client 的源码,第一篇,我们直接从大家最关注的元素查找说起。
注意!对于 driver 和 webelement 实例,均有对应的元素查找方法(webelement 查找的是下面的子元素),本文讨论的元素查找针对的是 driver 实例。

源码版本:0.9

结构图:

mobileby.py

OK,现在假如我们需要自定义一些 find 方法,比如 find_element_by_xxxx,我们该怎么做?我们看到,appium 提供了一些扩展的 find 方法,它有它自己的一套方式,例如 ACCESSIBILITY_ID 等,要想自定义实现这些方法,appium 首先做的就是:自定义一个 MobileBy 类,这个类从 By 类中继承,然后添加一些需要的属性,这些属性的 value 就是一些文本,不用担心他们不起作用,假如你熟悉 webdriver 的原理,应该会更好地理解。(不知道的同学可以戳这里:http://www.cnblogs.com/backpacker/archive/2012/11/22/2782686.html 或者 http://www.cnblogs.com/timsheng/archive/2012/06/12/2546957.html

#!/usr/bin/env python
from selenium.webdriver.common.by import By

class MobileBy(By): #这里显然是一个继承
    """三个扩展属性,清清楚楚地罗列在这里"""
    IOS_UIAUTOMATION = '-ios uiautomation'
    ANDROID_UIAUTOMATOR = '-android uiautomator'
    ACCESSIBILITY_ID = 'accessibility id'

既然他继承自 By 类,我们直接戳到 By 类看一下,因为 By 类中还有一个 classmethod 下面会用到:

class By(object):
    """
    Set of supported locator strategies.
    """

    ID = "id"
    XPATH = "xpath"
    LINK_TEXT = "link text"
    PARTIAL_LINK_TEXT = "partial link text"
    NAME = "name"
    TAG_NAME = "tag name"
    CLASS_NAME = "class name"
    CSS_SELECTOR = "css selector"

    @classmethod #好吧,我是一个类方法,下文中会用到我
    def is_valid(cls, by): #cls是把类对象本身传进来
        for attr in dir(cls):
            if by == getattr(cls, attr): #判断是不是可用的查找方式
                return True
        return False

这个 MobileBy 类在哪边起作用?我们去跟踪一下,来到这里:

appium 的 webdriver.py
def find_element_by_ios_uiautomation(self, uia_string):
    """Finds an element by uiautomation in iOS.

    :Args:
     - uia_string - The element name in the iOS UIAutomation library

    :Usage:
        driver.find_element_by_ios_uiautomation('.elements()[1].cells()[2]')
    """
    #这里直接访问Appium自己定义的几个类属性
    return self.find_element(by=By.IOS_UIAUTOMATION, value=uia_string)

def find_elements_by_ios_uiautomation(self, uia_string):
    """Finds elements by uiautomation in iOS.

    :Args:
     - uia_string - The element name in the iOS UIAutomation library

    :Usage:
        driver.find_elements_by_ios_uiautomation('.elements()[1].cells()[2]')
    """
    return self.find_elements(by=By.IOS_UIAUTOMATION, value=uia_string)

def find_element_by_android_uiautomator(self, uia_string):
    """Finds element by uiautomator in Android.

    :Args:
     - uia_string - The element name in the Android UIAutomator library

    :Usage:
        driver.find_element_by_android_uiautomator('.elements()[1].cells()[2]')
    """
    return self.find_element(by=By.ANDROID_UIAUTOMATOR, value=uia_string)

def find_elements_by_android_uiautomator(self, uia_string):
    """Finds elements by uiautomator in Android.

    :Args:
     - uia_string - The element name in the Android UIAutomator library

    :Usage:
        driver.find_elements_by_android_uiautomator('.elements()[1].cells()[2]')
    """
    return self.find_elements(by=By.ANDROID_UIAUTOMATOR, value=uia_string)

def find_element_by_accessibility_id(self, id):
    """Finds an element by accessibility id.

    :Args:
     - id - a string corresponding to a recursive element search using the
     Id/Name that the native Accessibility options utilize

    :Usage:
        driver.find_element_by_accessibility_id()
    """
    return self.find_element(by=By.ACCESSIBILITY_ID, value=id)

def find_elements_by_accessibility_id(self, id):
    """Finds elements by accessibility id.

    :Args:
     - id - a string corresponding to a recursive element search using the
     Id/Name that the native Accessibility options utilize

    :Usage:
        driver.find_elements_by_accessibility_id()
    """
    return self.find_elements(by=By.ACCESSIBILITY_ID, value=id)

所以,我们现在知道了,appium 的这些扩展方法都是通过继承 webdriver.Remote 类来直接扩展的,appium 扩展了 webdriver.Remote 来满足他的需求,我们尝试去追踪一下 find_element 和 find_elements 这两个核心方法!

selenium 的 webdriver.py

OK,我们终于来到实现的主体(核心)部分:find_element,find_elements:

def find_element(self, by=By.ID, value=None):
        """
        'Private' method used by the find_element_by_* methods.

        :Usage:
            Use the corresponding find_element_by_* instead of this.

        :rtype: WebElement
        """
        if not By.is_valid(by) or not isinstance(value, str):
            raise InvalidSelectorException("Invalid locator values passed in")

        return self.execute(Command.FIND_ELEMENT,
                             {'using': by, 'value': value})['value']

    def find_elements(self, by=By.ID, value=None):
        """
        'Private' method used by the find_elements_by_* methods.

        :Usage:
            Use the corresponding find_elements_by_* instead of this.

        :rtype: list of WebElement
        """
        if not By.is_valid(by) or not isinstance(value, str):
            raise InvalidSelectorException("Invalid locator values passed in")

        return self.execute(Command.FIND_ELEMENTS,
                             {'using': by, 'value': value})['value']

OK,我们从头到尾再试着理一下:

appium 为了实现自己的 find 查找方式,首先自定义了一个 MobileBy 类,给这个类对象塞入了它定义的一些扩展属性,这些属性的值会通过 webdriver 协议推送到 server 端去识别和执行,为了让这些属性运用到 find 方法中,appium 很好地继承和扩展了 webdriver.Remote,然后通过调用 driver 实例的 find_element 和 find_elements 两个核心方法实现元素查找,所以,既然是扩展,appiumdriver 实例可以使用 seleniumdriver 的所有关于元素查找的实例方法,他们的列表我们就可以整理出来了

seleniumdriver

find_element_by_id
find_elements_by_id
find_element_by_name
find_elements_by_name
find_element_by_link_text
find_elements_by_link_text
find_element_by_partial_link_text
find_elements_by_partial_link_text
find_element_by_tag_name
find_elements_by_tag_name
find_element_by_xpath
find_elements_by_xpath
find_element_by_class_name
find_elements_by_class_name
find_element_by_css_selector
find_elements_by_css_selector

appiumdriver

find_element_by_ios_uiautomation
find_elements_by_ios_uiautomation
find_element_by_android_uiautomator
find_elements_by_android_uiautomator
find_element_by_accessibility_id
find_elements_by_accessibility_id

共收到 10 条回复 时间 点赞

我觉得你不讲下 WebDriver 的源码和机制,这个文章很快就讲不下去了。 比如 :

return self._execute(Command.FIND_CHILD_ELEMENT,
                            {"using": by, "value": value})['value']

execute 方法做了什么,
Command 是什么

#1 楼 @lihuazhang 是的,我正在准备写(一)的扩展篇,甚至是下面这些罗列的所有 find 方法,都细写一下

在源代码中会有一个 find 方法,你漏掉了,看你能不能找出来。。呵呵

Archer #10 · 2014年07月24日 Author

#3 楼 @seveniruby 哦!?我看看~

额,写的略深奥啊,我想很多人不一定想知道怎么实现的,只想知道怎么用的吧

比如 find_element_by_android_uiautomator 的用法实例,与 find_elements_by_android_uiautomator 又有什么不同,控件查找方法选择时该怎么选才最好,有什么需要注意的

#6 楼 @pt4847 那就跟本文题目违背了。。按照此需求,只能另开一帖~

期待关于 appium 新文章更新

问一下,咱 android 的模拟器上怎么找到 app 页面上的元素

小 A;之二,什么时候出来啊?
_^

10楼 已删除
JennyHui [该话题已被删除] 中提及了此贴 08月26日 15:25
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册