1. 程式人生 > >Android自動化測試手段之Monkey(adb shell monkey)

Android自動化測試手段之Monkey(adb shell monkey)


android monkey原始碼下載見:http://fanfq.iteye.com/blog/781694

Monkeyrunner在這裡:http://www.51testing.com/html/59/n-242759.html

=======================================================

Monkey測試是Android自動化測試的一種手段,Monkey測試本身非常簡單,就是模擬使用者的按鍵輸入,觸控式螢幕輸入,手勢輸入等,看裝置多長時間會出異常。

當Monkey程式在模擬器或裝置執行的時候,如果使用者出發了比如點選,觸控,手勢或一些系統級別的事件的時候,它就會產生隨機脈衝,所以可以用Monkey用隨機重複的方法去負荷測試你開發的軟體.

 

最簡單的方法就是用用下面的命令來使用Monkey,這個命令將會啟動你的軟體並且觸發500個事件.

$ adb shell monkey -v -p your.package.name 500

 

Monkey是一個命令列工具,可以執行在模擬器裡或實際裝置中。它向系統傳送偽隨機的使用者事件流,實現對正在開發的應用程式進行壓力測試。Monkey包括許多選項,它們大致分為四大類:

• 基本配置選項,如設定嘗試的事件數量。

• 執行約束選項,如設定只對單獨的一個包進行測試。

• 事件型別和頻率。

• 除錯選項。

 

在Monkey執行的時候,它生成事件,並把它們發給系統。同時,Monkey還對測試中的系統進行監測,對下列三種情況進行特殊處理:

• 如果限定了Monkey執行在一個或幾個特定的包上,那麼它會監測試圖轉到其它包的操作,並對其進行阻止。

• 如果應用程式崩潰或接收到任何失控異常,Monkey將停止並報錯。

• 如果應用程式產生了應用程式不響應(application not responding)的錯誤,Monkey將會停止並報錯。

按照選定的不同級別的反饋資訊,在Monkey中還可以看到其執行過程報告和生成的事件。

 

Monkey基本用法 :

可以通過開發機器上的命令列或指令碼來啟動Monkey。由於Monkey執行在模擬器/裝置環境中,所以必須用其環境中的shell來進行啟動。可以通過在每條命令前加上adb shell來達到目的,也可以進入Shell後直接輸入Monkey命令。基本語法如下:

$ adb shell monkey [options]

如果不指定options,Monkey將以無反饋模式啟動,並把事件任意傳送到安裝在目標環境中的全部包。下面是一個更為典型的命令列示例,它啟動指定的應用程式,並向其傳送500個偽隨機事件:

$ adb shell monkey -p your.package.name -v 500

命令選項參考:

下表中列出了Monkey命令列可用的全部選項。

 


monkey [-p ALLOWED_PACKAGE [-p ALLOWED_PACKAGE] ...]

[-c MAIN_CATEGORY [-c MAIN_CATEGORY] ...]

[--ignore-crashes] [--ignore-timeouts]

[--ignore-security-exceptions] [--monitor-native-crashes]

[--kill-process-after-error] [--hprof]

[--pct-touch PERCENT] [--pct-motion PERCENT]

[--pct-trackball PERCENT] [--pct-syskeys PERCENT]

[--pct-nav PERCENT] [--pct-majornav PERCENT]

[--pct-appswitch PERCENT] [--pct-flip PERCENT]

[--pct-anyevent PERCENT]

[--wait-dbg] [--dbg-no-events] [-f scriptfile]

[--port port]

[-s SEED] [-v [-v] ...] [--throttle MILLISEC]

COUNT

講了這麼多,好像很枯燥,很難懂,即使看完了選項也不知道如何使用。

首先我們應該明白我們不可能使用monkey來制定做一樣測試,重複做很多次,因為monkey是偽隨機的,就是說雖然有一定規律可循,但是並不是我們就可以指定動作,但是我們可以通過新增命令選項來將操作限制在一定的範圍內。

monkey -v -p com.android.camera --throttle 5000 --pct-anyevent 100 500

這條命令的解釋是:

-v顯示預設程度的資訊;

-p com.android.camera是指定測試的程式,// Allowing start of Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.android.camera/.Camera } in package com.android.camera這是開始測試的camera的內容;

--throttle 5000 設定延時;

--pct-anyevent 100設定啟動activity的百分比為100%。如果沒有指定,我們可以看到在開始執行的時候8個事件(上面的選項中有介紹)的百分比如下:

# monkey -v -p com.android.camera --throttle 5000 500

monkey -v -p com.android.camera --throttle 5000 500

:Monkey: seed=0 count=500

:AllowPackage: com.android.camera

:IncludeCategory: android.intent.category.LAUNCHER

:IncludeCategory: android.intent.category.MONKEY

// Event percentages:

// 0: 15.0%

// 1: 10.0%

// 2: 15.0%

// 3: 25.0%

// 4: 15.0%

// 5: 2.0%

// 6: 2.0%

// 7: 1.0%

// 8: 15.0%

指定事件之後

# monkey -v -p com.android.camera --throttle 5000 --pct-anyevent 100 500

monkey -v -p com.android.camera --throttle 5000 --pct-anyevent 100 500

:Monkey: seed=0 count=500

