Monkeyrunner 问题

来源:互联网 发布:淘宝上怎么改差评 编辑:程序博客网 时间:2024/05/18 00:19

一: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 devicesist of devices attached123456789ABCDEF devicemulator-5554 device:\>monkeyrunnerython 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,MonkeyImagefrom com.android.monkeyrunner.easy import EasyMonkeyDevice,Bydevice = 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 reimport sysimport osimport stringimport locale# this must be imported before MonkeyRunner and MonkeyDevice,# otherwise the import failstry:    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 pclog = 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 timedate = 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")