Android自动化学习笔记之MonkeyRunner:MonkeyRunner的录制和回放
----------------------------------------------------------------------------------------------------------------------------
小记: 一直都是用公司自己研发的自动化工具,对市面开源的自动化工具知之甚少,所以开始自学开源的自动化工具。
初步学习中,难免会有疏漏和想不到的地方,我会及时更新,每天进步一点点。
**********************
2014-10-14:初版
2014-10-15:更新细节和Q&A
**********************
------------------------------------------------------------------------------------------------------------------------------
MonkeyRunner的使用方法,前面的文章很详细了,在学习过程中发现,它竟然还有录制和回放功能,速速记录下来。
1.新建文件monkey_recorder.py,复制以下代码:
#!/usr/bin/env monkeyrunner # Copyright 2010, The Android Open Source Project # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from com.android.monkeyrunner import MonkeyRunner as mr from com.android.monkeyrunner.recorder import MonkeyRecorder as recorder device = mr.waitForConnection() recorder.start(device)
2. 新建文件monkey_playback.py,复制以下代码:
#!/usr/bin/env monkeyrunner # Copyright 2010, The Android Open Source Project # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import sys from com.android.monkeyrunner import MonkeyRunner # The format of the file we are parsing is very carfeully constructed. # Each line corresponds to a single command. The line is split into 2 # parts with a | character. Text to the left of the pipe denotes # which command to run. The text to the right of the pipe is a python # dictionary (it can be evaled into existence) that specifies the # arguments for the command. In most cases, this directly maps to the # keyword argument dictionary that could be passed to the underlying # command. # Lookup table to map command strings to functions that implement that # command. CMD_MAP = { 'TOUCH': lambda dev, arg: dev.touch(**arg), 'DRAG': lambda dev, arg: dev.drag(**arg), 'PRESS': lambda dev, arg: dev.press(**arg), 'TYPE': lambda dev, arg: dev.type(**arg), 'WAIT': lambda dev, arg: MonkeyRunner.sleep(**arg) } # Process a single file for the specified device. def process_file(fp, device): for line in fp: (cmd, rest) = line.split('|') try: # Parse the pydict rest = eval(rest) except: print 'unable to parse options' continue if cmd not in CMD_MAP: print 'unknown command: ' + cmd continue CMD_MAP[cmd](device, rest) def main(): file = sys.argv[1] fp = open(file, 'r') device = MonkeyRunner.waitForConnection() process_file(fp, device) fp.close(); if __name__ == '__main__': main()
3. 运行(先连接上设备或者启动模拟器)
输入 monkeyrunner yourpath\monkey_recorder.py
启动之后如图:
4.开始录制
接下来,要完成一系列操作:从主界面进入Setting->向下滑动->查看about phone->返回到主界面
先说明下:
比如我打开settings,那么就用鼠标点击一下右下角settings,相应的模拟器或者设备就会打开settings。
右上角显示 Tap touchscreen at(415,710) ,也就是点击了setting是的坐标---如果当前图片显示的与手机不一致,点击Refresh Display即可。
再比如我要向上滑动,点击Fling,选择NORTH,然后点OK,手机就会做一次向上滑动的操作。
下图是我已经做完了一系列操作,回到了主界面
note:
因为press a button方法里没有BACK,手动写入也是一样的。
5.保存脚本--选择Export Actions,选择要保存的地方和文件名,比如我叫做checkAboutphone。
6.回放脚本
先看一下刚保存的文件(由于电脑太破,模拟器反应太慢,下面是我重新录制的脚本,步骤一样,只是相比之前每一步操作之后增加了几秒的等待,),打开如下
TOUCH|{'x':408,'y':701,'type':'downAndUp',} WAIT|{'seconds':10.0,} DRAG|{'start':(192,640),'end':(192,128),'duration':1.0,'steps':10,} WAIT|{'seconds':5.0,} DRAG|{'start':(192,640),'end':(192,128),'duration':1.0,'steps':10,} WAIT|{'seconds':5.0,} TOUCH|{'x':180,'y':765,'type':'downAndUp',} WAIT|{'seconds':10.0,} PRESS|{'name':'BACK','type':'downAndUp',} WAIT|{'seconds':10.0,} PRESS|{'name':'BACK','type':'downAndUp',}
怎么执行呢,cmd里输入
monkeyrunner monkey_playback.py 刚保存的文件
monkeyrunner monkey_playback.py checkAboutPhone.mr
然后看着模拟器或者设备吧,会自动执行一遍刚刚的操作。
Q&A:
自学过程中,遇到过几个问题。
1. Q: monkeyrecoder的显示界面,没有实时刷新。
A:点击一下Refresh Display就可以了。
2. Q:monkeyrecoder启动时候,抛出异常,没有启动起来。
A:查看异常信息可知,是adb端口被占用,关闭占用的程序,再试。也有一种情况是电脑