標籤:詳細 撥號 ref 擷取 and ima 否則 應該 標籤
本文為慕課網《Android App壓力測試》的學習筆記,視頻地址
http://www.imooc.com/video/13007
Monkey是發送偽隨機使用者事件的工具。
MonkeyScript實現自動化的測試的指令碼,是一組可以被Monkey識別的命令集合,可以完成重複固定的操作。不支援截屏。
MonkeyRunner提供一系列api操作。
Monkey與MonkeyRunner的區別:
- Monkey:在adb shell中,生產使用者或系統的偽隨機事件
- MonkeyRunner:通過API定義特定命令和事件控制裝置
MonkeyRunner APIs
MonkeyRunner的測試類型 :多裝置控制, 功能測試, 迴歸測試
實踐:(需配置android sdk和python環境)
第一步:usb資料線串連手機與電腦
第二步:cmd視窗輸入adb devices,若顯示類似則串連成功
第三步:安裝測試app
第四步:發送壓力指令,adb shell monkey 1000(隨機完成1000個指令)
顯示如下:
第五步:擷取測試應用程式套件名
adb logcat | grep START(擷取app執行的log資訊作為下一個抓取日誌中包括start標籤的輸出的輸入,grep 是linux下的命令,windows系統可以使用 findstr 替代 grep。或者執行adb shell進入到手機,在手機系統下,執行logcat | grep START。android系統是linux的,所以支援grep)
真機測試圖,與視頻中不一樣,所以建議用虛擬機器
紅色記號表示隱式intent的action,其命名規範是包名+intent.action+自訂。
虛擬機器測試圖,輸出會跟著操作而更新
第六步:給指定包打壓力
adb shell monkey -p 包名 1000
monkey進階參數的應用(參數均在包名後,可組合使用)
throttle參數,指定命令間隔
adb shell monkey - -throttle < milliseconds >
如C:\Users\Administrator>adb shell monkey -p com.android.calculator2 –throttle 1000 10
seed參數,指定隨機產生數的seed值,指定該值可讓產生同樣命令
adb shell monkey -s< seed >< event-count >
如:C:\Users\Administrator>adb shell monkey -p com.android.calculator2 -s 100 50
ps:視頻中多次執行,效果基本一樣,而且執行耗費時間只相差幾ms,但本人測試時效果大致一樣,耗費時間有的相差300ms,應該是電腦效能的問題
事件均能組合使用,百分比之和<=100即可
點擊事件,設定點擊事件百分比
adb shell monkey - -pct-touch< percent >
如percent設為100,表示Monkey只執行點擊事件
C:\Users\Administrator>adb shell monkey -v (可以把執行的具體操作列印出來)-p com.android.calculator2 –pct-touch 100 100
動作事件,設定動作事件百比
adb shell monkey - -pct-motion< percent >
基本導航事件,設定基本導航事件百分比,輸入裝置的上,下,左,右
adb shell monkey - -pct-nav< percent >
主要導航事件,設定主要導航事件百分比,相容中間鍵,返回鍵,菜單按鍵
adb shell monkey - -pct-majornav< percent >
系統導航事件,設定系統導航事件百分比,HOME,BACK,撥號鍵,音量鍵
adb shell monkey –pct-syskeys< percent >
啟動activity事件,設定啟動activity事件百分比
adb shell monkey –pct-appswitch< percent >
不常用事件,設定不常用事件百分比
adb shell monkey –pct-anyevent< percent >
崩潰事件,忽略崩潰和異常,否則遇到就停止測試
adb shell monkey –ignore-crashes< event-count >
逾時事件,忽略ANR,否則遇到就停止測試
adb shell monkey –ignore-timeouts< event-count >
CRASH結果析取
以crash開頭,具體內容和android studio裡一樣
ANR 結果析取
以anr開頭,或者adb shell,cd /data/anr/ ,ls可以看到traces.txt的檔案,more traces.txt可以查看詳細資料
MonkeyScript常用命令介紹
執行monkey指令碼的命令
adb shell monkey -f< scriptfile >< event-count>
指令碼命令:
- DispatchTrackball 軌跡球事件,配對使用
DispatchTrackball(long downtime,long eventide,int action,float x, float y,float pressure,float size,int metastate,float xprecision,float yprecision,int divice,int edgeflags)
action 0代表按下,1代表彈起,x,y代表座標點
DispatchPointer 點擊事件 ,配對使用
DispatchTrackball(long downtime,long eventide,int action,float x, float y,float pressure,float size,int metastate,float xprecision,float yprecision,int divice,int edgeflags)
action 0代表按下,1代表彈起,x,y代表座標點
DispatchString命令 輸入字串事件
DispatchString(String text)
LaunchActivity命令 啟動應用
LaunchActivity(package,Activity)
UserWait 等待事件
UserWait (millseconds)
DispatchPress 按下索引值
DispatchPress(int keycode) 如keycode 66=斷行符號鍵
執行個體:
1.使用android sdk/tools/uiautomatorviewer.bat工具對要測試的應用介面進行測量,得到控制項在介面的左上點座標,右下點座標
2.編寫指令碼(儲存為script檔案)
圖中LaunchActivity中第二個參數要把activity的action名寫全,即com.xxx.xxx.xxx.MainActivity
3.由於指令碼在電腦裡,monkey在手機裡,所以要先把指令碼複製到手機裡
adb push mooc.script /data/local/tmp/
可以進入目錄下,ls查看目錄,ll查看檔案建立時間
3.測試(此時在手機中,不用加adb shell)
monkey -f mooc.script 2
ps:要想能夠啟動對應應用,其activity在AndroidManifest檔案中的exported屬性要設定為true,才能在外部被開啟!!!
MonkeyRunner常用API(能截屏)
1.alert 警告框
void alert (String message,String title,String okTitle)
#!/user/bin/python#-*- UTF-8 -*-from com.android.monkeyrunner import MonkeyRunnerMonkeyRunner.alert("Hello","title","OK")
cmd進入Android sdk/tools目錄下,輸入monkeyrunner demo.py,(此時已經把demo.py放在了該目錄下,否則需加上路徑,如monkeyrunner D:\xx\demo.py)得到
2.waitForConnection 等待裝置串連,有多個device id,需指明具體哪個裝置
3.drag 拖動(tuple類似point)
drag(tuple start,tuple end,float duration,integer steps)
4.press 按鍵
press(String keycode,dictionary type)
5.startActivity 啟動應用
startActivity (package+’/’+activity)
6.touch 點擊
touch(integer x,integer y,integer type)
7.type 輸入
type(String message)
8.takeSnapShot 截屏
MonkeyImage API
1.sameAs 映像對比
boolean sameAs(MonkeyImage other,float percent)
2.writetoFile 映像儲存
void writeToFile (String path,String format)
實踐
#!/usr/bin/python#-*- UTF-8 -*-from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage#串連裝置,裝置名稱adb devices可以擷取device=MonkeyRunner.waitForConnection(3,"192.168.56.101:5555")#啟動App,sleep裡單位是秒device.startActivity("com.jikexueyuan.animationartdemo/com.jikexueyuan.animationartdemo.MainActivity")MonkeyRunner.sleep(2)#點擊device.touch(100,100,"DOWN_AND_UP")MonkeyRunner.sleep(1)....#截屏image=device.takeSnapshot()image.writeToFile(‘./test.png‘,‘png‘)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
若要能迴圈測試,需要python指令碼,並不會。。。。。。
Android App壓力測試之Monkey