Description:
首先MonkeyRunner主要是調用
/android-sdk-linux/tools/monkeyrunner 來自動化執行腳本
Step1.
在/android-sdk-linux/tools/中建立專屬的測試腳本資料夾,其中放入測試用腳本,
假設專案名稱為Project1,建立/android-sdk-linux/tools/Project1_script資料夾
Step2.
把專案apk(Project1.apk)放入步驟1建立的資料夾中並建立測試腳本(Project1_script.py),
測試腳本內容可參考以下範例
# -*- coding: utf-8 -*- # 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('../Project1_script/Project1.apk') # sets a variable with the package's internal name package = 'abc.efg.hij' # sets a variable with the name of an Activity in the package activity = 'abc.efg.hij.xxxActivity' # sets the name of the component to start組合(package和activity) runComponent = package + '/' + activity # Runs the component device.startActivity(component=runComponent) # Do something u want to do...
第10行為安裝 package(app), 若 app 已安裝且沒有任何變動,這行可以刪除。
第22行為啟動 activity,其中參數 component= 為固定用法。
2.如何執行測試腳本:
使用Terminal移動到/android sdk/tools/Project1_script資料夾,再輸入
../monkeyrunner Project1_script.py
3.相關的內容在android api 中都有使用說明
4.常用方法紀錄
MonkeyRunner.sleep(n) ->等待n秒 device = MonkeyRunner.waitForConnection() device.press('KEYCODE_NUMPAD_1','DOWN_AND_UP') -> 模擬虛擬鍵盤輸入1 device.press('KEYCODE_NUMPAD_7','DOWN_AND_UP') -> 模擬虛擬鍵盤輸入7 device.press('KEYCODE_NUMPAD_2','DOWN_AND_UP') -> 模擬虛擬鍵盤輸入2 device.press('KEYCODE_NUMPAD_DOT','DOWN_AND_UP') -> 模擬虛擬鍵盤輸入 . 以上指令可用 device.type('172.') 取代 device.touch(384,199,'DOWN_AND_UP') -> 點擊螢幕x:384 y:199座標 start = (100,100) -> 拖曳起點 end = (100,120) -> 拖曳終點 device.drag(start,end,1,10) -> 開始拖曳 device.getProperty() -> 可獲得系統資訊, 參考http://developer.android.com/tools/help/MonkeyDevice.html#table1 如 width = int(device,getProperty('display.width') -> 螢幕寬度 height = int(device.getProperty('display.height') -> 螢幕高度 #註解 print '顯示訊息'
5. MonkeyRunner 可以獨立於app測試,大部分使用 MonkeyRunner 的情況都是測試app內部功能,但MonkeyRunner可以獨立於app以外,如下腳本並沒有啟動任何activity,而是按下home key。
# -*- coding: utf-8 -*- # 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() # do anything "OUT" of app device.press('KEYCODE_HOME','DOWN_AND_UP')