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运行

Android自动化测试之——MonkeyRunner(2)

 

Android自动化测试之——MonkeyRunner(2)

 

三、MonkeyRunner

MonkeyRunner常用方法如下

1、waitForConnect(float timeout, string deviceId)

waitForConnection ()方法返回一个MonkeyDevice对象

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

deviceId可通过adb devices查看

Android自动化测试之——MonkeyRunner(2)

 2、sleep(float seconds)

等待时长

MonkeyRunner.sleep(5)

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

向运行当前程序的进程显示警报对话框。对话框是模态的,所以程序暂停,直到用户点击对话框的按钮。

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

结果如图:

Android自动化测试之——MonkeyRunner(2)

4、choice(string message, iterable choices, string title)

在运行当前程序的进程中显示一个具有选择列表的对话框。对话框是模态的,所以程序暂停,直到用户点击对话框中的一个按钮。

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

Android自动化测试之——MonkeyRunner(2)

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

显示一个接受输入并将其返回给程序的对话框。对话框是模态的,所以程序暂停,直到用户点击对话框中的一个按钮。

对话框包含两个按钮,一个显示okTitle值,另一个显示cancelTitle值。如果用户单击OKTITE按钮,则返回输入框的当前值。如果用户单击取消标题按钮,则返回一个空字符串。

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

 Android自动化测试之——MonkeyRunner(2)

四、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 callingpress() 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)

 

Android自动化测试之——MonkeyRunner(2)

上一篇:记录Appium-desktop踩过的坑could not find devices


下一篇:AXIOS 的请求