MonkeyRunner: easy way to determine coordinates for MonkeyDevice touch command?
Asked Answered
N

7

9

I've started creating a MonkeyRunner script. This is going ok, but whenever I add a MonkeyDevice::touch command, I have to determine the input coordinates by trial-and-error. Basically I guess at the coordinates I want to touch and see if those coordinates result in the button touch I'm trying to test. That works, but it's a slow process. Is there anyway to determine the coordinates of UI controls, perhaps from the layout XML files?

Nicolais answered 3/2, 2012 at 20:15 Comment(0)
N
7

I found how to do it. Use the Pixel Perfect view within Eclipse to determine the x & y coordinates of the UI element. Here's a quick overview: 1) Eclipse must be running 2) Your Android device must be connected (either the real device via ADB, or the emulator) 3) Run the hierarchy viewer (in /tools) 4) Select "Inspect screenshot" The Pixel Perfect view will launch automatically. Just place the cross-hairs on the UI element. The x and y coordinates, along with the RGB values, are displayed below.

Here's the URL that got me started: http://developer.android.com/guide/developing/debugging/debugging-ui.html#pixelperfect

Nicolais answered 7/2, 2012 at 15:39 Comment(0)
H
4

This post (monkeyrunner: interacting with views), may give you and idea of how to obtain the View's coordinates using AndroidViewClient.

Hayseed answered 7/2, 2012 at 6:46 Comment(0)
M
2

Most of the Android versions you can enable pointer location in Settings->Developer Options. Once you enable it, it is easy to find out the (x,y) coordinates.

Mister answered 17/5, 2012 at 10:4 Comment(1)
This is absolutely the easiest way to do this on any recent Android version. If your Android version is too old, e.g. Gingerbread, then there is an app, Dev Tools, available through the Android Market, aka, Play Store, which also has a developer options/settings menu with the ability to show where the pointer is the same as the developer options built-in with ICS and later.Atalee
B
2

you can also use the HierarchyViewer tool in your AndroidSDK>tools folder to take screenshots of the current screen and examine that image pixel by pixel to get exact coordinates.

Bradshaw answered 2/8, 2012 at 14:28 Comment(0)
A
0

For devices older than Android 4.0, see the paragraph following this one. Android 4.0 and later include Settings->Developer options->Pointer location toggle which toggles a transparent ribbon across the top of the device screen with coordinates, velocities and touch-pressure readings including swipe tracks and x/y crosshairs for the current touch location. This is a lot easier than using alternatives such as Monkey Recorder and other means. In Android 4.2 and later, Developer options is hidden from the Settings menu and must be enabled by going to Settings->About tablet and tapping on Build number seven times. One can only presume that Android hid Developer options because of the increasingly user-experience-affecting options it contains and the number of consumer calls/complaints to device makers from people who played with it or whose children played with it.

In older versions which may not include a Pointer location toggle, there is an app on the Play Store (aka Android Market), Developers Tools. See link here: https://play.google.com/store/apps/details?id=com.ggb.development It will show up with a gear icon and the caption Dev Tools on a device and provide similar functionality which Dev Tools in an AVD (Android Virtual Device) has. That includes Dev Tools ->Development settings>No Pointer Location/Pointer Location radio button toggle. Setting that toggle to Pointer Location provides the exact same functionality built-in with Android 4.0 and later. The same application also has a more limited pointer setting at Dev Tools->Pointer Location which limits pointer location to only a blank screen.

Enjoy!

Atalee answered 4/11, 2013 at 20:7 Comment(0)
H
0

Create a xyz.py file with below code and connect the device and run it in terminal like monkeyrunner xyz.py, then you will get your device in pc, then u click on any button in the recorder it will give you the coordinates, after that explore it to any file and you can use the coordinates.

from com.android.monkeyrunner import MonkeyRunner as mr
from com.android.monkeyrunner.recorder import MonkeyRecorder as recorder
device = mr.waitForConnection()
recorder.start(device)
Hammack answered 20/4, 2016 at 7:25 Comment(0)
F
0

I found an adb approach. Use adb shell getevent -l to get a list of events, grep for ABS_MT_POSITION (gets the line with touch events in hex) and finally use awk to get the relevant hex values, strip them of zeros and convert hex to decimal that monkey runner uses. This is all done with the following:

adb shell getevent -l | grep ABS_MT_POSITION --line-buffered | awk '{a = substr($0,54,8); sub(/^0+/, "", a); b = sprintf("0x%s",a); printf("%d\n",strtonum(b))}'

This continuously prints the x and y coordinates in the terminal only when you press on the device.

Fidel answered 6/5, 2020 at 21:58 Comment(3)
error: grep is not recognized as an internal or external command, operable program or batch file. in android 10 emulatorLorettelorgnette
@Lorettelorgnette grep is a linux shell program and looks like you need to install it first. If you're using mac os you'd need to install it using brew install grep.Fidel
So I am getting this error because I am using Windows! I thought it came from android emulator!Lorettelorgnette

© 2022 - 2024 — McMap. All rights reserved.