转载自http://blog.csdn.net/tzh2009/article/details/8152459
一:monkeyunner控制多台设备
根据monkeyrunner提供的函数:
MonkeyDevice
waitForConnection(floattimeout,stringdeviceId)
Tries to make a connection between themonkeyrunner
backend and the specified device or emulator.
Arguments
timeout | The number of seconds to wait for a connection. The default is to wait forever. |
---|
deviceId | A
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
- 123456789ABCDEFdevice
- mulator-5554device
- :\>monkeyrunner
- ython2.5.0(Release_2_5_0:6476, Jun162009,13:33:26)
- JavaHotSpot(TM) Client VM (OracleCorporation)] on java1.7.0_05
- >>fromcom.android.monkeyrunnerimportMonkeyRunner, MonkeyDevice
- >> device1 = MonkeyRunner.waitForConnection(1,"emulator-5554")
- >> device2 = MonkeyRunner.waitForConnection(1,"0123456789ABCDEF")
- 就可以控制多台设备或者虚拟机。
二:通过id触摸屏幕
其中id可以通过和monkeyrunner同级目录即tools\hierarchyviewer.bat获得
- fromcom.android.monkeyrunnerimportMonkeyRunner,MonkeyDevice,MonkeyImage
- fromcom.android.monkeyrunner.easyimportEasyMonkeyDevice,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:速度真的很慢,不是很喜欢。
- importre
- importsys
- importos
- importstring
- importlocale
- try:
- ANDROID_VIEW_CLIENT_HOME = os.environ['ANDROID_VIEW_CLIENT_HOME']
- exceptKeyError:
- 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')
- fromcom.dtmilano.android.viewclientimportViewClient
- device = MonkeyRunner.waitForConnection()
- <p><spanclass="pln">view </span><spanclass="pun">=</span><spanclass="pln"> vc</span><spanclass="pun">.</span><spanclass="pln">findViewWithText</span><spanclass="pun">(</span><spanclass="str"><a href="mailto:'@@@@@'">'@@@@@'</a></span><spanclass="pun">)</span></p><p><spanclass="pun"></span><spanclass="pln">view</span><spanclass="pun">.</span><spanclass="pln">touch</span><spanclass="pun">()</span><spanclass="pln"></span></p>
四:利用monkeyrunner往文件写log
- log = open('d:\\MonkeyruunerTestlog\\tmp.txt','w')
- log.write("strings")
- log.close()
五:获取测试DUT的系统时间和PC时间
- date = device.shell('date +%Y%m%d%H%M%S')
- importtime
- 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()
- printimage.sameAs(imageA,0.95)
- ifimage.sameAs(imageA,0.95):
- log12.write("Download the doc file success,testis PASS \n")
- else:
- log12.write("Fail to download the doc file or the doc file not open successed,please check")