用python編寫腳本
1.導入模塊:
MonkeyRunner
MonkeyDevice
MonkeyImage
ps:如果給導入模塊起別名,就應該使用別名,而不能使用原名,否則會出現錯誤。
from com.android.monkeyrunner import MonkeyRunner as mr
from com.android.monkeyrunner import MonkeyDevice as md
from com.android.monkeyrunner import MonkeyImage as mi
例:
device=mr.waitForConnection()
2.MonkeyRunner 模塊
2.1 com.android.monkeyrunner.MonkeyRunner.waitForConnection
注釋:等待連接設備,與模擬器鏈接,返回monkeydevice對象,代表鏈接的設備。沒有報錯的話說明連接成功。
參數:timeout:超時時間,單位秒,浮點數。默認是無限期等待。
deviceId:指定的設備名稱。默認為當前的設備(手機有限,比如手機通過usb線鏈接到pc、其次為模擬器)。
實例:默認連接:device=MonkeyRunner.waitForConnection()
參數連接:device=mr.waitForConnection(1.0,'emulator-5554')
2.2 com.android.monkeyrunner.MonkeyRunner.installPackage
注釋: #參數是相對或絕對APK路徑 路徑級別用“/”,不能用“\”
參數:path:被安裝到設備上的apk包在電腦上的文件路徑
實例:device.installPackage('myproject/bin/MyApplication.apk')
2.3 com.android.monkeyrunner.MonkeyRunner.removePackage
注釋:刪除連接設備中的應用包,刪除成功返回true
參數:pachage:將被刪除的文件的包名
實例:device.removePackage('myproject/bin/MyApplication.apk')
2.4 com.android.monkeyrunner.MonkeyRunner.startActivity
注釋:啟動任意的Activity,此時可以向模擬器發送如按鍵、滾動、截圖、存儲等操作了。 執行一個adb shell命令,並返回結果,如果有的話
device.shell("...")
參數:uri - The URI for the Intent.
action - The action for the Intent.
data - The data URI for the Intent
mimetype - The mime type for the Intent.
categories - A Python iterable containing the category names for the Intent.
extras - A dictionary of extras to add to the Intent. Types of these extras are inferred from the python types of the values.
component - The component of the Intent.
flags - An iterable of flags for the Intent.All arguments are optional. The default value for each argument is null.
實例:device.startActivity(component="your.www.com/your.www.com.TestActivity")
device.startActivity(component="your.www.com/.TestActivity")
2.5 com.android.monkeyrunner.MonkeyRunner.sleep
注釋:暫停目前正在運行的進程相應的時間
參數:second
實例:MonkeyRunner.sleep(秒數,浮點數)
2.6 com.android.monkeyrunner.MonkeyRunner.takeSnapshot
注釋:獲取設備的屏蔽緩沖區,產生了整個顯示器的屏蔽捕獲。(截圖)
參數:無
實例:result=device.takeSnapshot()
返回一個MonkeyImage對象(點陣圖包裝),我們可以用一下命令將圖保存到文件
result.writeToFile('takeSnapshot\\result1.png','png')
2.7 com.android.monkeyrunner.MonkeyImage.writeToFile
注釋:將截圖保存到文件 寫成功返回true,否則返回false
參數:path:輸出文件名和路徑
format:目標格式
實例:result.writeToFile('takeSnapshot\\result1.png','png')
2.8 com.android.monkeyrunner.MonkeyDevice.type
注釋:鍵盤上的類型指定字符串,這相當於要求字符中按(鍵碼,DOWN_AND_UP)字符串發送到鍵盤
參數:message:要鍵入的字符串
實例:device.type(‘字符串’)
2.9 com.android.monkeyrunner.MonkeyDevice.wake
注釋:喚醒設備屏幕(在設備屏幕上喚醒)
參數:無
實例:device.wake()
2.10 com.android.monkeyrunner.MonkeyDevice.reboot
注釋:重新引導到指定的引導程序指定的設備
參數:into-the bootloader to reboot into :bootloader,recovery,or None
實例:device.reboot()
3.Monkeyrunner類的方法介紹
3.1 com.android.monkeyrunner.MonkeyRunner.alert
注釋:彈出一個對話框直到用戶確認為止
參數:message:會話彈出的內容
title:會話標題,默認alert
okTitle:會話確認按鈕,默認ok
實例:Alert(string message,string title,string okTile)
3.2 com.android.monkeyrunner.MonkeyRunner.choice
注釋:顯示一個對話框,選擇你要添加的那個py文件
參數:message:顯示在對話框中的提示信息
choices:一個迭代的包含一系列選擇的python類型
title:對話框的標題,默認input
實例:Choice(string message,iterable choices,string title)
3.3 com.android.monkeyrunner.MonkeyRunner.help
注釋:Help(string format)monkeyrunner 的一些幫助,這個和api差不多
3.4com.android.monkeyrunner.MonkeyRunner.input
注釋:用戶可以在一個對話框里面輸入內容
參數:message:對話框顯示信息
initialValue:提供給用戶的初始化值,默認為空字符串
title:對話標題,默認為input
onTitle: the text to use in the dialog's confirmation button. default is 'OK'.
canceTitle: the text to use in the dialog's 'cancel' button. default is 'Cancel'.
返回:the text entered by the user ,or None if the user canceled the input.
實例:MonkeyRunner.input(string message,initialValue,title,okTitle,cancelTitle)
4 MonkeyDevice類的方法介紹
4.1 com.android.monkeyrunner.MonkeyDevice.ibroadcastIntent
注釋:對設備發送一個廣播信號
參數:uri - The URI for the Intent.
action - The action for the Intent.
data - The data URI for the Intent
mimetype - The mime type for the Intent.
categories - An iterable of category names for the Intent.
extras - A dictionary of extras to add to the Intent. Types of these extras are inferred from the python types of the values.
component - The component of the Intent.
flags - An iterable of flags for the Intent.All arguments are optional. The default value for each argument is null.(see android.content.Context.sendBroadcast(Intent))
實例:BroadcastIntent (string uri,string action,string data, string mimetype,iterable categories dectionnary extras,component component,iterable flages)
4.2 com.android.monkeyrunner.MonkeyDevice.drag
注釋:拖動屏幕,也是划屏的一些操作
參數:start:拖曳開始坐標 (a tuple(x,y)in pixels)
end:拖曳結束坐標點(a tuple(x,y)in pixels)
duration:持續時間(default 1.0 seconds)
steps:拖曳步驟(default is 10)
實例:MonkeyRunner.drag(start,end,duration,steps)
4.3
com.android.monkeyrunner.MonkeyDevice.getProperty(string key)
com.android.monkeyrunner.MonkeyDevice.getSystemProperty(string key)
com.android.monkeyrunner.MonkeyDevice.installPackage(string path)
com.android.monkeyrunner.MonkeyDevice.instrument(string className,dictionary args)
com.android.monkeyrunner.MonkeyDevice.press(string name,dectionary type)
com.android.monkeyrunner.MonkeyDevice.reboot(string package)
com.android.monkeyrunner.MonkeyDevice.removePackage(string package)
com.android.monkeyrunner.MonkeyDevice.shell(string cmd)執行adb shell
com.android.monkeyrunner.MonkeyDevice.takeSnapshot()
com.android.monkeyrunner.MonkeyDevice.touch(integer x,integer y,integer type)
com.android.monkeyrunner.MonkeyDevice.type(string message)
com.android.monkeyrunner.MonkeyDevice.wake()
5.MonkeyImage 類的方法介紹
com.android.monkeyrunner.MonkeyImage.convertBytes(string format)
將圖片轉換為其他特殊的格式,將結果作為字符串返回,用這個方法將像素存取為特殊的格式,輸出的字符串是一種更好的表現(format 目標格式 默認值為png)
com.android.monkeyrunner.MonkeyImage.getRawPixel(integer x, integer y)
在x,y位置處獲取一個單一的ARGB像素,參數x,y都是基於0坐標,表示一個像素尺寸,x向右增益,y向下增益,得到一個圖標的坐標,這個方法返回一個數組
com.android.monkeyrunner.MonkeyImage.getSubImage(tuple rect)
復制一個圖片的矩形,rect:A tuple(x,y,w,h),x,y,指定矩形區域的左上角,w為矩形寬,h為矩形高
com.android.monkeyrunner.MonkeyImage.sameAs(MonkeyImage other,float percent)
com.android.monkeyrunner.MonkeyImage.writeToFile(string path,string format)
com.android.monkeyrunner.MonkeyImage.getHierarchyViewer()
com.android.monkeyrunner.MonkeyImage.startActivity()