Monkeyrunner 问题

一:monkeyunner控制多台设备

根据monkeyrunner提供的函数:

MonkeyDevicewaitForConnection(float timeout,string deviceId)

Tries to make a connection between the monkeyrunner backend and the specified device or emulator.

Arguments
timeoutThe number of seconds to wait for a connection. The default is to wait forever.
deviceIdA regular expression that specifies the serial number of the device or emulator. See the topicAndroid Debug Bridge for a description of device and emulator serial numbers.
Returns
  • A MonkeyDevice instance for the device or emulator. Use this object to control and communicate with the device or emulator.

 

:\>adb devices
ist of devices attached
123456789ABCDEF device
mulator-5554 device

:\>monkeyrunner
ython 2.5.0 (Release_2_5_0:6476, Jun 16 2009, 13:33:26)
Java HotSpot(TM) Client VM (Oracle Corporation)] on java1.7.0_05
>> from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
>> device1 = MonkeyRunner.waitForConnection(1,"emulator-5554")
>> device2 = MonkeyRunner.waitForConnection(1,"0123456789ABCDEF")
就可以控制多台设备或者虚拟机。

通过id触摸屏幕

其中id可以通过和monkeyrunner同级目录即tools\hierarchyviewer.bat获得

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

device = MonkeyRunner.waitForConnection(1.0,'0123456789ABCDEF')

device.startActivity(component='com.android.browser/.BrowserActivity')
MonkeyRunner.sleep(5)

easy_device = EasyMonkeyDevice(device)
easy_device.touch(By.id('id/one'),MonkeyDevice.DOWN_AND_UP)

三:通过id触摸屏幕,多个Id名字相同

用hierarchyviewer.bat去获取id,但是同目录很多id都相同,比如setting下所有TextView id都是id/title

解决方法一:

device.press('KEYCODE_ENTER', 'DOWN_AND_UP')

evice.press('KEYCODE_DPAD_DOWN', 'DOWN_AND_UP')

evice.press('KEYCODE_DPAD_UP', 'DOWN_AND_UP')

device.press('KEYCODE_DPAD_RIGHT', 'DOWN_AND_UP')

device.press('KEYCODE_DPAD_LEFT', 'DOWN_AND_UP')

来控制上下左右移动。

解决方法二:

当两个ID名称相同时,可以使用层级进行定位

easy_device.touch(By.id('id/parent_button'),MonkeyDevice.DOWN_AND_UP,By.id('id/current_button'),MonkeyDevice.DOWN_AND_UP)

但是这个方法自己试过没有成功,也许是父子id没有找对。

解决方法三:ViewClient

下载viewclient包:

http://sourceforge.net/projects/wrapeasymonkey/files/wrapEasyMonkey/1.0/

解压添加到环境变量,ANDROID_VIEW_CLIENT_HOME,具体操作参考文章:

http://blog.csdn.net/tzh2009/article/details/8152382

可以用findViewWithAttribute获取text变量来控制。

PS:速度真的很慢,不是很喜欢。

import re
import sys
import os
import string
import locale
# this must be imported before MonkeyRunner and MonkeyDevice,
# otherwise the import fails

try:
    ANDROID_VIEW_CLIENT_HOME =  os.environ['ANDROID_VIEW_CLIENT_HOME']
except KeyError:
    print >>sys.stderr, "%s: ERROR: ANDROID_VIEW_CLIENT_HOME not set in environment" % __file__
    sys.exit(1)
sys.path.append(ANDROID_VIEW_CLIENT_HOME + '/src')
sys.setdefaultencoding('utf-8')

from com.dtmilano.android.viewclient import ViewClient
device = MonkeyRunner.waitForConnection()
vc = ViewClient(device)

view = vc.findViewWithText('@@@@@')

view.touch()

四:利用monkeyrunner往文件写log

#write file to pc
log = open('d:\\MonkeyruunerTestlog\\tmp.txt', 'w')
log.write("strings")
log.close()

五:获取测试DUT的系统时间和PC时间

#get device date&time 
date = device.shell('date +%Y%m%d%H%M%S')
#get the PC time
import time
date = time.strftime('%Y%m%d%H%M%S')

六:图片比较

用MonkeyRunner.loadImageFromFile()来添加电脑上已经存在的图片,与设备上的截图相比较;

imageA = MonkeyRunner.loadImageFromFile('D:\\WORKSF\\SAQtools\\Monkeyrunner\\scripts\\test12.png','png')
image=device.takeSnapshot()
#image.writeToFile('D:\\WORKSF\\SAQtools\\Monkeyrunner\\scripts\\test12A.png','png')
print image.sameAs(imageA, 0.95)
if image.sameAs(imageA, 0.95):
    log12.write("Download the doc file success, test is PASS \n")
 
else:
    log12.write("Fail to download the doc file or the doc file not open successed,please check")


 

 

 

 

 

 

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 13
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值