MonkeyRunner可以录制和回放脚本
前置条件:
电脑连接手机,输入adb devices 看看返回是否手机设备列表(我是真机,模拟器也可以)
配置好安卓sdk和Python环境
step:
1、启动录制脚本:cmd,输入命令
monkeyrunner monkey_recorder.py
会弹出一个MonkeyRecord窗口界面该窗口的功能:
注意:如果录制时候,点击,发现设备和窗口不同步,ctrl+c,输入y,再次输入录制命令(黑屏不好使)
- 1、可以自动显示手机当前的界面
- 2、自动刷新手机的最新状态
- 3、点击手机界面即可对手机进行操作,同时会反应到真机,而且会在右侧插入操作脚本
- 4:、wait: 用来插入下一次操作的时间间隔,点击后即可设置时间,单位是秒
Press a Button:用来确定需要点击的按钮,包括menu、home、search,以及对按钮的press、down、up属性
Type Something:用来输入内容到输入框
Fling:用来进行拖动操作,可以向上、下、左、右,以及操作的范围
Export Actions:用来导出脚本,不需要后缀名,也可以添加后缀名.mr
Refresh Display:用来刷新手机界面,估计只有在断开手机后,重新连接时才会用到
以学院的app为例,生成的脚本后缀为mr
生成的monkey脚本如下:
demo.mr
WAIT|{'seconds':2.0,}
TOUCH|{'x':924,'y':724,'type':'downAndUp',}
WAIT|{'seconds':2.0,}
TOUCH|{'x':756,'y':1704,'type':'downAndUp',}
WAIT|{'seconds':2.0,}
TOUCH|{'x':337,'y':288,'type':'downAndUp',}
WAIT|{'seconds':2.0,}
TOUCH|{'x':263,'y':640,'type':'downAndUp',}
WAIT|{'seconds':2.0,}
TOUCH|{'x':519,'y':660,'type':'downAndUp',}
WAIT|{'seconds':2.0,}
TOUCH|{'x':985,'y':1536,'type':'downAndUp',}
WAIT|{'seconds':2.0,}
TOUCH|{'x':985,'y':1536,'type':'downAndUp',}
WAIT|{'seconds':2.0,}
TOUCH|{'x':985,'y':1536,'type':'downAndUp',}
WAIT|{'seconds':2.0,}
TOUCH|{'x':985,'y':1536,'type':'downAndUp',}
WAIT|{'seconds':2.0,}
TOUCH|{'x':985,'y':1536,'type':'downAndUp',}
WAIT|{'seconds':2.0,}
TOUCH|{'x':985,'y':1536,'type':'downAndUp',}
WAIT|{'seconds':2.0,}
TOUCH|{'x':519,'y':660,'type':'downAndUp',}
WAIT|{'seconds':2.0,}
TOUCH|{'x':985,'y':1536,'type':'downAndUp',}
WAIT|{'seconds':2.0,}
TOUCH|{'x':985,'y':1536,'type':'downAndUp',}
WAIT|{'seconds':2.0,}
TOUCH|{'x':985,'y':1536,'type':'downAndUp',}
WAIT|{'seconds':2.0,}
TOUCH|{'x':985,'y':1536,'type':'downAndUp',}
WAIT|{'seconds':2.0,}
TOUCH|{'x':985,'y':1536,'type':'downAndUp',}
WAIT|{'seconds':2.0,}
TOUCH|{'x':985,'y':1536,'type':'downAndUp',}
WAIT|{'seconds':2.0,}
TOUCH|{'x':519,'y':660,'type':'downAndUp',}
WAIT|{'seconds':2.0,}
TOUCH|{'x':985,'y':1536,'type':'downAndUp',}
WAIT|{'seconds':2.0,}
TOUCH|{'x':985,'y':1536,'type':'downAndUp',}
WAIT|{'seconds':2.0,}
TOUCH|{'x':985,'y':1536,'type':'downAndUp',}
WAIT|{'seconds':2.0,}
TOUCH|{'x':985,'y':1536,'type':'downAndUp',}
WAIT|{'seconds':2.0,}
TOUCH|{'x':985,'y':1536,'type':'downAndUp',}
WAIT|{'seconds':2.0,}
TOUCH|{'x':985,'y':1536,'type':'downAndUp',}
WAIT|{'seconds':2.0,}
TYPE|{'message':'wangqiang_cyou',}
WAIT|{'seconds':2.0,}
TOUCH|{'x':421,'y':820,'type':'downAndUp',}
WAIT|{'seconds':2.0,}
TYPE|{'message':'13704042785',}
WAIT|{'seconds':2.0,}
TOUCH|{'x':999,'y':1028,'type':'downAndUp',}
WAIT|{'seconds':2.0,}
TOUCH|{'x':789,'y':1188,'type':'downAndUp',}
WAIT|{'seconds':2.0,}
TOUCH|{'x':108,'y':1744,'type':'downAndUp',}
WAIT|{'seconds':2.0,}
DRAG|{'start':(432,1420),'end':(432,284),'duration':1.0,'steps':10,}
WAIT|{'seconds':2.0,}
DRAG|{'start':(432,1420),'end':(432,284),'duration':1.0,'steps':10,}
WAIT|{'seconds':2.0,}
DRAG|{'start':(432,284),'end':(432,1420),'duration':1.0,'steps':10,}
WAIT|{'seconds':2.0,}
TOUCH|{'x':756,'y':1684,'type':'downAndUp',}
WAIT|{'seconds':2.0,}
TOUCH|{'x':563,'y':1480,'type':'downAndUp',}
WAIT|{'seconds':2.0,}
TOUCH|{'x':597,'y':1708,'type':'downAndUp',}
WAIT|{'seconds':2.0,}
生成的脚本建议放在sdk的tool目录下:
2、回放脚本,cmd输入命令:
monkeyrunner monkey_playback.py demo.mr
如果遇到不能回放的问题,需要关闭当前录制时的cmd窗口,重新打开cmd窗口执行回放操作,或者ctrl+c,重新输入命令即可(黑屏好像不好使,我自己试验的)
备注:以上路径都是绝对路径,录制后的脚本可以进行二次更改,而且每一步操作需要有时间间隔,这样才能保证测试的正确性。