App壓力測試——MonkeyRunner
阿新 • • 發佈:2019-02-13
一.常見的MonkeyRunner API
1.alert(警告框)
void alert(string message,string title,string okTitle)
如這樣一個python指令碼
#coding:utf-8
from com.android monkeyrunner import MonkeyRunner
MonkeyRunner.alert("Hello World","This is title","OK")
執行指令碼命令
使用monkeyrunner就要用monkeyrunner命令來執行這個指令碼,而不是我們以前的pyhton filename.py
monkeyrunner filename.py
2.waitForConnection(等待裝置連線)
注意:有多個device id時,需要指明裝置名
waitForConnection(float timeout,string deviceid)
#float timeout超時時間
#string deviceid裝置id
3.drag(拖動)
drag(tuple start,tuple end,float duration,integer steps)
#tuple start拖動的起點
#tuple end拖動的終點
#float duration手勢持續的時間
#integer steps拖動的過程分幾步來實現(插值點的步數,預設為10)
4.press(按鍵)
press(string keycode,dictionary type)
#string keycode按鍵值,如回車鍵為66
#dictionary type按鍵型別,如UP,Down,DOWN_AND_UP
5.startActivity(啟動應用)
startActivity(package+'/'+activity)
6.touch(點選)
touch(integer x,integer y,integer type)
#integer x,integer y 指x和y的座標
#integer type觸控型別,如UP,Down,DOWN_AND_UP
7.type(輸入)
type(string message)
8.take Snapshot(截圖)
MonkeyImage takeSnapshot()
9.sameAs(影象對比)
boolean sameAs(MonkyeyImage other,float percent)
#MonkyeyImage other需要對比的影象名稱
#float percent對比相似度的百分比
10.writetoFile(儲存影象檔案)
void writetoFile(string path,string format)
#string path指定影象儲存的路徑
#string format指定影象的型別,如jpg,png
指令碼示例:
#-*- coding:utf-8 -*-
from com.android.monkeyrunner import MonkeyRunner,MonekyDevice,MonkeyImage
#連線裝置,超時時間設定3秒
device = MonkeyRunner.waitForConnection(3,"emulator-5554")
#啟動APP
device.startActivity("com.example.zhangjian.minibrowser2/com.example.zhangjian.minibrowser2.myapplication.MiniActivity")
MonkeyRunner.sleep(2)
#點選搜尋框,輸入框的位置用uiautomatorviewer工具可檢視
device.touch(100,100,"DOWN_AND_UP")
MonkeyRunner.sleep(1)
#輸入查詢詞
device.type("text")
MonkeyRunner.sleep(1)
#點選回車鍵
device.press("KEYCODE_ENTER","DOWN_AND_UP")
MonkeyRunner.sleep(1)
#點選搜尋按鈕
device.touch(400,100,"DOWN_AND_UP")
MonkeyRunner.sleep(6)
#截圖
image = device.takeSnapshot()
image.writeToFile("./test.png",'png')
#點選清除按鈕
device.touch(300,100,"DOWN_AND_UP")
MonkeyRunner.sleep(3)