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