1、簡介 php
monkeyrunner工具提供了一個API,使用此API寫出的程序能夠在Android代碼以外控制Android設備和模擬器。經過monkeyrunner,您能夠寫出一個Python程序去安裝一個Android應用程序或測試包,運行它,向它發送模擬擊鍵,截取它的用戶界面圖片,並將截圖存儲於工做站上。monkeyrunner工具的主要設計目的是用於測試功能/框架水平上的應用程序和設備,或用於運行單元測試套件,但您固然也能夠將其用於其它目的。 html
2、monkeyrunner工具同Monkey工具的差異 java
Monkey: python
Monkey工具直接運行在設備或模擬器的adb shell中,生成用戶或系統的僞隨機事件流。 android
monkeyrunner: web
monkeyrunner工具則是在工做站上經過API定義的特定命令和事件控制設備或模擬器。 shell
3、monkeyrunner的測試類型 api
1、多設備控制:monkeyrunner API能夠跨多個設備或模擬器實施測試套件。您能夠在同一時間接上全部的設備或一次啓動所有模擬器(或通通一塊兒),依據程序依次鏈接到每個,而後運行一個或多個測試。您也能夠用程序啓動一個配置好的模擬器,運行一個或多個測試,而後關閉模擬器。 app
2、 功能測試: monkeyrunner能夠爲一個應用自動貫徹一次功能測試。您提供按鍵或觸摸事件的輸入數值,而後觀察輸出結果的截屏。 框架
3、 迴歸測試:monkeyrunner能夠運行某個應用,並將其結果截屏與既定已知正確的結果截屏相比較,以此測試應用的穩定性。
4、 可擴展的自動化:因爲monkeyrunner是一個API工具包,您能夠基於Python模塊和程序開發一整套系統,以此來控制Android設備。除了使用monkeyrunner API以外,您還可使用標準的Python os和subprocess模塊來調用Android Debug Bridge這樣的Android工具。
4、運行monkeyrunner
您能夠直接使用一個代碼文件運行monkeyrunner,抑或在交互式對話中輸入monkeyrunner語句。不論使用哪一種方式,您都須要調用SDK目錄的tools子目錄下的monkeyrunner命令。若是您提供一個文件名做爲運行參數,則monkeyrunner將視文件內容爲Python程序,並加以運行;不然,它將提供一個交互對話環境。
monkeyrunner的命令語法爲:
monkeyrunner -plugin <plugin_jar> <program_filename> <program_options>
5、主要涉及類及API
MonkeyRunner ,MonkeyDevice,MonkeyImage
· MonkeyRunner:一個爲monkeyrunner程序提供工具方法的類。這個類提供了用於鏈接monkeyrunner至設備或模擬器的方法。它還提供了用於建立一個monkeyrunner程序的用戶界面以及顯示內置幫助的方法。
· MonkeyDevice:表示一個設備或模擬器。這個類提供了安裝和卸載程序包、啓動一個活動以及發送鍵盤或觸摸事件到應用程序的方法。您也能夠用這個類來運行測試包。
· MonkeyImage:表示一個截圖對象。這個類提供了截圖、將位圖轉換成各類格式、比較兩個MonkeyImage對象以及寫圖像到文件的方法。
主要API(不想本身作,直接貼官方的)
MonkeyRunner:
void |
alert (string message, string title, string okTitle) (主要的,彈出alert) Displays an alert dialog to the process running the current program. |
integer |
choice (string message, iterable choices, string title) Displays a dialog with a list of choices to the process running the current program. |
void |
help (string format) (顯示幫助信息) Displays the monkeyrunner API reference in a style similar to that of Python's pydoc tool, using the specified format. |
string |
input (string message, string initialValue, string title, string okTitle, string cancelTitle) Displays a dialog that accepts input. |
void |
sleep (float seconds) (睡眠,參數爲秒) Pauses the current program for the specified number of seconds. |
waitForConnection (float timeout, string deviceId) (等待鏈接device) Tries to make a connection between the monkeyrunner backend and the specified device or emulator. |
MonkeyDevice:
Constants |
|||||||||||
string |
Use this with the type argument of press() or touch() to send a DOWN event. (按鍵事件,按下不彈上,適用於長按) |
||||||||||
string |
Use this with the type argument of press() or touch() to send an UP event. (按鍵事件,彈上來) |
||||||||||
string |
Use this with the type argument of press() or touch() to send a DOWN event immediately followed by an UP event. (先按下,再彈上) |
Methods |
|||||||||||
void |
broadcastIntent (string uri, string action, string data, string mimetype, iterable categories dictionary extras, component component, iterable flags) Broadcasts an Intent to this device, as if the Intent were coming from an application. |
||||||||||
void |
drag (tuple start, tuple end, float duration, integer steps) (拖動屏幕) Simulates a drag gesture (touch, hold, and move) on this device's screen. (參數爲起始點,終止點,及duration事件段的步驟數steps) |
||||||||||
object |
getProperty (string key) (獲取設備參數) Given the name of a system environment variable, returns its value for this device. The available variable names are listed in the detailed description of this method. |
||||||||||
object |
getSystemProperty (string key) (獲取系統參數) . The API equivalent of adb shell getprop <key>. This is provided for use by platform developers. |
||||||||||
void |
installPackage (string path) (安裝包,參數爲包路徑) Installs the Android application or test package contained in packageFile onto this device. If the application or test package is already installed, it is replaced. |
||||||||||
dictionary |
instrument (string className, dictionary args) Runs the specified component under Android instrumentation, and returns the results in a dictionary whose exact format is dictated by the component being run. The component must already be present on this device. |
||||||||||
void |
press (string name, dictionary type) (按下鍵值爲那麼的鍵,type爲按鍵動做類型) Sends the key event specified by type to the key specified by keycode. |
||||||||||
void |
reboot (string into) Reboots this device into the bootloader specified by bootloadType. |
||||||||||
void |
removePackage (string package) (卸載) Deletes the specified package from this device, including its data and cache. |
||||||||||
object |
shell (string cmd) (發送adb shell 命令) Executes an adb shell command and returns the result, if any. |
||||||||||
void |
startActivity (string uri, string action, string data, string mimetype, iterable (開啓activity)categories dictionary extras, component component, flags) Starts an Activity on this device by sending an Intent constructed from the supplied arguments. |
||||||||||
takeSnapshot() (截圖) Captures the entire screen buffer of this device, yielding a MonkeyImage object containing a screen capture of the current display. |
|||||||||||
void |
touch (integer x, integer y, integer type) (觸摸座標爲x,y的地方,type爲動做類型) Sends a touch event specified by type to the screen location specified by x and y. |
||||||||||
void |
type (string message) (在焦點處輸入信息) Sends the characters contained in message to this device, as if they had been typed on the device's keyboard. This is equivalent to calling press() for each keycode in message using the key event type DOWN_AND_UP. |
||||||||||
void |
wake () (喚醒屏幕) Wakes the screen of this device. |
MonkeyImage:
Methods |
|||||||||||
string |
convertToBytes (string format) Converts the current image to a particular format and returns it as a string that you can then access as an iterable of binary bytes. |
||||||||||
tuple |
getRawPixel (integer x, integer y) (得到x,y位置的ARGB矩陣值) Returns the single pixel at the image location (x,y), as an a tuple of integer, in the form (a,r,g,b). |
||||||||||
integer |
getRawPixelInt (integer x, integer y) Returns the single pixel at the image location (x,y), as a 32-bit integer. |
||||||||||
getSubImage (tuple rect) (截圖,參數爲一個矩形) Creates a new MonkeyImage object from a rectangular selection of the current image. |
|||||||||||
boolean |
sameAs (MonkeyImage other, float percent) (圖片比對,percent爲容忍出錯率) Compares this MonkeyImage object to another and returns the result of the comparison. The percent argument specifies the percentage difference that is allowed for the two images to be "equal". |
||||||||||
void |
writeToFile (string path, string format) (圖片存爲文件) Writes the current image to the file specified by filename, in the format specified by format |
6、簡單例子
MonkeyRunner腳本是用python寫的,因此要學習python。其實不難,本身理解理解,python簡約而不簡單的。
例子摘自網上,很小不過是可行的:
——————————————————————————
以sample中的ApiDemos爲例,先將其生成ApiDemos.apk。
前提:已有device鏈接
1、 將ApiDemos.apk放在$Android_Root\tools下。
2、 在$Android_Root\tools下新建一個monkeyrunnerprogram.py文件,裏面內容爲:
# Imports the monkeyrunner modules used by this program
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
# Connects to the current device, returning a MonkeyDevice object
device = MonkeyRunner.waitForConnection()
# Installs the Android package. Notice that this method returns a boolean, so you can test
# to see if the installation worked.
device.installPackage('./ApiDemos.apk')
# Runs the component
device.startActivity(component='com.example.android.apis/.ApiDemos')
# Presses the Menu button
device.press('KEYCODE_MENU','DOWN_AND_UP')
# Takes a screenshot
result = device.takeSnapshot()
# Writes the screenshot to a file
result.writeToFile('./shot1.png','png')
3、 打開命令行轉到Android_Root\tools目錄下運行一下命令:
monkeyrunner monkeyrunnerprogram.py
110307 15:33:19.625:I [main] [com.android.monkeyrunner.MonkeyManager] Monkey Command: wake.
110307 15:33:20.625:I [main] [com.android.monkeyrunner.MonkeyManager] Monkey Command: wake.
110307 15:33:21.625:I [main] [com.android.monkeyrunner.MonkeyManager] Monkey Command: wake.
110307 15:33:22.718:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] Error starting command: monkey --port 12345
110307 15:33:22.718:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice]com.android.ddmlib.ShellCommandUnresponsiveException
110307 15:33:22.718:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] at com.android.ddmlib.AdbHelper.executeRemoteCommand(AdbHelper.java:408)
110307 15:33:22.718:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] at com.android.ddmlib.Device.executeShellCommand(Device.java:276)
110307 15:33:22.718:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] at com.android.monkeyrunner.adb.AdbMonkeyDevice$1.run(AdbMonkeyDevice.java:89)
110307 15:33:22.718:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] at java.util.concurrent.Executors$RunnableAdapter.call(UnknownSource)
110307 15:33:22.718:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
110307 15:33:22.718:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] at java.util.concurrent.FutureTask.run(Unknown Source)
110307 15:33:22.718:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
110307 15:33:22.718:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] at java.util.concurrent.ThreadPoolExecutor$Worker.run(UnknownSource)
110307 15:33:22.718:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] at java.lang.Thread.run(Unknown Source)
110307 15:33:57.437:I [main] [com.android.monkeyrunner.MonkeyManager] Monkey Command: press KEYCODE_MENU.
110307 15:33:59.171:I [main] [com.android.monkeyrunner.MonkeyManager] Monkey Command: quit.
注:裏面exception的提示咱們能夠忽略。
4、 能夠Android_Root\tools下查看生成的shot1.png的截圖。
實例擴展
在實例五的基礎上繼續試驗:
1、 在$Android_Root\tools下新建一個monkeyrunnerprogram1.py文件,裏面內容爲:
# Imports the monkeyrunner modules used by this program
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
# Connects to the current device, returning a MonkeyDevice object
device = MonkeyRunner.waitForConnection()
# Takes a screenshot
result = device.takeSnapshot()
# Writes the screenshot to a file
result.writeToFile('./shotbegin.png','png')
# Presses the Down button
device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')
device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')
device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')
device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')
device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')
# Takes a screenshot
result = device.takeSnapshot()
# Writes the screenshot to a file
result.writeToFile('./shotend.png','png')
2、 將畫面定位在Apidemos的首頁,並將光標定位在第一項上。
3、 在$Android_Root\tools目錄下運行一下命令:
monkeyrunner monkeyrunnerprogram1.py
(該例子摘自:http://www.189works.com/article-13451-1.html)
全文出自本人博客:http://iwangyue.cn(不二生的老窩)