官方文檔網址是: file:///D:/tool/new_androidsdk/docs/tools/help/monkey.html
The basic syntax is:
$ adb shell monkey [options]<event-count>
With no options specified, the Monkey will launch in a quiet (non-verbose) mode, and will send events to any (and all) packages installed on your target. Here is a more typical command line, which will launch your application and send 500 pseudo-random events to it:
$ adb shell monkey -p your.package.name -v 500
例1: 在shell中輸入命令(以測試com.hskj.memo為例):monkey -p com.hskj.memo -s 100 -v -v -v 500(測試com.hskj.memo包,以seed為100的隨機排序<設定seed方便下次進行同樣事件的測試,不
過要求測試的起始位置相同,比如起始位置為案頭或者為某一個Activity的某一個狀態>,測試結果為第三層級的顯示,即為最詳細的顯示)
例2: Monkey測試出現錯誤後一般查錯步驟:
1。找到是哪個monkey的哪個動作出的錯:
2。查看monkey出錯前的一些事件動作,並手動執行該動作。
3。若還不能找出,可以使用命令monkey -p com.hskj.memo -s 100 -v -v -v --throttle 2000 500
例3:
1.設定每一個動作執行後停頓5秒鐘或者別的一個時間段,直到出錯,此時可以有更充分的時間來查看每一個動作的執行過程
2。找到錯誤後,進行原始碼更改,更改錯誤後進行 相同的seed值 及 相同的起始位置 再進行一次相同事件的測試。
3。若錯誤不再發生,則進行別的SEED值的測試,直到經過千錘百鍊都沒有錯再發生它才算是一個穩定性足夠的程式。
--------------------------------monkey 測試載入器 --------------------------------------------------------------
monkey 測試載入器
來源:http://blog.163.com/bobile45@126/blog/static/9606199220124882231634/
一、Monkey測試簡介
Monkey測試是Android平台自動化測試的一種手段,通過Monkey程式類比使用者觸控螢幕幕、滑動Trackball、按鍵等操作來對裝置上的程式進行壓
力測試,檢測程式多久的時間會發生異常。
二、Monkey程式介紹
1) Monkey程式由Android系統內建,使用Java語言寫成,在Android檔案系統中的存放路徑是:/system/framework/monkey.jar;
2) Monkey.jar程式是由一個名為“monkey”的Shell指令碼來啟動執行,shell指令碼在Android檔案系統中的存放路徑是:/system/bin/monkey;
這樣就可以通過在CMD視窗中執行: adb shell monkey {+命令參數}來進行Monkey測試了。
三、Monkey命令的簡單協助
要擷取Monkey命令內建的簡單協助,在CMD中執行命令:
adb shell monkey –help
四、Monkey命令參數介紹
1) 參數: -p
參數-p用於約束限制,用此參數指定一個或多個包(Package,即App)。指定
包之後,Monkey將只允許系統啟動指定的APP。如果不指定包,Monkey將允許系統啟動裝置中的所有APP。
* 指定一個包: adb shell monkey -p com.htc.Weather 100
說明:com.htc.Weather為包名,100是事件計數(即讓Monkey程式類比100次隨機使用者事件)。
* 指定多個包:adb shell monkey -p com.htc.Weather –p com.htc.pdfreader -p com.htc.photo.widgets 100
* 不指定包:adb shell monkey 100
說明:Monkey隨機啟動APP並發送100個隨機事件。
* 要查看裝置中所有的包,在CMD視窗中執行以下命令:
>adb shell
#cd data/data
#ls
2) 參數: -v
用於指定反饋資訊層級(資訊層級就是日誌的詳細程度),總共分3個層級,分別對應的參數如下表所示:
記錄層級 Level 0
樣本 adb shell monkey -p com.htc.Weather –v 100
說明 預設值,僅提供啟動提示、測試完成和最終結果等少量資訊
記錄層級 Level 1
樣本 adb shell monkey -p com.htc.Weather –v -v 100
說明 提供較為詳細的日誌,包括每個發送到Activity的事件資訊
記錄層級 Level 2
樣本 adb shell monkey -p com.htc.Weather –v -v –v 100
說明 最詳細的日誌,包括了測試中選中/未選中的Activity資訊
3)參數: -s
用於指定偽隨機數產生器的seed值,如果seed相同,則兩次Monkey測試所產生的事件序列也相同的。
* 樣本:
Monkey測試1:adb shell monkey -p com.htc.Weather –s 10 100
Monkey 測試2:adb shell monkey -p com.htc.Weather –s 10 100
兩次測試的效果是相同的,因為類比的使用者操作序列(每次操作按照一定的先後順序所組成的一系列操作,即一個序列)是一樣的。操作序
列雖 然是隨機產生的,但是只要我們指定了相同的Seed值,就可以保證兩次測試產生的隨機操作序列是完全相同的,所以這個操作序列偽隨
機的;
4) 參數: --throttle <毫秒>
用於指定使用者操作(即事件)間的時延,單位是毫秒;
* 樣本:adb shell monkey -p com.htc.Weather –throttle 3000 100
5) 參數: --ignore-crashes
用於指定當應用程式崩潰時(Force & Close錯誤),Monkey是否停止運行。如果使用此參數,即使應用程式崩潰,Monkey依然會發送事件,直
到事件計數完成。
* 樣本1:adb shell monkey -p com.htc.Weather --ignore-crashes 1000
測試過程中即使Weather程式崩潰,Monkey依然會繼續發送事件直到事件數目達到1000為止;
* 樣本2:adb shell monkey -p com.htc.Weather 1000
測試過程中,如果Weather程式崩潰,Monkey將會停止運行。
6) 參數: --ignore-timeouts
用於指定當應用程式發生ANR(Application No Responding)錯誤時,Monkey是否停止運行。如果使用此參數,即使應用程式發生ANR錯誤,
Monkey依然會發送事件,直到事件計數完成。
7) 參數: --ignore-security-exceptions
用於指定當應用程式發生許可錯誤時(如認證許可,網路許可等),Monkey是否停止運行。如果使用此參數,即使應用程式發生許可錯誤,
Monkey依然會發送事件,直到事件計數完成。
8) 參數: --kill-process-after-error
用於指定當應用程式發生錯誤時,是否停止其運行。如果指定此參數,當應用程式發生錯誤時,應用程式停止運行並保持在目前狀態(注意:
應用程式僅是靜止在發生錯誤時的狀態,系統並不會結束該應用程式的進程)。
9) 參數: --monitor-native-crashes
用於指定是否監視並報告應用程式發生崩潰的本地代碼。
10) 參數: --pct-{+事件類別目錄} {+事件類別目錄百分比}
用於指定每種類別事件的數目百分比(在Monkey事件序列中,該類事件數目佔總事件數目的百分比)
參數:
使用說明:
樣本:
--pct-touch {+百分比}
調整觸摸事件的百分比(觸摸事件是一個down-up事件,它發生在螢幕上的某單一位置)
adb shell monkey -p com.htc.Weather --pct-touch 10 1000
--pct-motion {+百分比}
調整動作事件的百分比(動作事件由螢幕上某處的一個down事件、一系列的偽隨機事件和一個up事件組成)adb shell monkey -p
com.htc.Weather --pct-motion 20 1000
--pct-trackball {+百分比}
調整軌跡事件的百分比(軌跡事件由一個或幾個隨機的移動組成,有時還伴隨有點擊)
adb shell monkey -p com.htc.Weather --pct-trackball 30 1000
--pct-nav {+百分比}
調整“基本”導航事件的百分比(導航事件由來自方向輸入裝置的up/down/left/right組成)
adb shell monkey -p com.htc.Weather --pct-nav 40 1000
--pct-majornav {+百分比}
調整“主要”導航事件的百分比(這些導航事件通常引發圖形介面中的動作,如:5-way鍵盤的中間按鍵、回退按鍵、菜單按鍵)
adb shell monkey -p com.htc.Weather --pct-majornav 50 1000
--pct-syskeys {+百分比}
調整“系統”按鍵事件的百分比(這些按鍵通常被保留,由系統使用,如Home、Back、Start Call、End Call及音量修飾鍵)
adb shell monkey -p com.htc.Weather --pct-syskeys 60 1000
--pct-appswitch {+百分比}
調整啟動Activity的百分比。在隨機間隔裡,Monkey將執行一個startActivity()調用,作為最大程度覆蓋包中全部Activity的一種方法
adb shell monkey -p com.htc.Weather --pct-appswitch 70 1000
--pct-anyevent {+百分比}
調整其它類型事件的百分比。它包羅了所有其它類型的事件,如:按鍵、其它不常用的裝置按鈕、等等
adb shell monkey -p com.htc.Weather
--pct -anyevent 100 1000* 指定多個類型事件的百分比:
adb shell monkey -p com.htc.Weather --pct-anyevent 50 --pct-appswitch 50 1000
注意:各事件類型的百分比總數不能超過100%;
--------------------------------------------這個就是官方文檔------------------------------------------------------
UI/Application Exerciser Monkey
The Monkey is a program that runs on your emulator or device and generates pseudo-random streams of user events such as clicks, touches, or gestures, as well as a number of system-level events. You can use the Monkey to stress-test applications that you are developing, in a random yet repeatable manner.
Overview
The Monkey is a command-line tool that that you can run on any emulator instance or on a device. It sends a pseudo-random stream of user events into the system, which acts as a stress test on the application software you are developing.
The Monkey includes a number of options, but they break down into four primary categories:
- Basic configuration options, such as setting the number of events to attempt.
- Operational constraints, such as restricting the test to a single package.
- Event types and frequencies.
- Debugging options.
When the Monkey runs, it generates events and sends them to the system. It also watches the system under test and looks for three conditions, which it treats specially:
- If you have constrained the Monkey to run in one or more specific packages, it watches for attempts to navigate to any other packages, and blocks them.
- If your application crashes or receives any sort of unhandled exception, the Monkey will stop and report the error.
- If your application generates an application not responding error, the Monkey will stop and report the error.
Depending on the verbosity level you have selected, you will also see reports on the progress of the Monkey and the events being generated.
Basic Use of the Monkey
You can launch the Monkey using a command line on your development machine or from a script. Because the Monkey runs in the emulator/device environment, you must launch it from a shell in that environment. You can do this by prefacing adb shell
to each command, or by entering the shell and entering Monkey commands directly.
The basic syntax is:
$ adb shell monkey [options]<event-count>
With no options specified, the Monkey will launch in a quiet (non-verbose) mode, and will send events to any (and all) packages installed on your target. Here is a more typical command line, which will launch your application and send 500 pseudo-random events to it:
$ adb shell monkey -p your.package.name -v 500
Command Options Reference
The table below lists all options you can include on the Monkey command line.
Category |
Option |
Description |
General |
--help |
Prints a simple usage guide. |
-v |
Each -v on the command line will increment the verbosity level. Level 0 (the default) provides little information beyond startup notification, test completion, and final results. Level 1 provides more details about the test as it runs, such as individual events being sent to your activities. Level 2 provides more detailed setup information such as activities selected or not selected for testing. |
Events |
-s <seed> |
Seed value for pseudo-random number generator. If you re-run the Monkey with the same seed value, it will generate the same sequence of events. |
--throttle <milliseconds> |
Inserts a fixed delay between events. You can use this option to slow down the Monkey. If not specified, there is no delay and the events are generated as rapidly as possible. |
--pct-touch <percent> |
Adjust percentage of touch events. (Touch events are a down-up event in a single place on the screen.) |
--pct-motion <percent> |
Adjust percentage of motion events. (Motion events consist of a down event somewhere on the screen, a series of pseudo-random movements, and an up event.) |
--pct-trackball <percent> |
Adjust percentage of trackball events. (Trackball events consist of one or more random movements, sometimes followed by a click.) |
--pct-nav <percent> |
Adjust percentage of "basic" navigation events. (Navigation events consist of up/down/left/right, as input from a directional input device.) |
--pct-majornav <percent> |
Adjust percentage of "major" navigation events. (These are navigation events that will typically cause actions within your UI, such as the center button in a 5-way pad, the back key, or the menu key.) |
--pct-syskeys <percent> |
Adjust percentage of "system" key events. (These are keys that are generally reserved for use by the system, such as Home, Back, Start Call, End Call, or Volume controls.) |
--pct-appswitch <percent> |
Adjust percentage of activity launches. At random intervals, the Monkey will issue a startActivity() call, as a way of maximizing coverage of all activities within your package. |
--pct-anyevent <percent> |
Adjust percentage of other types of events. This is a catch-all for all other types of events such as keypresses, other less-used buttons on the device, and so forth. |
Constraints |
-p <allowed-package-name> |
If you specify one or more packages this way, the Monkey will only allow the system to visit activities within those packages. If your application requires access to activities in other packages (e.g. to select a contact) you'll need to specify those packages as well. If you don't specify any packages, the Monkey will allow the system to launch activities in all packages. To specify multiple packages, use the -p option multiple times — one -p option per package. |
-c <main-category> |
If you specify one or more categories this way, the Monkey will only allow the system to visit activities that are listed with one of the specified categories. If you don't specify any categories, the Monkey will select activities listed with the category Intent.CATEGORY_LAUNCHER or Intent.CATEGORY_MONKEY. To specify multiple categories, use the -c option multiple times — one -c option per category. |
Debugging |
--dbg-no-events |
When specified, the Monkey will perform the initial launch into a test activity, but will not generate any further events. For best results, combine with -v, one or more package constraints, and a non-zero throttle to keep the Monkey running for 30 seconds or more. This provides an environment in which you can monitor package transitions invoked by your application. |
--hprof |
If set, this option will generate profiling reports immediately before and after the Monkey event sequence. This will generate large (~5Mb) files in data/misc, so use with care. See Traceview for more information on trace files. |
--ignore-crashes |
Normally, the Monkey will stop when the application crashes or experiences any type of unhandled exception. If you specify this option, the Monkey will continue to send events to the system, until the count is completed. |
--ignore-timeouts |
Normally, the Monkey will stop when the application experiences any type of timeout error such as a "Application Not Responding" dialog. If you specify this option, the Monkey will continue to send events to the system, until the count is completed. |
--ignore-security-exceptions |
Normally, the Monkey will stop when the application experiences any type of permissions error, for example if it attempts to launch an activity that requires certain permissions. If you specify this option, the Monkey will continue to send events to the system, until the count is completed. |
--kill-process-after-error |
Normally, when the Monkey stops due to an error, the application that failed will be left running. When this option is set, it will signal the system to stop the process in which the error occurred. Note, under a normal (successful) completion, the launched process(es) are not stopped, and the device is simply left in the last state after the final event. |
--monitor-native-crashes |
Watches for and reports crashes occurring in the Android system native code. If --kill-process-after-error is set, the system will stop. |
--wait-dbg |
Stops the Monkey from executing until a debugger is attached to it. |
Except as noted, this content is licensed under Creative Commons Attribution 2.5. For details and restrictions, see the Content License.
About Android | Legal | Support