用monkeyrunner代替一些重复手工操作

关于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()











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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值