dtmilano / CulebraTester2-public

CulebraTester: Snaky Android Testing
Apache License 2.0
139 stars 29 forks source link

Wildcards on Text Selector for findObjects #6

Closed bradmill closed 2 years ago

bradmill commented 2 years ago

Using the example code

oid=$(do_curl -f -X POST "${base_url}/uiDevice/findObject" \ -d "{'text':'${label}'}" | jq .oid)

is there a way that text: could use wildcard / partial match ?

Thanks

dtmilano commented 2 years ago

I would do something like this to simplify the use of the API as AndroidViewClient/culebra includes most of the cases:

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

from com.dtmilano.android.viewclient import ViewClient

device, serialno = ViewClient.connectToDeviceOrExit()

kwargs2 = {'useuiautomatorhelper': True}
vc = ViewClient(device, serialno, **kwargs2)

for o in vc.uiAutomatorHelper.findObjects(by_selector='text@$[A-Z].*'):
    print(vc.uiAutomatorHelper.getText(o.oid))

This is a simple example based on your request.

Here, we use AndroidViewClient bindings. We request the backend to be CulebraTester2-public by passing {'uiautomatorhelper': True} to the constructor. Notice that the default backend is adb, therefore we need to specify we want uiautomatorhelper (a synonim for CulebraTester2-public) and have the service already running on the device or emulator.

Then, we invoke findObjects (see API) passing a selector text with a value starting with $ which is then interpreted as a regular expression.

We loop over the list of objects found and we invoke getText passing the specific object id.

I run it on an emulator with the Clock as the top Activity to obtain these results:

{'text': 'Alarm'}
{'text': 'Clock'}
{'text': 'Timer'}
{'text': 'Stopwatch'}
{'text': 'Thu, Nov 25'}

If AndroidViewClient does not satisfy your needs my second recommendation would be to use CulebraTester2-client which is the lower level API python client used by AndroidViewClient.

And there's more, if you need bindings for another language other than python you can generate the client from the openapi.yaml spec.

Hope this helps. Feel free to follow up with more questions here or in Stackoverflow using tag androidviewclient.

Resources

bradmill commented 2 years ago

Thanks, very helpful