Android monkeyrunner整理
[TOC]
簡(jiǎn)介
monkeyrunner即android SDK中自帶的工具之一檐什,此工具提供API可按制android設(shè)備或模擬器鹅心。
monkeyrunner提供了一個(gè)API隧膏,使用此API寫(xiě)出的程序可以在Android代碼之外控制Android設(shè)備和模擬器国夜。通過(guò)monkeyrunner,您可以寫(xiě)出一個(gè)Python程序去安裝一個(gè)Android應(yīng)用程序或測(cè)試包畦攘,運(yùn)行它届吁,向它發(fā)送模擬擊鍵,截取它的用戶界面圖片规辱,并將截圖存儲(chǔ)于工作站上谆棺。
monkeyrunner工具的主要目的是用于測(cè)試功能/框架水平上的應(yīng)用程序和設(shè)備,或用于運(yùn)行單元測(cè)試套件罕袋,但您當(dāng)然也可以將其用于其它目的改淑。
特性
多設(shè)備控制:monkeyrunner API可以跨多個(gè)設(shè)備或模擬器實(shí)施測(cè)試套件。您可以在同一時(shí)間接上所有的設(shè)備或一次啟動(dòng)全部模擬器(或統(tǒng)統(tǒng)一起)浴讯,依據(jù)程序依次連接到每一個(gè)朵夏,然后運(yùn)行一個(gè)或多個(gè)測(cè)試蟆沫。您也可以用程序啟動(dòng)一個(gè)配置好的模擬器嗜逻,運(yùn)行一個(gè)或多個(gè)測(cè)試,然后關(guān)閉模擬器神得。
功能測(cè)試:monkeyrunner可以為一個(gè)應(yīng)用自動(dòng)化功能測(cè)試奈籽。為您提供按鍵或觸摸事件的輸入數(shù)值饥侵,然后觀察輸出結(jié)果的截屏。
回歸測(cè)試:monkeyrunner可以運(yùn)行某個(gè)應(yīng)用衣屏,并將其結(jié)果截屏與既定已知正確的結(jié)果截屏相比較躏升,以此測(cè)試應(yīng)用的穩(wěn)定性。
可擴(kuò)展的自動(dòng)化:由于monkeyrunner是一個(gè)API工具包狼忱,您可以基于Python模塊和程序開(kāi)發(fā)一整套系統(tǒng)膨疏,以此來(lái)控制Android設(shè)備。除了使用monkeyrunner API之外钻弄,您還可以使用標(biāo)準(zhǔn)的Python os和subprocess模塊來(lái)調(diào)用如adb這樣的Android工具佃却。
使用方法
Monkeyrunner API
主要包括三個(gè)模塊
1、MonkeyRunner:這個(gè)類提供了用于連接monkeyrunner和設(shè)備或模擬器的方法斧蜕,它還提供了用于創(chuàng)建用戶界面顯示提供了方法双霍。
2、MonkeyDevice:代表一個(gè)設(shè)備或模擬器。這個(gè)類為安裝和卸載包洒闸、開(kāi)啟Activity染坯、發(fā)送按鍵和觸摸事件、運(yùn)行測(cè)試包等提供了方法丘逸。
3单鹿、MonkeyImage:這個(gè)類提供了捕捉屏幕的方法。這個(gè)類為截圖深纲、將位圖轉(zhuǎn)換成各種格式仲锄、對(duì)比兩個(gè)MonkeyImage對(duì)象、將image保存到文件等提供了方法湃鹊。
#引用導(dǎo)入API
from com.android.monkeyrunner import <module>
運(yùn)行
命令語(yǔ)法為:
monkeyrunner -plugin <plugin_jar> <program_filename> <program_options>
方式一:在CMD命令窗口直接運(yùn)行monkeyrunner
方式二:使用Python編寫(xiě)測(cè)試代碼文件儒喊,在CMD中執(zhí)行monkeyrunner Findyou.py運(yùn)行
不論使用哪種方式,您都需要調(diào)用SDK目錄的tools子目錄下的monkeyrunner命令币呵。
注意
在運(yùn)行monkeyrunner之前必須先運(yùn)行相應(yīng)的模擬器或連接真機(jī)怀愧,否則monkeyrunner無(wú)法連接到設(shè)備
運(yùn)行模擬器有兩種方法:
1、通過(guò)eclipse中執(zhí)行模擬器
2余赢、在CMD中通過(guò)命令調(diào)用模擬器
這里介紹通過(guò)命令芯义,在CMD中執(zhí)行模擬器的方法
emulator -avd test
上面命令中test是指模擬器的名稱。
附:
問(wèn)題:CMD運(yùn)行提示monkeyrunner不是內(nèi)部或外部命令妻柒,也不是可運(yùn)行的程序或批處理文件扛拨。
解決:電腦環(huán)境變量未配置,將monkeyrunner所在目錄配在環(huán)境變量里举塔。
變量名:Path
變量值:${ANDROID_HOME}\platform-tools
示例——卸載舊的APP绑警,安裝新的APP
準(zhǔn)備
a. 連接安卓真機(jī)設(shè)備
b. 運(yùn)行CMD,檢測(cè)是否連接成功
命令
adb devices
方法一
1.打開(kāi)CMD啤贩,運(yùn)行monkeyrunner
2.進(jìn)入monkeyrunner的shell命令交互模式后待秃,逐條輸入以下命令
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
device = MonkeyRunner.waitForConnection()
device.removePackage('xx.oo.package')
device.installPackage('xx.oo.apk')
注:每條命令的作用拜秧,請(qǐng)見(jiàn)方法二中的注解
方法二
- 編寫(xiě)Python測(cè)試代碼
# 引入本程序所用到的模塊
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
# 連接手機(jī)設(shè)備
device = MonkeyRunner.waitForConnection()
# 截圖
result = device.takeSnapshot()
# 將截圖保存到文件
result.writeToFile('./shot01.png','png')
# 卸載APP
device.removePackage('xx.oo.package')
print ('Uninstall xx.oo.package Success!')
# 暫停5秒
MonkeyRunner.sleep(5)
# 截圖
result = device.takeSnapshot()
result.writeToFile('./shot02.png','png')
# 安裝新的APP
device.installPackage('xx.oo.apk')
print ('Install xx.oo.apk Success!')
# 截圖
result = device.takeSnapshot()
result.writeToFile('./shot03.png','png')
注:拷貝運(yùn)行時(shí)請(qǐng)去掉中文注釋痹屹;或者在開(kāi)頭加入 #coding=utf-8
- 運(yùn)行腳本
Monkeyrunner Test1.py
- 檢查手機(jī)app是否已更新
API
Monkey Runner主要API有下面三種,代碼package為com.android.monkeyrunner
枉氮。
- MonkeyRunner
彈出警告框志衍、選擇列表、幫助文檔聊替、輸入楼肪、暫停、等待連接設(shè)備等惹悄。 - MonkeyDevice
主要操作設(shè)備的春叫,包含安裝、卸載,發(fā)送廣播暂殖,啟動(dòng)應(yīng)用等价匠。 - MonkeyImage
主要操作圖片,包含圖片保存呛每、對(duì)比踩窖、格式轉(zhuǎn)化、獲取像素點(diǎn)等晨横。
MonkeyRunner
void alert (string message, string title, string okTitle)
// Displays an alert dialog to the process running the current program.
// 彈出一個(gè)確認(rèn)框洋腮,在pc上面
integer choice (string message, iterable choices, string title)
// Displays a dialog with a list of choices to the process running the current program.
// 彈出一個(gè)選擇框,在pc上面
void help (string format)
// Displays the monkeyrunner API reference in a style similar to that of Python's pydoc tool, using the specified format.
string input (string message, string initialValue, string title, string okTitle, string cancelTitle)
// Displays a dialog that accepts input.
// 彈出一個(gè)輸入框手形,在pc上面
void sleep (float seconds)
// Pauses the current program for the specified number of seconds.
MonkeyDevice waitForConnection (float timeout, string deviceId)
// Tries to make a connection between the monkeyrunner backend and the specified device or emulator.
// 待定設(shè)備連接
MonkeyDevice
可以使用newdevice = MonkeyRunner.waitForConnection()
獲取MonkeyDevice對(duì)象
void broadcastIntent (string uri, string action, string data, string mimetype, iterable categories dictionary extras, component component, iterable flags)
//Broadcasts an Intent to this device, as if the Intent were coming from an application.
// 向設(shè)備發(fā)送一個(gè)廣播
void drag (tuple start, tuple end, float duration, integer steps)
// Simulates a drag gesture (touch, hold, and move) on this device's screen.
// 拖動(dòng)
object getProperty (string key)
//Given the name of a system environment variable, returns its value for this device. The available variable names are listed in the detailed description of this method.
//object getSystemProperty (string key)
//The API equivalent of adb shell getprop <key>. This is provided for use by platform developers.
// 獲取一個(gè)屬性
void installPackage (string path)
//Installs the Android application or test package contained in packageFile onto this device. If the application or test package is already installed, it is replaced.
//dictionary instrument (string className, dictionary args)
//Runs the specified component under Android instrumentation, and returns the results in a dictionary whose exact format is dictated by the component being run. The component must already be present on this device.
// 安裝一個(gè)apk啥供,參數(shù)是apk的路徑
void press (string name, dictionary type)
//Sends the key event specified by type to the key specified by keycode.
// 發(fā)送一個(gè)按鈕事件
void reboot (string into)
//Reboots this device into the bootloader specified by bootloadType.
// 重啟設(shè)備
void removePackage (string package)
//Deletes the specified package from this device, including its data and cache.
//根據(jù)apk的package刪除app
object shell (string cmd)
//Executes an adb shell command and returns the result, if any.
// 執(zhí)行一個(gè)shell命令
void startActivity (string uri, string action, string data, string mimetype, iterable categories dictionary extras, component component, flags)
//Starts an Activity on this device by sending an Intent constructed from the supplied arguments.
//MonkeyImage takeSnapshot()
//Captures the entire screen buffer of this device, yielding a MonkeyImage object containing a screen capture of the current display.
// 啟動(dòng)一個(gè)activity
void touch (integer x, integer y, integer type)
//Sends a touch event specified by type to the screen location specified by x and y.
// 發(fā)送觸摸事件
void type (string message)
//Sends the characters contained in message to this device, as if they had been typed on the device's keyboard. This is equivalent to calling press() for each keycode in message using the key event type DOWN_AND_UP.
// 向設(shè)備輸入一些字符串
void wake ()
//Wakes the screen of this device.
// 喚醒設(shè)備
MonkeyImage
使用newimage = MonkeyDevice.takeSnapshot()
獲取MonkeyImage對(duì)象
string convertToBytes (string format)
// Converts the current image to a particular format and returns it as a string that you can then access as an iterable of binary bytes.
// 轉(zhuǎn)換格式
tuple getRawPixel (integer x, integer y)
// Returns the single pixel at the image location (x,y), as an a tuple of integer, in the form (a,r,g,b).
// 獲取像素值
integer getRawPixelInt (integer x, integer y)
// Returns the single pixel at the image location (x,y), as a 32-bit integer.
// 獲取像素值
MonkeyImage getSubImage (tuple rect)
// Creates a new MonkeyImage object from a rectangular selection of the current image.
// 獲取像素值
// 根據(jù)舉行獲取一個(gè)新的對(duì)象
boolean sameAs (MonkeyImage other, float percent)
// Compares this MonkeyImage object to another and returns the result of the comparison. The percent argument specifies the percentage difference that is allowed for the two images to be "equal".
// 比較2各圖片是相同
void writeToFile (string path, string format)
// Writes the current image to the file specified by filename, in the format specified by format.
// 保存圖片
總結(jié)
優(yōu)點(diǎn):
Monkeyrunner可以運(yùn)行多個(gè)device,能夠截屏库糠,能夠提供按鍵和輸入事件滤灯。可以基于python進(jìn)行擴(kuò)展開(kāi)發(fā)曼玩,功能強(qiáng)大鳞骤。
缺點(diǎn):
Monkeyrunner獲取控件是根據(jù)坐標(biāo)進(jìn)行定位的,不同的設(shè)備的坐標(biāo)不一樣黍判,獲取不穩(wěn)定豫尽。
Monkey與Monkeyrunner差別
- Monkey
Monkey工具直接運(yùn)行在設(shè)備或模擬器的adb shell中,生成用戶或系統(tǒng)的偽隨機(jī)事件流顷帖。 - Monkeyrunner
Monkeyrunner工具是在工作站上通過(guò)API定義的特定命令和事件控制設(shè)備或模擬器美旧。
相關(guān)鏈接
https://developer.android.com/studio/test/monkeyrunner/index.html
http://www.cnblogs.com/findyou/p/3420936.html