Android Monkey整理
[TOC]
簡(jiǎn)介
Monkey是Android中的一個(gè)命令行工具米诉,可以運(yùn)行在模擬器里或?qū)嶋H設(shè)備中浆熔。它向系統(tǒng)發(fā)送偽隨機(jī)的用戶事件流(如按鍵輸入、觸摸屏輸入蛛枚、手勢(shì)輸入等),實(shí)現(xiàn)對(duì)正在開發(fā)的應(yīng)用程序進(jìn)行壓力測(cè)試。Monkey測(cè)試是一種為了測(cè)試軟件的穩(wěn)定性培慌、健壯性的快速有效的方法。
Monkey命令包含下面4個(gè)命令選項(xiàng)夏哭。
- 常規(guī)選項(xiàng)
- 運(yùn)行約束
- 事件類型和頻率
- 調(diào)試選項(xiàng)
當(dāng)命令運(yùn)行的時(shí)候检柬,它會(huì)產(chǎn)生隨機(jī)的事件到Android系統(tǒng),然后監(jiān)聽系統(tǒng)的測(cè)試情況竖配,同時(shí)對(duì)一些條件進(jìn)行特俗對(duì)待何址。
- 當(dāng)你對(duì)一個(gè)或者多個(gè)設(shè)備進(jìn)行測(cè)試的時(shí)候,如果app跳轉(zhuǎn)大其他app进胯,則會(huì)停止運(yùn)行
- 當(dāng)程序發(fā)送了奔潰或者是一些列沒有處理的異常用爪,命令會(huì)停止同時(shí)報(bào)告錯(cuò)誤
- 當(dāng)程序發(fā)送ANR的時(shí)候,命令也會(huì)停止并報(bào)告錯(cuò)誤
基本使用
基本語法如下:
adb shell monkey [options] <event-count>
如果不指定options胁镐,Monkey將以無反饋模式啟動(dòng)偎血,并把事件任意發(fā)送到安裝在目標(biāo)環(huán)境中的全部包。下面是一個(gè)更為典型的命令行示例盯漂,它啟動(dòng)指定的應(yīng)用程序颇玷,并向其發(fā)送500個(gè)偽隨機(jī)事件:
adb shell monkey -p your.package.name -v 500
命令語法
General
--help
Prints a simple usage guide.
列出簡(jiǎn)單的用法。-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.命令行的每一個(gè) -v 將增加反饋信息的級(jí)別就缆。
Level 0(缺省值)除啟動(dòng)提示帖渠、測(cè)試完成和最終結(jié)果之外,提供較少信息竭宰。
Level 1提供較為詳細(xì)的測(cè)試信息空郊,如逐個(gè)發(fā)送到Activity的事件份招。
Level 2提供更加詳細(xì)的設(shè)置信息,如測(cè)試中被選中的或未被選中的Activity狞甚。
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.
用于指定偽隨機(jī)數(shù)生成器的seed值锁摔,如果seed相同,則兩次Monkey測(cè)試所產(chǎn)生的事件序列也相同的哼审。--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.
在事件之間插入固定延遲谐腰。通過這個(gè)選項(xiàng)可以減緩 Monkey 的執(zhí)行速度。如果不指定該選項(xiàng)棺蛛,Monkey 將不會(huì)被延遲怔蚌,事件將盡可能快地被產(chǎn)成。--pct-touch <percent>
Adjust percentage of touch events. (Touch events are a down-up event in a single place on the screen.)
調(diào)整觸摸事件的百分比(觸摸事件是一個(gè) down-up 事件旁赊,它發(fā)生在屏幕上的某單一位置)桦踊。--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.)
調(diào)整動(dòng)作事件的百分比(動(dòng)作事件由屏幕上某處的一個(gè) down 事件、一系列的偽隨機(jī)事件和一個(gè) up 事件組成)终畅。--pct-trackball <percent>
Adjust percentage of trackball events. (Trackball events consist of one or more random movements, sometimes followed by a click.)
調(diào)整軌跡事件的百分比(軌跡事件由一個(gè)或幾個(gè)隨機(jī)的移動(dòng)組成籍胯,有時(shí)還伴隨有點(diǎn)擊)。--pct-nav <percent>
Adjust percentage of "basic" navigation events. (Navigation events consist of up/down/left/right, as input from a directional input device.)
調(diào)整“基本”導(dǎo)航事件的百分比(導(dǎo)航事件由來自方向輸入設(shè)備的 up/down/left/right 組成)离福。--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.)
調(diào)整“主要”導(dǎo)航事件的百分比(這些導(dǎo)航事件通常引發(fā)圖形界面中的動(dòng)作杖狼,如:5-way鍵盤的中間按鍵、回退按鍵妖爷、菜單按鍵)--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.)
調(diào)整“系統(tǒng)”按鍵事件的百分比(這些按鍵通常被保留蝶涩,由系統(tǒng)使用,如 Home絮识、Back绿聘、Start Call、End Call 及音量控制鍵)次舌。--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.
調(diào)整啟動(dòng) Activity 的百分比熄攘。在隨機(jī)間隔里,Monkey 將執(zhí)行一個(gè) startActivity() 調(diào)用彼念,作為最大程度覆蓋包中全部Activity 的一種方法挪圾。--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.
調(diào)整其它類型事件的百分比。它包羅了所有其它類型的事件逐沙,如:按鍵哲思、其它不常用的設(shè)備按鈕、等等吩案。
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.如果用此參數(shù)指定了一個(gè)或幾個(gè)包也殖,Monkey 將只允許系統(tǒng)啟動(dòng)這些包里的 Activity。如果你的應(yīng)用程序還需要訪問其它包里的 Activity (如選擇取一個(gè)聯(lián)系人),那些包也需要在此同時(shí)指定忆嗜。如果不指定任何包,Monkey 將允許系統(tǒng)啟動(dòng)全部包里的 Activity崎岂。要指定多個(gè)包捆毫,需要使用多個(gè) -p 選項(xiàng),每個(gè) -p 選項(xiàng)只能用于一個(gè)包冲甘。
指定一個(gè)包: adb shell monkey -p com.htc.Weather 100
說明:com.htc.Weather為包名绩卤,100是事件計(jì)數(shù)(即讓Monkey程序模擬100次隨機(jī)用戶事件)。
指定多個(gè)包:adb shell monkey -p com.htc.Weather –p com.htc.pdfreader -p com.htc.photo.widgets 100
不指定包:adb shell monkey 100
說明:Monkey隨機(jī)啟動(dòng)APP并發(fā)送100個(gè)隨機(jī)事件江醇。-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.
如果用此參數(shù)指定了一個(gè)或幾個(gè)類別濒憋,Monkey 將只允許系統(tǒng)啟動(dòng)被這些類別中的某個(gè)類別列出的 Activity。如果不指定任何類別陶夜,Monkey 將選 擇下列類別中列出的 Activity: Intent.CATEGORY_LAUNCHER 或Intent.CATEGORY_MONKEY凛驮。要指定多個(gè)類別,需要使用多個(gè) -c 選項(xiàng)条辟,每個(gè) -c 選 項(xiàng)只能用于一個(gè)類別黔夭。
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.
設(shè)置此選項(xiàng),Monkey將執(zhí)行初始啟動(dòng)羽嫡,進(jìn)入到一個(gè)測(cè)試Activity本姥,然后不會(huì)再進(jìn)一步生成事件。為了得到最佳結(jié)果杭棵,把它與-v婚惫、一個(gè)或幾個(gè)包約 束、以及一個(gè)保持Monkey運(yùn)行30秒或更長(zhǎng)時(shí)間的非零值聯(lián)合起來魂爪,從而提供一個(gè)環(huán)境先舷,可以監(jiān)視應(yīng)用程序所調(diào)用的包之間的轉(zhuǎn)換。--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.
設(shè)置此選項(xiàng)甫窟,將在Monkey事件序列之前和之后立即生成profiling報(bào)告密浑。這將會(huì)在data/misc中生成大文件(~5Mb),所以要小心使用它粗井。--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.
通常尔破,當(dāng)應(yīng)用程序崩潰或發(fā)生任何失控異常時(shí),Monkey將停止運(yùn)行浇衬。如果設(shè)置此選項(xiàng)懒构,Monkey將繼續(xù)向系統(tǒng)發(fā)送事件,直到計(jì)數(shù)完成耘擂。--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.
通常胆剧,當(dāng)應(yīng)用程序發(fā)生任何超時(shí)錯(cuò)誤(如“Application Not Responding”對(duì)話框)時(shí),Monkey 將停止運(yùn)行。如果設(shè)置此選項(xiàng)秩霍,Monkey 將繼續(xù)向系統(tǒng)發(fā)送事件篙悯,直到計(jì)數(shù)完成。--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.
通常铃绒,當(dāng)應(yīng)用程序發(fā)生許可錯(cuò)誤(如啟動(dòng)一個(gè)需要某些許可的 Activity )時(shí)鸽照,Monkey 將停止運(yùn)行。如果設(shè)置了此選項(xiàng)颠悬,Monkey 將繼續(xù)向系統(tǒng)發(fā)送事件矮燎,直到計(jì)數(shù)完成。--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.
通常赔癌,當(dāng) Monkey 由于一個(gè)錯(cuò)誤而停止時(shí)诞外,出錯(cuò)的應(yīng)用程序?qū)⒗^續(xù)處于運(yùn)行狀態(tài)。當(dāng)設(shè)置了此選項(xiàng)時(shí)灾票,將會(huì)通知系統(tǒng)停止發(fā)生錯(cuò)誤的進(jìn)程峡谊。注意,正常的(成功的)結(jié)束铝条,并沒有停止啟動(dòng)的進(jìn)程靖苇,設(shè)備只是在結(jié)束事件之后,簡(jiǎn)單地保持在最后的狀態(tài)班缰。--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.
監(jiān)視并報(bào)告 Android 系統(tǒng)中本地代碼的崩潰事件贤壁。如果設(shè)置了 --kill-process-after-error,系統(tǒng)將停止運(yùn)行埠忘。--wait-dbg
Stops the Monkey from executing until a debugger is attached to it.
停止執(zhí)行中的 Monkey脾拆,直到有調(diào)試器和它相連接。
相關(guān)鏈接
https://developer.android.com/studio/test/monkey.html
http://www.cnblogs.com/yyangblog/archive/2011/03/10/1980068.html
http://blog.csdn.net/huangbiao86/article/details/8490743