使用SDK自帶的NotePad應(yīng)用作為實(shí)踐目標(biāo)應(yīng)用,目的是對(duì)MonkeyDevice擁有的成員方法做一個(gè)初步的了解。
成都創(chuàng)新互聯(lián)公司網(wǎng)站建設(shè)公司,提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè),網(wǎng)頁(yè)設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專(zhuān)業(yè)做網(wǎng)站服務(wù);可快速的進(jìn)行網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專(zhuān)業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,是專(zhuān)業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
以下是官方列出的方法的Overview。
Return Type | Methods | Comment |
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. |
|
void | drag (tuple start, tuple end, float duration, integer steps) Simulates a drag gesture (touch, hold, and move) on this device's screen. |
|
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. |
|
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. | Obsolete,返回值是Boolean |
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. |
|
void | press (string name, dictionary type) Sends the key event specified by type to the key specified by keycode. |
|
void | reboot (string into) Reboots this device into the bootloader specified by bootloadType. |
|
void | removePackage (string package) Deletes the specified package from this device, including its data and cache. | Obsolete,返回值是Boolean |
object | shell (string cmd) Executes an adb shell command and returns the result, if any. |
|
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. |
|
void | touch (integer x, integer y, integer type) Sends a touch event specified by type to the screen location specified by x and y. |
|
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 callingpress() for each keycode in message using the key event type DOWN_AND_UP. |
|
void | wake () Wakes the screen of this device. |
|
其實(shí)官方這個(gè)表是沒(méi)有及時(shí)更新的,我現(xiàn)在手頭上用到的MonkeyRunner是當(dāng)前最新的,里面就擁有好幾個(gè)官網(wǎng)沒(méi)有列出來(lái)的API,我懷疑是不是自從UIAutomator在03年出來(lái)后,google就不打算再繼續(xù)維護(hù)MonkeyRunner了?如果有朋友知道事實(shí)的話,還麻煩告知。
以下是我整理出來(lái)的源碼多出來(lái)的可用公共API列表
Return Type | Methods | Comment |
HierarchyViewer | getHierarchyViewer(PyObject args[], String kws[]) 獲取一個(gè)HierarchyViewer對(duì)象 | 請(qǐng)查看《MonkenRunner通過(guò)HierarchyViewer定位控件的方法和建議》 |
PyList | getPropertyList(PyObject args[], String kws[]) | 取得所有的property屬性鍵值 |
PyList | getViewIdList(PyObject args[], String kws[]) | Failed |
MonkeyView | getViewById(PyObject args[], String kws[]) | Failed |
MonkeyView | getViewByAccessibilityIds(PyObject args[], String kws[]) | Failed |
MonkeyView | getRootView(PyObject args[], String kws[]) | Failed |
PyList | getViewsByText(PyObject args[], String kws[]) | Failed |
但可惜的是在本人嘗試以上多出來(lái)的API的時(shí)候,發(fā)現(xiàn)除了最上面兩個(gè)可用之外,其他的都不可用并拋出錯(cuò)誤。且網(wǎng)上資料少的可憐,別人碰到同樣的問(wèn)題也找不到解決辦法。所以本人懷疑這些“隱藏”API是不是并沒(méi)有完善,或者說(shuō)google不準(zhǔn)備完善,所以才沒(méi)有列出到官網(wǎng)上面去。本人用的SDK tools和Platform tools已經(jīng)是當(dāng)前最新的23.0.2和20.
一個(gè)臺(tái)灣網(wǎng)友碰到的問(wèn)題描述:http://imsardine.simplbug.com/note/monkeyrunner/api/hierarchy-viewer.html
(string uri, string action,string data, string mimetype, iterable categories dictionary extras, componentcomponent, iterable flags)
本人理解的此方法的本意是想廣播一個(gè)Intent給我們的AndroidDevice,目標(biāo)應(yīng)用接收到該Intent做相應(yīng)的處理,比如打開(kāi)一個(gè)Activity等。但在我的多次嘗試下并沒(méi)有成功!
targetDevice.broadcastIntent(action='android.intent.action.INSERT', mimetype='vnd.android.cursor.dir/contact', extras = {'name':'user1501488', 'phone':'123-15489'}
如果使用同樣的參數(shù),使用下面的startActivity是沒(méi)有問(wèn)題的。
targetDevice.startActivity(action='android.intent.action.INSERT', mimetype='vnd.android.cursor.dir/contact', extras = {'name':'user1501488', 'phone':'123-15489'})
google了半天網(wǎng)上根本找不到這個(gè)方法的使用例子,倒是stackOverFlow上有人建議用Shell來(lái)達(dá)到同樣的效果。
targetDevice.shell("am start -a android.intent.action.INSERT -t vnd.android.cursor.dir/contact -e name 'Donald Duck' -e phone 555-1234").
所以可見(jiàn)這個(gè)方法并沒(méi)有多少人在用,原因應(yīng)該是它完全可以用上面介紹的兩個(gè)方法替代。
(string uri, string action, string data, string mimetype, iterable categories dictionary extras, component component, flags)
targetDevice.startActivity(action='android.intent.action.VIEW', mimetype='vnd.android.cursor.dir/vnd.google.note')
targetDevice.startActivity(component="com.example.android.notepad/com.example.android.notepad.NotesList")
targetDevice.startActivity(action='android.intent.action.INSERT', mimetype='vnd.android.cursor.dir/contact', extras = {'name':'user1501488', 'phone':'123-15489'})
這個(gè)方法的目的是按住一個(gè)控件然后把她拖動(dòng)到其他位置
viewer = targetDevice.getHierarchyViewer() note = viewer.findViewById('id/text1') point = viewer.getAbsoluteCenterOfView(note) startX = point.x startY = point.y targetDevice.drag((startX,startY),(startX,startY),1) targetDevice.press('KEYCODE_BACK', MonkeyDevice.DOWN_AND_UP)
以上示例是通過(guò)drag的方法來(lái)模擬LongPress,只要把參數(shù)中的起始坐標(biāo)和目標(biāo)坐標(biāo)都設(shè)置成同樣一個(gè)值,然后時(shí)常設(shè)置成一個(gè)有效的值就好了。
通過(guò)環(huán)境變量的key來(lái)獲得其對(duì)應(yīng)的值。以下是官方提供的可用化境變量列表
Property Group | Property | Description | Notes |
build | board | Code name for the device's system board | See Build |
brand | The carrier or provider for which the OS is customized. |
| |
device | The device design name. |
| |
fingerprint | A unique identifier for the currently-running build. |
| |
host |
|
| |
ID | A changelist number or label. |
| |
model | The end-user-visible name for the device. |
| |
product | The overall product name. |
| |
tags | Comma-separated tags that describe the build, such as "unsigned" and "debug". |
| |
type | The build type, such as "user" or "eng". |
| |
user |
|
| |
CPU_ABI | The name of the native code instruction set, in the form CPU type plus ABI convention. |
| |
manufacturer | The product/hardware manufacturer. |
| |
version.incremental | The internal code used by the source control system to represent this version of the software. |
| |
version.release | The user-visible name of this version of the software. |
| |
version.sdk | The user-visible SDK version associated with this version of the OS. |
| |
version.codename | The current development codename, or "REL" if this version of the software has been released. |
| |
display | width | The device's display width in pixels. | SeeDisplayMetricsfor details. |
height | The device's display height in pixels. |
| |
density | The logical density of the display. This is a factor that scales DIP (Density-Independent Pixel) units to the device's resolution. DIP is adjusted so that 1 DIP is equivalent to one pixel on a 160 pixel-per-inch display. For example, on a 160-dpi screen, density = 1.0, while on a 120-dpi screen, density = .75. The value does not exactly follow the real screen size, but is adjusted to conform to large changes in the display DPI. See density for more details. |
| |
am.current | package | The Android package name of the currently running package. | The am.currentkeys return information about the currently-running Activity. |
action | The current activity's action. This has the same format as the name attribute of the action element in a package manifest. |
| |
comp.class | The class name of the component that started the current Activity. See comp.package for more details. |
| |
comp.package | The package name of the component that started the current Activity. A component is specified by a package name and the name of class that the package contains. |
| |
data | The data (if any) contained in the Intent that started the current Activity. |
| |
categories | The categories specified by the Intent that started the current Activity. |
| |
clock | realtime | The number of milliseconds since the device rebooted, including deep-sleep time. | SeeSystemClock for more information. |
displayWidth =targetDevice.getProperty ('display.width') printdisplayWidth.encode('utf-8') displayHight =targetDevice.getProperty('display.width') printdisplayHight.encode('utf-8')
以上示例的目的是獲得目標(biāo)設(shè)備的長(zhǎng)和高,當(dāng)我們使用坐標(biāo)點(diǎn)來(lái)操作控件的時(shí)候,調(diào)試的時(shí)候在一臺(tái)機(jī)器上通過(guò)了,但是如果換了另外一個(gè)屏幕大小不一樣的機(jī)器的話就會(huì)失敗,因?yàn)榭丶淖鴺?biāo)點(diǎn)位置可能就變了。這個(gè)時(shí)候我們就需要用到示例中的連個(gè)屬性來(lái)動(dòng)態(tài)計(jì)算控件在不同屏幕大小的設(shè)備上面的坐標(biāo)點(diǎn)了。
這里需要注意參數(shù)應(yīng)該填寫(xiě)的格式是以上列表中前兩列的組合PropertyGroup.Property
displayWidth = targetDevice.getSystemProperty ('service.adb.tcp.port') print displayWidth.encode('utf-8')
根據(jù)官網(wǎng)的描述,這個(gè)函數(shù)和getProperty函數(shù)應(yīng)該有同樣的功能(Synonym for getProperty().),使用的屬性表也如上面的屬性列表一樣。但是按照我的實(shí)踐并非如此,不過(guò)它確實(shí)如官方描述的等同于命令“adb shell getprop <key>.”倒是真的。
以上的例子是獲取adb這個(gè)服務(wù)所打開(kāi)的TCP端口,等同于如下的shell命令:“adb shell getprop service.adb.tcp.port“
如果我嘗試使用下面的方法去獲得設(shè)備的長(zhǎng)度,返回的結(jié)果其實(shí)會(huì)是None
displayWidth = targetDevice.getSystemProperty ('display.width') print displayWidth.encode('utf-8')
if True == targetDevice.installPackage('D:\\Projects\\Workspace\\PythonMonkeyRunnerDemo\\apps\\MPortal.apk'): print "Installationfinished successfully" else: print "Failedto install the apk"
這里有兩點(diǎn)需要注意的:
注意這里碰到一個(gè)路徑問(wèn)題,如果我的路徑寫(xiě)成:
'D:\Projects\Workspace\PythonMonkeyRunnerDemo\apps\MPortal.apk'
那么會(huì)出現(xiàn)下圖這樣的錯(cuò)誤:
作者
自主博客
微信
CSDN
天地會(huì)珠海分舵
http://techgogogo.com
服務(wù)號(hào):TechGoGoGo
掃描碼:
MonkeyDevcieAPI實(shí)踐全記錄
地址分享:http://bm7419.com/article48/gejchp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、網(wǎng)站營(yíng)銷(xiāo)、Google、服務(wù)器托管、定制網(wǎng)站、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容