Android monkeyrunner整理

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市贬墩,隨后出現(xiàn)的幾起案子榴嗅,更是在濱河造成了極大的恐慌,老刑警劉巖陶舞,帶你破解...
    沈念sama閱讀 221,406評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件嗽测,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡肿孵,警方通過(guò)查閱死者的電腦和手機(jī)唠粥,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,395評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)停做,“玉大人晤愧,你說(shuō)我怎么就攤上這事◎入纾” “怎么了官份?”我有些...
    開(kāi)封第一講書(shū)人閱讀 167,815評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵只厘,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我舅巷,道長(zhǎng)懈凹,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,537評(píng)論 1 296
  • 正文 為了忘掉前任悄谐,我火速辦了婚禮介评,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘爬舰。我一直安慰自己们陆,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,536評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布情屹。 她就那樣靜靜地躺著坪仇,像睡著了一般。 火紅的嫁衣襯著肌膚如雪垃你。 梳的紋絲不亂的頭發(fā)上椅文,一...
    開(kāi)封第一講書(shū)人閱讀 52,184評(píng)論 1 308
  • 那天,我揣著相機(jī)與錄音惜颇,去河邊找鬼皆刺。 笑死,一個(gè)胖子當(dāng)著我的面吹牛凌摄,可吹牛的內(nèi)容都是我干的羡蛾。 我是一名探鬼主播,決...
    沈念sama閱讀 40,776評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼锨亏,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼痴怨!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起器予,我...
    開(kāi)封第一講書(shū)人閱讀 39,668評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤浪藻,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后乾翔,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體爱葵,經(jīng)...
    沈念sama閱讀 46,212評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,299評(píng)論 3 340
  • 正文 我和宋清朗相戀三年末融,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了钧惧。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片暇韧。...
    茶點(diǎn)故事閱讀 40,438評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡勾习,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出懈玻,到底是詐尸還是另有隱情巧婶,我是刑警寧澤乾颁,帶...
    沈念sama閱讀 36,128評(píng)論 5 349
  • 正文 年R本政府宣布,位于F島的核電站艺栈,受9級(jí)特大地震影響英岭,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜湿右,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,807評(píng)論 3 333
  • 文/蒙蒙 一诅妹、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧毅人,春花似錦吭狡、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,279評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至缔俄,卻和暖如春弛秋,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背俐载。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,395評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工蟹略, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人遏佣。 一個(gè)月前我還...
    沈念sama閱讀 48,827評(píng)論 3 376
  • 正文 我出身青樓科乎,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親贼急。 傳聞我的和親對(duì)象是個(gè)殘疾皇子茅茂,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,446評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,275評(píng)論 25 707
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn)太抓,斷路器空闲,智...
    卡卡羅2017閱讀 134,695評(píng)論 18 139
  • 經(jīng)常在網(wǎng)上看到各種關(guān)于吵架斗氣而開(kāi)車相撞的新聞,不論陌生人之間走敌,還是夫妻之間都有這種可能碴倾。總認(rèn)為他們傻掉丽,把那么好的...
    小丫屠閱讀 218評(píng)論 0 0
  • 今天是第n遍看永澄老師的《個(gè)人成長(zhǎng)的6條元規(guī)范》跌榔,對(duì)于這類講道理的文字已經(jīng)很久沒(méi)有遇到過(guò)能夠讀到心里去的,6條元規(guī)...
    magiccici婷婷閱讀 273評(píng)論 0 0
  • 很多人都在說(shuō)“迷茫”暂论。我第一次正式接觸這兩個(gè)字還是同哥的《誰(shuí)的青春不迷妹婧郑》。現(xiàn)在仔細(xì)想來(lái)取胎,迷茫的似乎不是個(gè)人展哭,而成...
    汪子皓閱讀 1,504評(píng)論 3 17