android 自动化测试之monkeyrunner学习(三),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运行

60374bff05f348c21fc93672e9c45261.png

a7fd8f6f6c52db1be9bde6e4ed1b2e49.png

三、MonkeyRunner

MonkeyRunner常用方法如下

1、waitForConnect(float timeout, string deviceId)

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

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

deviceId可通过adb devices查看

f7796e1201750032932523d4ba59ff6e.png

2、sleep(float seconds)

等待时长

MonkeyRunner.sleep(5)

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

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

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

结果如图:

6e021d6b1a3e16bb5514708644cc6655.png

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

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

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

553c7e529c40d91493d8db3ee0dd5ab6.png

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

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

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

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

5cc31457a375d76cef610b0468e3c883.png

四、MonkeyDevice

通常情况下,不必创建MonkeyDevice的实例。相反,您使用MonkeyRunner.waitForConnection()从到设备或仿真器的连接创建新对象。

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

1、常量

Constants

string

Use this with the type argument of

string

Use this with the type argument of

string

Use this with the type argument of

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

. The API equivalent of adb shell getprop . This is provided for use by platform developers.

void

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.

Captures the entire screen buffer of this device, yielding a

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 callingmessage using the key event type DOWN_AND_UP.

void

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 importMonkeyRunner, 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‘)

原文:https://www.cnblogs.com/fancy0158/p/10068845.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值