关于monkeyrunner的介绍不说了,sdk的文档上面有。
这里记录一下看完文档之后的实际操作过程,当初我看完了文档,摸索了半天才把脚本跑起来的。
1,附上一段示例脚本:
# Imports the monkeyrunner modules used by this program from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage # Connects to the current device, returning a MonkeyDevice object device = MonkeyRunner.waitForConnection(deviceId='SH14FPL02740') imgOld = device.takeSnapshot() imgNew = device.takeSnapshot() while imgNew.sameAs(imgOld, 0.9): device.touch(150, 225, 'DOWN_AND_UP') MonkeyRunner.sleep(0.5) device.touch(150, 350, 'DOWN_AND_UP') #MonkeyRunner.sleep(0.5) imgNew = device.takeSnapshot() #print 'xxxxx' imgOld.writeToFile('d:/old.png', 'png') imgNew.writeToFile('d:/new.png', 'png')
2,将上述脚本保存为d:/test.py3,命令行中运行命令: monkeyrunner d:/test.py注意的地方:1,已经安装了python2,执行脚本的命令中,要使用脚本文件的绝对路径。(关于python我不大熟,可能设置一下环境变量也可以让命令识别到当前路径)
2,脚本中waitForConnection的参数的意思是:当有多个设备或模拟器开启的时候,指定要连接的设备标识3,while循环条件的后面有个冒号,循环体的语句之前有个tab(我对python语法不熟)
运行方法:
1,运行脚本:
monkeyrunner file_full_path
2,交互式输入命令行:
cmd>monkeyrunner
>>>from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
>>>device = MonkeyRunner.waitForConnection()
>>>device.installPackage("../samples/android-10/ApiDemos/bin/Apidemos.apk")
支持多个设备:
device = MonkeyRunner.waitForConnection(deviceId='SH14FPL02740')
支持一个设备时,可以不写参数:
device = MonkeyRunner.waitForConnection()