1、Monkey的使用python
Monkey使用起来比较简单,简而言之就是模拟手机点击效果,随机发送N个点击动做给手机,主要对于程序的稳定和承受压力的测试。android
1.首先链接上你的手机或者启动模拟器;shell
2.运行CMD,进入命令输入框;express
3.输入 adb shell monkey -p your.package -vvv 500 > e:\exception.txt (可能有朋友会问为何不进入adb shell 以后才操做呢? 由于进入adb shell 中他没有可操做的权限了,也就不能建立exception.txt 文件,会报错。)apache
4.最后运行结束后能够在exception.txt 文档中查看你的运行结果。app
解释:less
-p 后面也就是app的包名,若是你是在测试你本身的app,尽可能在manifest.xml中去复制,也能够adb shell 中 进入 data/data 用命令 ls 列出来,找出你须要的包。测试
-vvv 这个是输出三种运行的三种状态,就是详细输出事件等级,这个3个v就是输出等级1至3的全部事件。ui
至于500 则是随机发送500个点击事件给app点击使用。this
存储到exception.txt是为了更好的查看测试结果。
其中也能够加上 --throttle 3000 每执行一次有效的事件后休眠3秒,若是又须要也能够添加上去。
2、MonkeyRunner的使用
这个是通用运行脚本文件来测试的,目的性强,更有针对性,更容易控制。
1.首先链接上你的手机或者启动一个模拟器;
2.运行CMD,进入你的sdk的tools文件夹下,在下面有一个monkeyrunner.bat;
3.而后就是建立你的脚本文件,里面有你写好的一系列的操做。(注意:这里你的脚本文件必定要是UTF-8文件格式,否则会出错。)
新建一个testmonkeyrunner.py
#导入咱们须要用到的包和类而且起别名 import sys from com.android.monkeyrunner import MonkeyRunner as mr from com.android.monkeyrunner import MonkeyDevice as md from com.android.monkeyrunner import MonkeyImage as mi #connect device 链接设备 #第一个参数为等待链接设备时间 #第二个参数为具体链接的设备 device = mr.waitForConnection(1.0,'ca985d92') if not device: print >> sys.stderr,"fail" sys.exit(1) #定义要启动的Activity componentName='com.org.hl.john.monkeytest/.MainActivity' #启动特定的Activity device.startActivity(component=componentName) mr.sleep(3.0) #do someting 进行咱们的操做 #输入 helloworld device.type('helloworld') #输入回车 device.press('KEYCODE_ENTER') #return keyboard #device.press('KEYCODE_BACK') #------ #takeSnapshot截图 mr.sleep(3.0) result = device.takeSnapshot() #save to file 保存到文件 result.writeToFile('./shot.png','png');
这里借用了一下其余朋友的代码,表示很是感谢!
在命令行输入
monkeyrunner testmonkeyrunner.py
就会根据你脚本里设置的包名和activity启动应用,而后根据脚本里的一系列的操做,跟着操做。
记录和回放
使用脚本,启用一个可视化操做的界面
新建一个(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)
而后在命令行运行:
monkeyrunner monkey_recorder.py
就会弹出一个可视化的操做界面
而后能够在手机上进入你的app,如图:
跟着就能够在app里面作你想要测试的操做,必须是在电脑上这个界面上操做。
Wait | 等待时间 |
Press a Button | 发送,MENU,HOME,or SEARCH 按钮.Press,Down,or Up事件 |
Type Something | 发送一些字符串 |
Fling | 用来操做虚拟键盘 ![]() |
Export Action | 将咱们的脚本导出来 |
Refresh Display | 刷新当前界面 |
当你操做完成后能够选择export action 导出咱们的脚本,我保存的名称是action.mr,保存到tools目录下
导出后打开能够看到里面一系列的操做,可是这样是用不了的,还必须写到可运行的脚本里。
新建脚本:monkey_playback.py (也保存到tools目录下)
#!/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()
写这个脚本仍是必须先研究一下Phthon这个语言,楼主暂时尚未研究,因此暂时还不能解释上面的代码。。。。。
输入:
monkeyrunner play_black.py action.mr
运行便可,以前你操做的动做就重复实现了。
以上仅供参考!
--throttle 3000