Monkey原理初步和改良优化--Android自动化测试学习历程
章節:自動化基礎篇——Monkey原理初步和改良優化(第三講)
?
?
主要講解內容與筆記:
一、理論知識:
?
直接看文檔,來了解monkey的概念、基本原理,以及如何使用。
?
First,what is Android 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.
?
翻譯過來:Monkey 是一個可以運行在模擬器或設備上的程序,它可以生成用戶時間的偽隨機序列,如點擊,觸屏,或者抓取,也包含一系列的系統級的事件。我們可以使用Monkey對自己開發的應用程序做壓力測試,使用隨機且可重復的操作。
?
注:說明一下什么是偽隨機事件?
所謂偽隨機事件,就是:seed相同,則隨機序列不變;并且在一定時間內,所生成的隨機事件不重復。
?
Second,what can Monkey do?
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.
?
翻譯過來:
Monkey包含多種選擇,但是它們分為以下四種基本部分:
- 基礎設置選項,如設置將做事件的數目
- 操作限制,如限制某個單一包的測試
- 事件類型和頻率
- 調試選項
當Monkey跑起來之后,就會產生事件,并將它們發送到系統。它也會關注測試下的系統情況,注意一下的三種情況會被特殊對待哦:
- 如果你限制了Monkey只能泡在某一個或更多個特定的包上時,它就會關注打算去跨越到其他包的企圖,并阻止它們。
- 如果你的應用程序崩掉了或者接受到了其他種類的不可控異常,Monkey就會停掉,并report這個Error
- 如果你的應用程序生成了一個不發送error的應用,Monkey也會阻止并report這個Error
?
Third,how to use Monkey?
The basic syntax is:
$ adb shell monkey [options] <event-count>一般大家比較簡單的就這樣用:
$ adb shell monkey -p your.package.name -v 500接下來重點介紹命令行參數的含義:
?
| General ? ? ? ? ? ? | --help | 打開幫助,可以即時看到參數信息 |
| -v | 每個-v 命令增加結果不同冗余級別。不同的level,從0到2,所能提供的是越來越詳盡的內容提示? | |
| Events | -s <seed> | 隨機生成序列的種子,如果seed 的值一樣的話,生成的隨機序列就是確定的 |
| --throttle <milliseconds> | 插入一個在兩個事件之間的固定延時,如果不插入的話,這個事件就會被迅速生成,一般應該生成的是用戶能夠點擊的一個比較正常的延時 | |
| --pct-touch <percent> | 調整touch觸屏事件的百分比,觸碰事件就是指在屏幕上的一個單獨位置的一次點擊-抬起的事件 | |
| --pct-motion <percent> | 調整移動事件的百分比,移動事件就是指在屏幕上按下somewhere,經歷一系列的偽隨機的移動,最后抬起 | |
| --pct-trackball <percent> | 調整軌跡球事件的百分比(這個通過drag是沒有辦法做到的,因為drag是直來直去的) | |
| --pct-nav <percent> | 調整導航事件的百分比(基本導航事件的百分比,如上下左右鍵) | |
| --pct-majornav <percent> | 調整主要導航事件的百分比(如中間鍵,取消、確定或菜單引發的圖形接口的動作) | |
| --pct-syskeys <percent> | 調整系統按鍵事件,如Home、Back、StartCall、Endcall及音量控制鍵等 | |
| --pct-appswitch <percent> | 調整啟動Activity的百分比,在隨機間隔中,執行一個startActivity()方法調用,作為一種最大化的覆蓋安裝包的所有的Activity的方法 | |
| --pct-anyevent <percent> | 調整其他類型的事件的百分比,比如按鍵,或者其他不太常用到的一些事件的百分比 | |
| Constraints | -p <allowed-package-name> | 這個參數可以指定特定的一個或多個包,每增加一個包,就需要使用一次-p |
| -c <main-category> | 可以確定一個或多個特定的類別,Monkey就只允許系統去訪問這些特定類別下列出的Activity了。若不指定的話,Monkey將選擇以下類別中列出的Activity:Intent.CATEGORY_LAUNCHER or Intent.CATEGORY_MONKEY。 -c的命令與-p相同,指定一個特定的類別就需要一個-c | |
| Debugging ? ? ? ? | --dbg-no-events | 該選項主要可以用來監控你的應用程序所調用的包之間的轉換。該參數被設置后,Monkey就執行初始啟動,進入一個test的Activity。為了達到一個最好的效果,應與-v、一個或多個package的限制, 以及一個非零的throttle來保證Monkey可以跑至少30S才行 |
| --hprof | ?該選項被設置后,將會在Monkey事件序列前后立刻生成report,大小為大約5Mb,存儲在data/misc,所以要小心使用 | |
| --ignore-crashes | 通常情況下,Monkey在遇到了應用程序崩潰或是任何其他類型的非可控異常時會停止運行。如果設置此項,Monkey就不會停,會繼續跑,繼續向系統發送事件,直到這個所有計數全部完成 | |
| --ignore-timeouts | 通常情況下,Monkey在遇到應用程序遭遇任何類型的timeout的error如“應用程序停止響應”的對話框時會聽停止運行。如果設置此項,Monkey就會繼續向系統發送事件,直到這個計數全部完成 | |
| --ignore-security-exceptions | 通常情況下,Monkey在遇到應用程序遭遇任何形式的權限錯誤時會停止運行,例如它打算去啟動一個activity而它卻需要一定的權限。如果設置了該項,Monkey就會繼續發送事件給系統,直到計數結束 | |
| --kill-process-after-error | 通常情況下,Monkey因為某個錯誤停止運行,應用程序就會繼續跑。當這項被設定后,當發生這個error的時候,它就會通知系統去stop這個 process。注意,在一個正常(成功的)完成過程中,已經啟動的程序不被停止運行,device的狀態就是最后事件發生后停留的那個狀態 | |
| --monitor-native-crashes | 監控和報告在android系統本地代碼發生的crashes。如果--kill-process-after-error被設定,則系統將會終止 | |
| --wait-dbg | 等待連接上調試器 |
?
二、Monkey架構原理初步
1、Monkey主類
?
找到void main()——>run(//查看注釋,run就是通過把命令行參數后面的值賦值到相應變量,運行在adb shell里面的monkey -p xxx -v xxx等的命令)
?
run方法中的邏輯:
(1)設置初始化值
(2)生成隨機值事件
(3)將Intent.CATEGORY_LAUNCHER和Intent.CATEGORY_MONKEY添加到我們category中,以便完成我們的啟動
(4)根據賦值結果進行一堆輸出操作,具體可見自己跑monkey的時候的輸出
(5)進入main monkey loop
?
2、MonkeyActivityEvent——負責進入APK的主界面
1 private Intent getEvent() { 2 Intent intent = new Intent(Intent.ACTION_MAIN); //定義一個Intent(意圖),標識Activity為一個程序的開始 3 intent.addCategory(Intent.CATEGORY_LAUNCHER); //把應用程序添加到程序列表里 4 intent.setComponent(mApp); //設置部件 5 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //增加標志,四種啟動模式之一 6 return intent; 7 }?
注:關于四種啟動模式:(摘自http://www.cnblogs.com/fanchangfa/archive/2012/08/25/2657012.html)
Activity啟動模式設置:<activity android:name=".MainActivity" android:launchMode="standard" />Activity的四種啟動模式:1. standard標準啟動模式,每次激活Activity時都會創建Activity,并放入任務棧中2. singleTop如果在任務的棧頂正好存在該Activity的實例, 就重用該實例,否者就會創建新的實例并放入棧頂(即使棧中已經存在該Activity實例,只要不在棧頂,都會創建實例)3. singleTask如果在棧中已經有該Activity的實例,就重用該實例(會調用實例的onNewIntent())。重用時,會讓該實例回到棧頂,因此在它上面的實例將會被移除棧。如果棧中不存在該實例,將會創建新的實例放入棧中4. singleInstance在一個新棧中創建該Activity實例,并讓多個應用共享該棧中的該Activity實例。一旦該模式的Activity的實例存在于某個棧中,任何應用再激活改Activity時都會重用該棧中的實例,其效果相當于多個應用程序共享一個應用,不管誰激活該Activity都會進入同一個應用中?
在andorid程序的AndroidManifest.xml中有以下配置信息:
1 <activity 2 android:name="com.example.com.android.hello.FullscreenActivity" 3 android:configChanges="orientation|keyboardHidden|screenSize" 4 android:label="@string/app_name" 5 android:theme="@style/FullscreenTheme" > 6 <intent-filter> 7 <action android:name="android.intent.action.MAIN" /> 8 9 <category android:name="android.intent.category.LAUNCHER" /> 10 </intent-filter> 11 </activity>其中就有配置出android的哪個activity是先啟動的,注:android.intent.action.MAIN決定應用程序最先啟動的Activity; android.intent.category.LAUNCHER決定應用程序是否顯示在程序列表里
?
3、MonkeyEvent類和一系列子類
MonkeyEvent:定義一個超類,定義變量,構造方法MonkeyEvent(int type) ,?以及獲取Type的方法public int getEventType()
MonkeyKeyEvent、MonkeyMotionEvent等等都是繼承自這個超類
?
三、改良Monkey方案1(只點擊你需要點的地方):做java工程,adb push進去
?
四、優化Monkey方案2(脫離adb口控制):做成android apk形式,讓系統級進程直接調用它
?
網易云課堂:
http://study.163.com/course/courseLearn.htm?courseId=712011#/learn/video?lessonId=878100&courseId=712011
| 金陽光測試 | 新浪微博:金陽光woody |
| ? ? ? ? ? ? ? ? ? ? 網站地址 | 1、百度搜:金陽光測試 2、官網:www.goldensunshine.cc |
| ? ? 微信公眾號 |
轉載于:https://www.cnblogs.com/GoldenSunshinetester/p/4380421.html
總結
以上是生活随笔為你收集整理的Monkey原理初步和改良优化--Android自动化测试学习历程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《梦断代码》读后感一
- 下一篇: Runtime.getRuntime()