如何通过MonkeyRunner点击android程序的某个视图?

发布于 2024-11-29 04:10:28 字数 121 浏览 2 评论 0原文

我想使用 MonkeyRunner 来测试我的 android 程序对具有不同屏幕分辨率的设备列表的兼容性。我需要单击一个视图,但对于不同的分辨率,该视图的位置不同。我怎样才能获得它的位置或做其他事情来点击它? 需要你的帮助!

I want to use MonkeyRunner to test the compatibility of my android program for a list of devices with different screen resolutions. I need to click a view, but the view is not in the same position for different resolutions. How can I get the position of it or do something else to click it?
NEED your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

妖妓 2024-12-06 04:10:28

我知道有点晚了,但是你可以使用android sdk中的hierarchyviewer来获取视图id。

然后,在您的脚本中,使用以下内容:

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice  
from com.android.monkeyrunner.easy import EasyMonkeyDevice, By  

device = MonkeyRunner.waitForConnection()  
easy_device = EasyMonkeyDevice(device)  

# Start your android app

# touch the view by id

easy_device.touch(By.id('view_id'), MonkeyDevice.DOWN_AND_UP)

稍后编辑:感谢 dtmilano 和 AndroidViewClient,我能够根据需要对视图进行单击。链接在这里:https://github.com/dtmilano/AndroidViewClient

I know it's a little late, but you can use hierarchyviewer in android sdk to get the view id.

Then, in your script, use this:

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice  
from com.android.monkeyrunner.easy import EasyMonkeyDevice, By  

device = MonkeyRunner.waitForConnection()  
easy_device = EasyMonkeyDevice(device)  

# Start your android app

# touch the view by id

easy_device.touch(By.id('view_id'), MonkeyDevice.DOWN_AND_UP)

Later edit: thanks to dtmilano and AndroidViewClient, I was able to do the clicks on views as needed. The link is here: https://github.com/dtmilano/AndroidViewClient

夜光 2024-12-06 04:10:28

不幸的是,这对于 MonkeyRunner 来说实际上是不可能的。一种选择是使用 device.getProperty("display.width")device.getProperty("display.height")device.getProperty("display .密度") 并尝试使用它们以某种方式找出视图在哪里。另一种选择是使用 Sikuli 之类的工具尝试单击视图。

编辑(此答案最初发布一年后):现在可以使用 https:// 执行您最初想要的操作github.com/dtmilano/AndroidViewClient

Unfortunately this is not really possible with MonkeyRunner. One option is to use device.getProperty("display.width"),device.getProperty("display.height") and device.getProperty("display.density") and try to use those to somehow figure out where the view is. Another option would be to use a tool like Sikuli to try to click on the view.

Edit (one year after this answer was initially posted): It is now possible to do what you initially wanted with https://github.com/dtmilano/AndroidViewClient

今天小雨转甜 2024-12-06 04:10:28

与上面的 someHuman 类似,我使用两个函数根据我最初编写脚本的设备和我当前使用的任何设备的分辨率差异来转换点击坐标。

首先我获取当前设备的 x 和 y 像素宽度。

CurrentDeviceX = float(device.getProperty("display.width"))
CurrentDeviceY = float(device.getProperty("display.height"))

然后我定义一个用于转换 x 和 y 坐标的函数。您可以看到下面的函数是为 1280 x 800 的设备编写的。

def transX(x):
''' (number) -> intsvd
TransX takes the x value supplied from the original device
and converts it to match the resolution of whatever device 
is plugged in
'''
OriginalWidth = 1280;
#Get X dimensions of Current Device
XScale = (CurrentDeviceX)/(OriginalWidth)
x = XScale * x
return int(x)


def transY(y):
''' (number) -> int
TransY takes the y value supplied from the original device
and converts it to match the resolution of whatever device 
is plugged in.
'''
OriginalHeight = 800;
#Get Y dimensions of Current Device
YScale = (CurrentDeviceY)/(OriginalHeight)
y = YScale * y
return int(y)

然后我可以在脚本中创建点击事件时使用这些函数。

请注意,这种方法

device.touch(transX(737), transY(226), 'DOWN_AND_UP')

远非完美,只有当您的应用程序利用锚定来根据屏幕尺寸调整 UI 时,它才有效。这是我在 UI ID 不可用时制作可在多个设备上运行的脚本的快速而肮脏的方法。

Similar to what someoneHuman said above, I use two functions that convert tap coordinates based on the difference in the resolution of the device I originally wrote the script for and whatever device I am currently using.

First I get the current devices x and y pixel width.

CurrentDeviceX = float(device.getProperty("display.width"))
CurrentDeviceY = float(device.getProperty("display.height"))

Then I define a function for converting x and y coordinates. You can see that the functions below were written for a device that is 1280 x 800.

def transX(x):
''' (number) -> intsvd
TransX takes the x value supplied from the original device
and converts it to match the resolution of whatever device 
is plugged in
'''
OriginalWidth = 1280;
#Get X dimensions of Current Device
XScale = (CurrentDeviceX)/(OriginalWidth)
x = XScale * x
return int(x)


def transY(y):
''' (number) -> int
TransY takes the y value supplied from the original device
and converts it to match the resolution of whatever device 
is plugged in.
'''
OriginalHeight = 800;
#Get Y dimensions of Current Device
YScale = (CurrentDeviceY)/(OriginalHeight)
y = YScale * y
return int(y)

Then I can use these functions when creating tap events in my scripts.

example

device.touch(transX(737), transY(226), 'DOWN_AND_UP')

Please note that this approach is far from perfect, and it will only work if your app utilizes anchoring to adjust UI based on screen size. This is my quick and dirty approach to making scripts that work on multiple devices when UI IDs are unavailable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文