#--coding:utf-8
from com.android.monkeyrunner import MonkeyRunneras mr
from com.android.monkeyrunner.recorderimport MonkeyRecorder as recorder
device =mr.waitForConnection(3,'127.0.0.1:62001')
recorder.start(device)
#coding=utf8
#需要用到命令行参数列表因此导入sys
import sys
#导入monkeyrunner用来操作设备
from com.android.monkeyrunner importMonkeyRunner
#窗念一个命令哈希表,定义操作事件
CMD_MAP = {
'TOUCH': lambda device, arg: device.touch(**arg),
'DRAG': lambda device, arg: device.drag(**arg),
'PRESS': lambda device, arg: device.press(**arg),
'TYPE': lambda device, arg: device.type(**arg),
'WAIT': lambda device, arg: MonkeyRunner.sleep(**arg)
}
#处理录制脚本文件
#定义两个参数:
#scriptfile----录制脚本的文件名
#device--------当前设备
def process_file(scriptfile, device):
#读取文件信息
for line in scriptfile:
#文件以|进行分割
#把命令放入cmd中
#把命令操作之后的信息存入rest中
(cmd, rest) = line.split('|')
try:
#将字符串rest当成有效的表达式来求值并返回计算结果
rest = eval(rest)
except:
print 'unable to parse options'
continue
#判断命令是否在 CMD_MAP中
if cmd not in CMD_MAP:
print 'unknown command: ' + cmd
continue
#回放脚本中命令操作
CMD_MAP[cmd](device, rest)
def main():
#在命令行输入例如:
#monkeyrunnerapp_playback "文件名[可以带有路径]"
file = sys.argv[1]
#打开录制脚本
scriptfile = open(file, 'r')
#对连接的对象创建一个对象
device = MonkeyRunner.waitForConnection()
#调用回放函数,对脚本进行回放
process_file(scriptfile, device)
#关闭脚本文件
scriptfile.close();
#判断是否作为运行程序
#如果是运行main函数
if __name__ == '__main__':
main()
monkeyrunner monkey_recorder.py
Monkeyrunner monkey_playback.py 录制脚本的路径
联系客服