'AndroidViewClient: unable to dump correctly the view

I'm trying to use CulebraTester2 to dump the Window hierarchy. I'm testing an app where the text of some TextView changes: the first time I call dumpWindowHierarchy the obtained JSON content corresponds to the text shown, when the text changes and I try to call again dumpWindowHierarchy, the text of the TextView remains unaltered in the JSON even if it actually has changed in the app. I've noticed instead that the bounds change. I need to get the actual text shown by the app, is there a way? Thanks.

UPDATE

I've tried using the mrin9 provided api and also using python vc.uiAutomatorHelper.ui_device.dump_window_hierarchy() and the result is the same: the text is not updated.

A snippet of the JSON:

{
    "id": 38,
    "parent": 36,
    "text": "Si pensa che porti fortuna mangiare:",
    "package": "com.mypackage",
    "checkable": false,
    "clickable": false,
    "index": 2,
    "content_description": "",
    "focusable": false,
    "resource_id": "questionDataQuestionText",
    "enabled": true,
    "password": false,
    "long_clickable": false,
    "long_text": "android.view.View_questionDataQuestionText id=38 parent=36",
    "clazz": "android.view.View",
    "scrollable": false,
    "selected": false,
    "checked": false,
    "focused": false,
    "bounds": [
        150,
        648,
        930,
        1010
    ],
    "children": []
}


Solution 1:[1]

I added the clock widget to the home screen as it has a changing text that we can use as an example (otherwise we both should install the same app which might not be possible).

clock widget

Then, using this script I could verify the text changes.

#! /usr/bin/env python3

import time

from com.dtmilano.android.viewclient import ViewClient

def search(resource_id, children):
    if not children:
        return
    for child in children:
        if child.resource_id == resource_id:
            print(child.text)
            return
        search(resource_id, child.children)


if __name__ == '__main__':
    device, serialno = ViewClient.connectToDeviceOrExit()
    vc = ViewClient(device, serialno, useuiautomatorhelper=True)

    while True:
        h = vc.uiAutomatorHelper.ui_device.dump_window_hierarchy()
        search('com.google.android.deskclock:id/clock', h.children)
        time.sleep(10)

and produces this output when run

?? CulebraTester2 server should have been started and port redirected.
6:04
6:04
6:04
6:04
6:04
6:05
6:05
6:05
6:05
6:05
6:05
6:06
6:06
6:06

You can probably adapt the script and test your app.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Diego Torres Milano