dtmilano / AndroidViewClient

Android ViewServer and ADB client
Apache License 2.0
1.63k stars 347 forks source link

Help me with incomplete escape \U at position 2 #300

Closed flyingmoon15 closed 2 years ago

flyingmoon15 commented 2 years ago

Hello dears!

I am quite new here as well as the programming environment.

I'm recently interested in Android automation and been trying to make AndroidViewClient work. But failed....

Everytime I try to run this ViewClient(*ViewClient.connectToDeviceOrExit()), i got 'incomplete escape \U at position 2' message.

I'm not sure if my running environment is okey to run or not.

So please please check mine!! (It will be sososo appreciated if I can use this awesome library)

My environment is

What I did was wrong? Please help me =( Plus. I attached a text file (the error messages resulted from running 'ViewClient(*ViewClient.connectToDeviceOrExit())' on Jupyter Notebook

error.txt

dtmilano commented 2 years ago

You are mixing stuff from different projects, that unfortunately have similar names.

The AdbClient you need to work with AndroidViewClient is this one: https://github.com/dtmilano/AndroidViewClient/blob/master/src/com/dtmilano/android/adb/adbclient.py#L195

flyingmoon15 commented 2 years ago

I don't still get what you mentioned above about AdbClient... and its link

However, I finally make it.

What I did was...

  1. Run 'Jupyter Notebook'
  2. Run 'Adb server' (Just 'SDK Platform Tools' installed from 'https://developer.android.com/studio/releases/platform-tools?hl=en'
  3. Open 'culebra'
  4. Copy all codes from 'culebra' file and paste them into Jupyter Notebook
  5. Run the codes
  6. Run vc = ViewClient(*ViewClient.connectToDeviceOrExit()) vc.traverse()
  7. Finally, it shows what I'm running on my phone!! yeah~

I had thought that 'ViewClient(*ViewClient.connectToDeviceOrExit())' could be run with my steps above before I wrote this question. But running the culebra code before ViewClient makes all work well!

dtmilano commented 2 years ago

I guess that copying the code culebra generated gave you the correct imports.

flyingmoon15 commented 2 years ago

Wow really thanks about your answer!

Finally, I got the code to run 'AndroidViewClient' !!!

import sys import getopt from com.dtmilano.android.viewclient import ViewClient, CulebraOptions optlist, args = getopt.getopt(sys.argv[1:], CulebraOptions.SHORT_OPTS, CulebraOptions.LONG_OPTS) sys.argv[1:] = args ViewClient(*ViewClient.connectToDeviceOrExit()).traverse()

Those codes are all I need to see the result which I wanted to see...

Can you please tell me how the codes above solved the problem? (error: incomplete escape \U at position 2)

dtmilano commented 2 years ago

What I meant is that you imported something different as AndClient and importing the wrong package or class may lead to those strange errors.

As you may have discovered already, culebra and Culebra GUI are really helpful to generate base script that you can later adapt to your needs.

The smallest script you can write using traverse could be something like this

#!  /usr/bin/env python3

from com.dtmilano.android.viewclient import ViewClient

ViewClient(*ViewClient.connectotoDeviceOrExit()).traverse(transform=print)

Feel free to ask any question.

flyingmoon15 commented 2 years ago

A... small question arised!

I want to make my phone receive calls automatically.

So I tried 'traverse' to get the id of 'receiving call button' like the below e.g. android.widget.Button com.samsung.android.dialer:id/dialer_menu_search

So when my phone is ringing, I did 'traverse'. But the result was 'RuntimeError: The views are being refreshed too frequently to dump.'

Is these any way to get 'resource-id'? So I can receive a call by the .touch() command when phone is ringing!

dtmilano commented 2 years ago

Can you explain your objective?

Do you want to wait in a loop util you detect a View with id com.samsung.android.dialer:id/dialer_menu_search and then click it?

flyingmoon15 commented 2 years ago

No... sorry my explanation was not good.

My objective is that I want to see the ID when my phone is ringing so I can click the button (receiving call button)

Like using a command (vc.findViewById('com.samsung.android.incallui:id/seven').touch())

dtmilano commented 2 years ago

I got that. My question was more in the lines of how are you going to wait for the incoming call?

Are you detecting that there's an incoming call when that Button appears?

If the screen is refreshing too fast for uiautomator to get the dump there are other alternative backends like CulebraTester2-public.

flyingmoon15 commented 2 years ago

hmm... I'm still fuzzy about what you just said. Simply saying.... How can I click 'receving call button' when a phone is ringing? At least, I need to know ID like com.samsung.android.incallui:id/seven. Thus, I can use a command 'vc.findViewById('com.samsung.android.incallui:id/seven').touch()' in order to click 'receving call button'. Anyway, I will try CulebraTester2-public. I don't know how to use it though :)

dtmilano commented 2 years ago

simple-calculator-test may give you some ideas about how to locate and touch on Buttons.

dtmilano commented 2 years ago

Assuming you have culebratester_client >= 2.0.19 and CulebraTester2-public >= 2.0.19-alpha installed and the service running on the device, that the Button you want to click has the text Answer, then

#! /usr/bin/env python3

import culebratester_client as ctc

by_selector = 'text@Answer'
timeout = 60000

api = ctc.DefaultApi(ctc.ApiClient())
api.ui_object2_oid_click_get(api.ui_device_wait_get(api.until_find_object_get(by_selector=by_selector).oid, timeout=timeout).oid)

will wait for 60 secs (you can extend it as you want) and when an incoming call is detected it will click on Answer.

The steps:

flyingmoon15 commented 2 years ago

Thanks for the answer! I couldn't try your last two answers due to dealing with something urgent. I will try them and let you know. Again, I really appreciate your answer!

dtmilano commented 2 years ago

No problem. Also check https://github.com/dtmilano/AndroidViewClient/blob/master/examples/phone-answer-incoming-call