:AllowPackage: com.android.camera

:IncludeCategory: android.intent.category.LAUNCHER

:IncludeCategory: android.intent.category.MONKEY

// Event percentages:

// 0: 0.0%

// 1: 0.0%

// 2: 0.0%

// 3: 0.0%

// 4: 0.0%

// 5: 0.0%

// 6: 0.0%

// 7: 0.0%

// 8: 100.0%

最後count就是事件數設定為500

 

接下來看一個例項

Monkey的用法是$ adb shell monkey -p your.package.name -v 500 ,首先,我們要找到應用程式在Emulator中所對應的包名,我一開始是一個個目錄找得,前7步完成了這項工作,最終發現應用程式包都在data/data下,你可以在shell中cd data/data,然後ls檢視當前Emulator中的所有應用程式包。

假如我們想對SDK中的APIDemos做壓力測試,

1.在Eclipse中新建工程,將: \android-sdk-windows\samples\android-8\samples\ApiDemos新增到工程中點選執行,此時,他的APK應該載入到了Emulator上

2.在命令列輸入adb shell

3.輸入ls檢視當前資料夾下的目錄,執行結果如下

C:\Documents and Settings\fanfq>adb shell

# ls

ls

config

cache

sdcard

acct

mnt

d

etc

system

sys

sbin

proc

init.rc

init.goldfish.rc

init

default.prop

data

root

dev

4.應用程式包都在data下,我們輸入cd data進入data資料夾,如下

# cd data

cd data

5.輸入ls檢視資料夾下的內容,如下所示:

# ls

ls

backup

property

data

misc

local

app-private

dontpanic

system

app

dalvik-cache

lost+found

6.還有個data,所有的應用程式就在這個data下了,進入這個data,然後輸入ls如下:

# cd data

cd data

# ls

ls

com.android.fallback

com.android.providers.settings

com.android.inputmethod.pinyin

com.android.launcher

com.android.providers.telephony

com.android.providers.contacts

com.android.providers.downloads

com.android.email

com.android.alarmclock

com.android.mms

com.example.android.apis

com.android.settings

com.android.inputmethod.latin

com.android.providers.userdictionary

com.android.music

com.android.camera

com.android.certinstaller

android.tts

com.svox.pico

com.android.browser

com.android.providers.applications

com.android.netspeed

com.android.gallery

com.android.htmlviewer

com.android.customlocale

com.android.term

com.android.providers.media

com.android.wallpaper.livepicker

com.android.providers.drm

com.android.speechrecorder

com.android.server.vpn

com.android.packageinstaller

com.android.protips

com.android.development

com.android.defcontainer

com.android.carhome

com.android.cardock

com.android.calculator2

com.android.spare_parts

jp.co.omronsoft.openwnn

com.android.sdksetup

com.android.contacts

com.android.soundrecorder

com.android.providers.subscribedfeeds

com.android.phone

7.最後一個就是我們剛才載入的APIDemo的應用程式包

8.可以直接輸入monkey -p com.example.android.apis -v 50,結果如下,也可以退出shell,在命令列輸入,adb shell monkey -p com.example.android.apis -v 500 ,執行過程中,Emulator中的應用程式在不斷地切換畫面,可以看一下,呵呵

# monkey -p com.example.android.apis -v 50

monkey -p com.example.android.apis -v 50

:Monkey: seed=0 count=50

:AllowPackage: com.example.android.apis

:IncludeCategory: android.intent.category.LAUNCHER

:IncludeCategory: android.intent.category.MONKEY

// Event percentages:

// 0: 15.0%

// 1: 10.0%

// 2: 15.0%

// 3: 25.0%

// 4: 15.0%

// 5: 2.0%

// 6: 2.0%

// 7: 1.0%

// 8: 15.0%

:Switch: #Intent;action=android.intent.action.MAIN;category=android.intent.categ

ory.LAUNCHER;launchFlags=0x10000000;component=com.example.android.apis/.ApiDemos

;end

// Allowing start of Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.android.apis/.ApiDemos } in package com.

example.android.apis

:Sending Pointer ACTION_MOVE x=-4.0 y=2.0

:Sending Pointer ACTION_UP x=0.0 y=0.0

// Allowing start of Intent { cmp=com.example.android.apis/.ApiDemos } in pa

ckage com.example.android.apis

:Sending Pointer ACTION_DOWN x=207.0 y=282.0

:Sending Pointer ACTION_UP x=189.0 y=289.0

// Allowing start of Intent { cmp=com.example.android.apis/.app.Intents } in

package com.example.android.apis

:Sending Pointer ACTION_DOWN x=95.0 y=259.0

:Sending Pointer ACTION_UP x=95.0 y=259.0

:Sending Pointer ACTION_DOWN x=295.0 y=223.0

:Sending Pointer ACTION_UP x=290.0 y=213.0

:Sending Pointer ACTION_MOVE x=-5.0 y=3.0

:Dropped: keys=0 pointers=0 trackballs=0 flips=0

## Network stats: elapsed time=3799ms (3799ms mobile, 0ms wifi, 0ms not connecte

d)

// Monkey finished


此文件居然有被翻譯成英文版的,http://www.codeweblog.com/the-android-monkey-automated-testing-tools/