1

I was wondering if there was any method to use adb commands to simulate touch on the connected device by identifying the element based on its text? Eg: i want to go to device settings using adb, and touch a particular option based on the text like 'Display', 'Sim Card Manager' etc. Once i am in that settings, i need to again touch some options using their text.

I have used the UI Automator Viewer to get the 'resource-id' and 'text' for the elements on the screen. Can they be used to target the touch location?

Can this be done using adb? or is there any other tool that can be used? I needed it to be a script so that it can be run from command prompt and does the required job.

Currently, i am using the below commands for opening the settings, and navigating through options and selecting them, but this is unreliable, because different android versions have different UI layouts :

adb shell am start -S com.android.settings/.Settings
adb shell input keyevent KEYCODE_DPAD_RIGHT
adb shell input keyevent KEYCODE_DPAD_DOWN
adb shell input keyevent ENTER

Any help is appreciated.

Thanks in advance.

EDIT

This question was marked duplicate for being as same as another question which asked Is there a way to get current activity's layout and views via adb?

My question however, is different. I know how to get the 'text', 'resource-id', and 'class-name' from a UI layout by using automator. I just needed to know if i could simulate touches on the UI of the device to touch that particular area, if i know the text written? Eg: Touching the 'Settings' icon, or touching the 'On/Off' toggle switch?

EDIT

Thanks everyone for the help! I have found a solution to what i was looking for. So if you want to touch something on the screen using its text, or to automate a task without having to use 'adb shell input keyevents' to navigate through the screen, you could just download and use the uiautomator (Python wrapper of Android uiautomator test tool.), and simulate the touch on UI using the text.

Eg:

from uiautomator import device as d

d.press.home()
d(text="Settings").click()

The above code presses the home button, and clicks/touches the place on the UI where 'Settings' is written, i.e if you have the 'Settings' icon on your homepage. I found this solution easier when compared to Appium because you could just run the script and voila!

You can also use uiautomator library in combination with subprocess if you want to run adb commands too.

Also, if there is a language with characters other than English written on the UI, you could just use the UI Automation Viewer tool and copy the text and paste it in your program and use a supported encoding method for the python script to run.

Community
  • 1
  • 1
User192828
  • 130
  • 2
  • 12
  • "but this is unreliable, because different android versions have different UI layouts" -- that will affect your UiAutomator stuff too. Manufacturers routinely modify the Settings app. You are not going to be able to come up with some universal approach. "Can they be used to target the touch location?" -- through a UiAutomator test suite, yes. I am not aware of other options. – CommonsWare Mar 22 '17 at 20:34

1 Answers1

1

welcome to the world of Automation testing (Even if you don't really want to test anything).

For Android you can go and use Espresso or Appium (Probably Appium would be easier for a novice). These Tools will let you automate any scenario you would like to do on an android device.

Good luck.

David Ep
  • 270
  • 1
  • 6