1. 程式人生 > >Android自動化測試之——MonkeyRunner(2)

Android自動化測試之——MonkeyRunner(2)

一、MonkeyRunner API

MonkeyRunner API包含了三個模組在com.android.monkeyruner包中:

1、MonkeyRunner

一類用於MonkeyRunner程式的實用方法。該類提供了一種將MonkeyRunner連線到裝置或模擬器的方法。它還提供了為monkeyrunner程式建立UI以及顯示內建幫助的方法

2、MonkeyDevice

表示裝置或模擬器。這個類提供了安裝和解除安裝包、啟動Activity、以及向app傳送鍵盤或觸控事件的方法。您還可以使用該類執行測試包。

3、MonkeyImage

表示螢幕捕獲影象。這個類提供了用於捕獲螢幕、將點陣圖影象轉換為各種格式、比較兩個MonkeyImage物件以及將影象寫入檔案的方法。

二、執行MonkeyRunner

有兩種方式可以編寫MonkeyRunner用例:

  • 在cmd視窗直接執行monkeyrunner
  • 使用Python編寫測試程式碼,在CMD中執行monkeyrunner test.py執行

 

 

三、MonkeyRunner

MonkeyRunner常用方法如下

1、waitForConnect(float timeout, string deviceId)

waitForConnection ()方法返回一個MonkeyDevice物件

device = MonkeyRunner.waitForConnection(3,'
127.0.0.1:62001')

deviceId可通過adb devices檢視

 2、sleep(float seconds)

等待時長

MonkeyRunner.sleep(5)

3、alert(string message, string title, string okTitle)

向運行當前程式的程序顯示警報對話方塊。對話方塊是模態的,所以程式暫停,直到使用者點選對話方塊的按鈕。

MonkeyRunner.alert('TestTitle', 'This is the message', 'OK')

結果如圖:

4、choice(string

message, iterable choices, string title)

在運行當前程式的程序中顯示一個具有選擇列表的對話方塊。對話方塊是模態的,所以程式暫停,直到使用者點選對話方塊中的一個按鈕。

MonkeyRunner.choice('TestChoice', ['choice1','choice2'], 'Title')

5、input(string message string initialValue, string title, string okTitle, string cancelTitle)

顯示一個接受輸入並將其返回給程式的對話方塊。對話方塊是模態的,所以程式暫停,直到使用者點選對話方塊中的一個按鈕。

對話方塊包含兩個按鈕,一個顯示okTitle值,另一個顯示cancelTitle值。如果使用者單擊OKTITE按鈕,則返回輸入框的當前值。如果使用者單擊取消標題按鈕,則返回一個空字串。

MonkeyRunner.input('message', 'initialValue', 'title', 'ok', 'cancel')

 

四、MonkeyDevice

通常情況下,不必建立MonkeyDevice的例項。相反,您使用MonkeyRunner.waitForConnection()從到裝置或模擬器的連線建立新物件。

device = MonkeyRunner.waitForConnection(3,'127.0.0.1:62001')

1、常量

Constants
string DOWN Use this with the type argument of press() or touch() to send a DOWN event.
string UP Use this with the type argument of press() or touch() to send an UP event.
string DOWN_AND_UP Use this with the type argument of press() or touch() to send a DOWN event immediately followed by an UP event.

2、常用方法:

press()、touch()、startActivity()、installPackage()、type()、drag()

方法詳細如下:

Methods
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.
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.
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 calling press() for each keycode in  message using the key event type  DOWN_AND_UP.
void wake () Wakes the screen of this device.

五、MonkeyImage

該類主要用於截圖

newimage = MonkeyDevice.takeSnapshot()
newimage.writeToFile('E:\\autoTest\\test_02.png','png')

 

如下是官網給的一個例子

# Imports the monkeyrunner modules used by this program
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice

# Connects to the current device, returning a MonkeyDevice object
device = MonkeyRunner.waitForConnection()

# Installs the Android package. Notice that this method returns a boolean, so you can test
# to see if the installation worked.
device.installPackage('myproject/bin/MyApplication.apk')

# sets a variable with the package's internal name
package = 'com.example.android.myapplication'

# sets a variable with the name of an Activity in the package
activity = 'com.example.android.myapplication.MainActivity'

# sets the name of the component to start
runComponent = package + '/' + activity

# Runs the component
device.startActivity(component=runComponent)

# Presses the Menu button
device.press('KEYCODE_MENU', MonkeyDevice.DOWN_AND_UP)

# Takes a screenshot
result = device.takeSnapshot()

# Writes the screenshot to a file
result.writeToFile('myproject/shot1.png','png')