I verified this bug using the following script on the Temperature Converter (3500_10_source) from Packt:
device, serialno = ViewClient.connectToDeviceOrExit()
viewclient = ViewClient(device, serialno)
menuButton = viewclient.findViewWithContentDescriptionOrRaise('More options')
prefButton = viewclient.findViewWithTextOrRaise('Preferences')
print prefButton.getCenter() # (900, 323)
What happens is that we get an X value that is ridiculous for the menu item. I'm trying to press the Preferences button, but the X value for the touch() command is 900 on a device with only 768 width. I think that the adjustment of adding the virtual window to the actual x is causing this problem.
For what it's worth, the following code worked as a workaround for me:
prefButton = viewclient.findViewWithTextOrRaise('Preferences') #@UndefinedVariable
x = prefButton.getX()
y = prefButton.getY()
w = prefButton.getWidth()
h = prefButton.getHeight()
device.touch(x+w/2, y+h/2, MonkeyDevice.DOWN_AND_UP) #@UndefinedVariable
Thanks for your report.
This varies across devices and Android versions.
Which ones are you using?
This was on an LG Nexus 4 with Android 4.2.2
AndroidViewClient fails to find center of menu items in Action Bar
- Modified window offsets for API-17 (should be tested on previous
Give the experimental branch experimental-api-17-coords a try (https://github.com/dtmilano/AndroidViewClient/tree/experimental-api-17-coords).
I tested on a Nexus 7 (4.2.2) and worked correctly.
As a side note, original TemperatureConverter's manifest was modified to include
<uses-sdk android:minSdkVersion="15" />
and the script used to reproduce your problem is:
#! /usr/bin/env monkeyrunner
Copyright (C) 2012 Diego Torres Milano
Created on Feb 28, 2013
@author: diego (based on adzellman script)
# This must be imported before MonkeyRunner and MonkeyDevice,
# otherwise the import fails.
# PyDev sets PYTHONPATH, use it
for p in os.environ['PYTHONPATH'].split(':'):
if not p in sys.path:
from com.dtmilano.android.viewclient import ViewClient
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
vc = ViewClient(*ViewClient.connectToDeviceOrExit())
menuButton = vc.findViewWithContentDescriptionOrRaise('More options')
prefButton = vc.findViewWithTextOrRaise('Preferences')
Hi all, could you please give a few examples of AndroidViewClients steady configuration? I mean device model and Android APi that get right coordinates always. For example, I'm currently working with a Nexus S and 4.0.4 version (insecure of course) but when I touch a button it actually touches the button above the target-button. Same for Nexus One with 2.3.6. Thanks you and all the other contributors for your work.
I had the same issue so I fixed it my self. I can send you my file but it's not a very clean fix.
This is my solution:
So I added an optional argument for touch() named "spe" set to "False" so that when i'm on a menu i use "touch(spe=True)" and on all the other case i just use "touch()"
The issue is fixed since 4.2.1 (Fixed coordinates problem in View.getXY() for UiAutomator)