monkeyrunner使用小心得

Android SDK提供的monkeyrunner(不是Monkey)用来做功能性测试还是比较给力的,而且使用也很容易上手,按着使用文档来就可以。

使用过程中,发现gesture的模拟不是那么好用,也就是drag()方法。在HOME下,倒没问题,能看到屏幕拖动动作,但是在自己应用中没有反应。通过logcat发现实际触发的是OnGestureListener.onLongPress () 这个接口, 所以需要把他禁掉,调用setIsLongpressEnabled(false)即可。这个在模拟双击的时候也是如此,发现monkeyrunner的press() 也会被onLongPress ()截走。但在实际使用自己的应用中,并没有这个情况,即在setIsLongpressEnabled (true)的时候,拖动和双击都没问题,并没有触发onLongPress()。

没看过 monkeyrunner的源码,这只是从log得出的一个解决方法,供参考。如果有别的解决方法,希望讨论讨论。

另外,在monkeyrunner中尽量避免对于坐标的依赖,特别是针对需要多SDK版本、多种不同大小屏幕下的自动测试(不知道是不是能动态通过getPropery(display.width/height)来调整,这个还没研究过)。而且大多数情况下,确实也可以避免(只要这个操作是可以通过模拟器按钮来完成):

1)比如对话框不同按钮点击,这个可以通过先LEFT _KEY 和ENTER来模拟。

2)比如菜单选择,根据菜单个数和需要选择的菜单位置,也可以用LEFT_KEY, DOWN_KEY来定位, 等等。

当然对于那些没有焦点的组件点击,就没什么好的办法了。

针对单个设备的测试,可以用recorder类来录制测试用例即可。

----------------

更新,刚才看了一下最新的MonkeyRunner代码,已经开始实现用id来和应用交互了,而不是通过坐标,那就更好用了,贴一段里面的README。

com.android.monkeyrunner.easy contains classes intended to make it easier
to interact with applications using the MonkeyRunner framework. Instead of
referencing a button or input box by x,y coordinate, they can be referenced
by identifier, as in the following Python example:

##############################################################################

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

# Connect to the current device.
device = MonkeyRunner.waitForConnection()

# Use the EasyMonkey API, all methods on device are available in easy_device.
easy_device = EasyMonkeyDevice(device)

if not easy_device.visible(By.id('id/all_apps_button')):
    raise Error('Could not find the "all apps" button')

print "Location of element:", easy_device.locate(By.id('id/all_apps_button'))

easy_device.touch(By.id('id/all_apps_button'), 'DOWN_AND_UP')

##############################################################################

WARNING: This API is under development and may change without notice.


评论 25
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值