紀錄:
首先MonkeyRunner主要是調用
/android-sdk-linux/tools/monkeyrunner
來自動化執行腳本
步驟1
在/android-sdk-linux/tools/中建立專屬的測試腳本資料夾,其中放入測試用腳本,
假設專案名稱為Project1,建立/android-sdk-linux/tools/Project1_script
步驟2
把專案apk(Project1.apk)放入步驟1建立的資料夾中並建立測試腳本(Project1_script.py),
測試腳本內容可參考以下範例,必須修改
device.installPackage('../Project1_script/Project1.apk')
->路徑必須對應apk位置
package
= 'abc.efg.hij' -> package name
activity
= 'abc.efg.hij.xxxActivity' -> activity name
範例如下
Project1_script.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('../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....
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
'顯示訊息'