user846316
user846316

Reputation: 6331

Locating UI objects with same attribute values except bounds

While automating the test cases, I got into situation where multiple objects are showing same attribute values in UiAutomator except bounds (i.e. x,y location on the display). I do not want to use the bounds to locate the object.

In AndroidViewClient, there are methods to find specific object with its attributes. Is there any way to locate the object under the specific hierarchy. Something like: findViewByHierarchy(attr1:val1, attr2:val2, attr3:val3 ), where Id2 is child of Id1 in the UI tree and so on. If not, is there any workaround available to achieve the same?

Upvotes: 1

Views: 218

Answers (1)

Diego Torres Milano
Diego Torres Milano

Reputation: 69198

All the findView*() methods receive a root parameter which indicates the root of the tree where the search takes place.

For example:

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}
    '''

Furthermore, you can traverse the View tree yourself using

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
    '''

Upvotes: 2

Related Questions