Android自動化測試之MonkeyRunner工具 一、什麼是monkeyrunnermonkeyrunner工具提供了一個API,使用此API寫出的程式可以在Android代碼之外控制Android裝置和模擬器。通過monkeyrunner,您可以寫出一個Python程式去安裝一個Android應用程式或測試包,運行它,向它發送類比擊鍵,截取它的使用者介面圖片,並將儲存於工作站上。monkeyrunner工具的主要設計目的是用於測試功能/架構水平上的應用程式和裝置,或用於運行單元測試套件,但您當然也可以將其用於其它目的。二、monkeyrunner工具同Monkey工具的差別Monkey:Monkey工具直接運行在裝置或模擬器的adb shell中,產生使用者或系統的偽隨機事件流。monkeyrunner:monkeyrunner工具則是在工作站上通過API定義的特定命令和事件控制裝置或模擬器。三、monkeyrunner的測試類型1、多裝置控制:monkeyrunner API可以跨多個裝置或模擬器實施測試套件。您可以在同一時間接上所有的裝置或一次啟動全部模擬器,依據程式依次串連到每一個,然後運行一個或多個測試。您也可以用程式啟動一個配置好的模擬器,運行一個或多個測試,然後關閉模擬器。2、功能測試: monkeyrunner可以為一個應用自動貫徹一次功能測試。您提供按鍵或觸摸事件的輸入數值,然後觀察輸出結果的截屏。3、迴歸測試:monkeyrunner可以運行某個應用,並將其結果截屏與既定已知正確的結果截屏相比較,以此測試應用的穩定性。4、可擴充的自動化:由於monkeyrunner是一個API工具包,您可以基於Python模組和程式開發一整套系統,以此來控制Android裝置。除了使用monkeyrunner API之外,您還可以使用標準的Python os和subprocess模組來調用Android Debug Bridge這樣的Android工具。四、運行monkeyrunner您可以直接使用一個代碼檔案運行monkeyrunner,或者在互動式對話中輸入monkeyrunner語句。不論使用哪種方式,您都需要調用SDK目錄的tools子目錄下的monkeyrunner命令。如果您提供一個檔案名稱作為運行參數,則monkeyrunner將視檔案內容為Python程式,並加以運行;否則,它將提供一個互動對話環境。monkeyrunner的命令文法為:monkeyrunner -plugin <plugin_jar> <program_filename> <program_options>[以上的內容部分摘自互連網,借鑒前人的總結]五、執行個體我這裡就以sample中的ApiDemos為例,我們學習任何一樣東西似乎都可以有內建的Sample開始,可以在開發環境中先將其產生ApiDemos.apk。前提:已有device串連(這裡可以是真實裝置,或者模擬器)1、 將ApiDemos.apk放在$Android_Root\tools下。2、 在$Android_Root\tools下建立一個monkeyrunnerprogram.py檔案,按照官方的協助文檔,該檔案裡面內容為: 1 # Imports the monkeyrunner modules used by this program 2 3 from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage 4 5 # Connects to the current device, returning a MonkeyDevice object 6 7 device = MonkeyRunner.waitForConnection() 8 9 # Installs the Android package. Notice that this method returns a boolean, so you can test10 11 # to see if the installation worked.12 13 device.installPackage('./ApiDemos.apk')14 15 16 # Runs the component17 18 device.startActivity(component='com.example.android.apis/.ApiDemos')19 20 21 # Presses the Menu button22 23 device.press('KEYCODE_MENU','DOWN_AND_UP')24 25 26 # Takes a screenshot27 28 result = device.takeSnapshot()29 30 31 # Writes the screenshot to a file32 33 result.writeToFile('./shot1.png','png') 3、 開啟命令列轉到Android_Root\tools目錄下運行一下命令:monkeyrunner monkeyrunnerprogram.py130215 23:35:19.711:I [main] [com.android.monkeyrunner.MonkeyManager] Monkey Command: wake.130215 23:35:20.711:I [main] [com.android.monkeyrunner.MonkeyManager] Monkey Command: wake.130215 23:35:21.711:I [main] [com.android.monkeyrunner.MonkeyManager] Monkey Command: wake.130215 23:35:22.835:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] Error starting command: monkey --port 12345130215 23:35:22.835:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice]com.android.ddmlib.ShellCommandUnresponsiveException130215 23:35:22.835:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] at com.android.ddmlib.AdbHelper.executeRemoteCommand(AdbHelper.java:408)130215 23:35:22.835:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] at com.android.ddmlib.Device.executeShellCommand(Device.java:276)130215 23:35:22.835:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] at com.android.monkeyrunner.adb.AdbMonkeyDevice$1.run(AdbMonkeyDevice.java:89)130215 23:35:22.835:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] at java.util.concurrent.Executors$RunnableAdapter.call(UnknownSource)130215 23:35:22.835:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)130215 23:35:22.835:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] at java.util.concurrent.FutureTask.run(Unknown Source)130215 23:35:22.835:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)130215 23:35:22.835:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] at java.util.concurrent.ThreadPoolExecutor$Worker.run(UnknownSource)130215 23:35:22.835:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] at java.lang.Thread.run(UnknownSource)130215 23:35:55.632:I [main] [com.android.monkeyrunner.MonkeyManager] Monkey Command: press KEYCODE_MENU.130215 23:35:58.311:I [main] [com.android.monkeyrunner.MonkeyManager] Monkey Command: quit.4、 可以Android_Root\tools下查看產生的shot1.png的。六、執行個體改進因為ApiDemos首頁上按下MENU鍵沒有菜單出現,為了更加形象化,在上一個實驗的基礎上繼續實驗一下:1、 在$Android_Root\tools下建立一個monkeyrunnerprogram1.py檔案,裡面內容為: 1 # Imports the monkeyrunner modules used by this program 2 3 from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage 4 5 # Connects to the current device, returning a MonkeyDevice object 6 7 device = MonkeyRunner.waitForConnection() 8 9 # Takes a screenshot10 11 result = device.takeSnapshot()12 13 # Writes the screenshot to a file14 15 result.writeToFile('./shotbegin.png','png')16 17 # Presses the Down button18 #這裡是我們加入的主要內容 19 device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')20 21 device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')22 23 device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')24 25 device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')26 27 device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')28 29 # Takes a screenshot30 31 result = device.takeSnapshot()32 33 # Writes the screenshot to a file34 35 result.writeToFile('./shotend.png','png')www.2cto.com2、 將畫面定位在Apidemos的首頁,此時游標定位在第一行。3、 在$Android_Root\tools目錄下運行一下命令:monkeyrunner monkeyrunnerprogram1.py4、在運行過程中我們可以看見游標不斷向下移動,並且可以在目前的目錄下我們自訂的:運行前:shotbegin.png運行後,shotend.png(在代碼中進行了5次KEYCODE_DPAD_DOWN操作)