Skip to content

Loading…

AndroidViewClient fails to find center of menu items in Action Bar (Android 4.2) #22

Open
adzellman opened this Issue · 7 comments

4 participants

@adzellman

I verified this bug using the following script on the Temperature Converter (3500_10_source) from Packt:

device, serialno = ViewClient.connectToDeviceOrExit()
MonkeyRunner.sleep(5)
viewclient = ViewClient(device, serialno)
viewclient.dump()
MonkeyRunner.sleep(1)
menuButton = viewclient.findViewWithContentDescriptionOrRaise('More options')
menuButton.touch()
MonkeyRunner.sleep(1)
viewclient.dump()
MonkeyRunner.sleep(1)
prefButton = viewclient.findViewWithTextOrRaise('Preferences')
print prefButton.getCenter() # (900, 323)
prefButton.touch()
MonkeyRunner.sleep(1)

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.

@adzellman

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
@dtmilano
Owner

Thanks for your report.

This varies across devices and Android versions.
Which ones are you using?

@adzellman

This was on an LG Nexus 4 with Android 4.2.2

@dtmilano dtmilano added a commit that referenced this issue
@dtmilano Issue #22
AndroidViewClient fails to find center of menu items in Action Bar
(Android 4.2)

- Modified window offsets for API-17 (should be tested on previous
versions)
ad4151c
@dtmilano
Owner

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)
'''


import sys
import os
import getopt

# This must be imported before MonkeyRunner and MonkeyDevice,
# otherwise the import fails.
# PyDev sets PYTHONPATH, use it
try:
    for p in os.environ['PYTHONPATH'].split(':'):
        if not p in sys.path:
            sys.path.append(p)
except:
    pass

try:
    sys.path.append(os.path.join(os.environ['ANDROID_VIEW_CLIENT_HOME'], 'src'))
except:
    pass

from com.dtmilano.android.viewclient import ViewClient
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice

vc = ViewClient(*ViewClient.connectToDeviceOrExit())

menuButton = vc.findViewWithContentDescriptionOrRaise('More options')
menuButton.touch()
MonkeyRunner.sleep(1)
vc.dump()
MonkeyRunner.sleep(1)
prefButton = vc.findViewWithTextOrRaise('Preferences')
print prefButton.getCenter()
prefButton.touch()
MonkeyRunner.sleep(1)
@damianadalid

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.

@elbajo

Hi diego,
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:

  • When I have the issue (motsly on menus) i don't use getXY() (who doesn't give the good data) but i use getX() and getY().
  • When I don't have the issue I keep using getXY() because getX() and getY() don't give the good data.

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()"

@elbajo

The issue is fixed since 4.2.1 (Fixed coordinates problem in View.getXY() for UiAutomator)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.