使用除边界之外的相同属性值定位UI对象

时间:2014-09-11 15:27:04

标签: android monkeyrunner uiautomator androidviewclient

在自动化测试用例的同时,我遇到了多个对象在UiAutomator中显示相同属性值的情况,除了边界(即显示器上的x,y位置)。我不想使用边界来定位对象。

在AndroidViewClient中,有一些方法可以查找具有其属性的特定对象。有没有办法在特定的层次结构下找到对象。就像是: findViewByHierarchy(attr1:val1, attr2:val2, attr3:val3 ),其中Id2是UI树中Id1的子级,依此类推。如果没有,是否有任何可用的解决方法可以实现相同的目标?

1 个答案:

答案 0 :(得分:2)

所有findView*()方法都会收到root参数,该参数指示搜索所在树的根。

例如:

def findViewById(self, viewId, root="ROOT", viewFilter=None):
    '''
    Finds the View with the specified viewId.

    @type viewId: str
    @param viewId: the ID of the view to find
    @type root: str
    @type root: View
    @param root: the root node of the tree where the View will be searched
    @type: viewFilter: function
    @param viewFilter: a function that will be invoked providing the candidate View as a parameter
                       and depending on the return value (C{True} or C{False}) the View will be
                       selected and returned as the result of C{findViewById()} or ignored.
                       This can be C{None} and no extra filtering is applied.

    @return: the C{View} found or C{None}
    '''

此外,您可以使用

自行遍历View树
def traverse(self, root="ROOT", indent="", transform=View.__str__, stream=sys.stdout):
    '''
    Traverses the C{View} tree and prints its nodes.

    The nodes are printed converting them to string but other transformations can be specified
    by providing a method name as the C{transform} parameter.

    @type root: L{View}
    @param root: the root node from where the traverse starts
    @type indent: str
    @param indent: the indentation string to use to print the nodes
    @type transform: method
    @param transform: a method to use to transform the node before is printed
    '''