Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ADB to access a particular UI control on the screen

Tags:

Is it possible for adb shell console to access an Android app's specific button using its id? or text?

I'm trying to automate the button click on the device. This is a web app accessed from a browser. So if I have that button id can I send an action to that button?

like image 398
Siddharthan Asokan Avatar asked Sep 20 '13 20:09

Siddharthan Asokan


People also ask

How do I target a specific device on ADB?

Target a device by serial number Use the -s option followed by a device name to select on which device the adb command should run. The -s options should be first in line, before the command.

How do I access files using ADB?

Open cmd type adb shell then press enter. Type ls to view files list.

How do I open ADB commands?

Platform List is part of Android SDK and the best way to find the location is to open SDK manager and get the path. Appearance & Behaviour --> System Settings --> Android SDK You can get the path where SDK is installed and can edit the location as well. Show activity on this post. Type adb shell.


1 Answers

UI automator gives resource IDs, text, and the bounds of the UI element. An XML viewer or Chrome browser can be used to get a better view of the file.

adb pull $(adb shell uiautomator dump | grep -oP '[^ ]+.xml') /tmp/view.xml

The UI element's bounds can be be extracted and the mid point can be calculated. Replace text= with resource-id= or content-desc= as needed.

coords=$(perl -ne 'printf "%d %d\n", ($1+$3)/2, ($2+$4)/2 if /text="MY_BUTTON_TEXT"[^>]*bounds="\[(\d+),(\d+)\]\[(\d+),(\d+)\]"/' /tmp/view.xml)

Now we have the coordinates of the UI element's center in $coords and we just need to send a tap event.

adb shell input tap $coords
like image 189
apricot Avatar answered Oct 06 '22 16:10

apricot