MonkeyRunner常用方法

MonkeyRunner常用方法

monkeyrunner工具从外部代码为用来控制Android设备或模拟器的程序编写提供一个API。通过monkeyrunner ,你可以编写一个Python程序:安装android应用或者测试程序包、运行程序、发送键盘指令到程序、给程序界面截图、保存截图到工作站。monkeyrunner工具最初是设计用于在功能/框架层次测试应用和设备和执行单元套件测试的,但是你也可以自由地用于其他目的

monkeyrunner工具和 UI/Application Exerciser Monkey(—也被称为monkey工具)是不相关的。monkey工具直接运行在设备和模拟器的一个adb shell 中,产生用户级别或者系统级别的伪随机流事件。相比而言,monkeyrunner工具是从工作站通过一个API发送具体的命令或者事件来控制设备和模拟器的。

monkeyrunner工具Android测试提供了以下这些独特的特性:

多设备控制:monkeyrunner API可以应用一个或多个测试套件跨多个设备或模拟器。您可以在同一时间手动接上所有的设备或启动模拟器(或两者一起),按编程方式依次连接到每一个设备,然后运行一个或多个测试。你也可以以编程方式启动一个模拟器配置,运行一个或多个测试,然后关闭模拟器。

功能测试:monkeyrunner可以运行一个自动化的开始到结束的Android应用测试。你可以提供包含按键和触摸事件的输入值,以截图的方式观察结果视图。

回归测试:monkeyrunner可以通过稳定地运行一个应用程序来测试应用程序,且和一组已知是正确的截图比较其输出的截图。

可扩展的自动化:因为monkeyrunner是 API工具包,您可以开发整个系统基于python的模块和程序用来控制安卓设备。除了使用monkeyrunner API本身,您可以使用标准的Python os和子流程模块来调用Android工具如:ADB。

monkeyrunner包括以下三个模块

1、MonkeyRunner:这个类提供了用于连接monkeyrunner和设备或模拟器的方法,它还提供了用于创建用户界面显示提供了方法。
2、MonkeyDevice:代表一个设备或模拟器。这个类为安装和卸载包、开启Activity、发送按键和触摸事件、运行测试包等提供了方法。
3、MonkeyImage:这个类提供了捕捉屏幕的方法。这个类为截图、将位图转换成各种格式、对比两个MonkeyImage对象、将image保存到文件等提供了方法。

运行monkeyrunner

命令语法为:

monkeyrunner -plugin <plugin_jar> <program_filename> <program_options>

方式一:在CMD命令窗口直接运行monkeyrunner

方式二:使用Python编写测试代码文件,在CMD中执行monkeyrunner Findyou.py运行

不论使用哪种方式,您都需要调用SDK目录的tools子目录下的monkeyrunner命令。

注意:在运行monkeyrunner之前必须先运行相应的模拟器或连接真机,否则monkeyrunner无法连接到设备

编写基于python的测试代码先引入monkeyrunner模块

from com.android.monkeyrunner import MonkeyRunner 
from com.android.monkeyrunner import MonkeyDevice 
from com.android.monkeyrunner import MonkeyImage
from com.android.monkeyrunner.easy import EasyMonkeyDevice as emd
from com.android.monkeyrunner.easy import By

连接到设备或模拟器

参数1:超时时间,单位秒,浮点数。默认是无限期地等待。
参数2:串deviceid,指定的设备名称。默认为当前设备(手机优先,比如手机通过USB线连接到PC、其次为模拟器)。

默认

device = MonkeyRunner.waitForConnection()

指定设备和等待时间


device = MonkeyRunner.waitForConnection(1.0,'4df74b8XXXXXXX') 

向设备或模拟器安装APK

以下两种方式都是对的

device.installPackage('E:/JAVA/monkeyrunner/Test1/ThinkDrive_new.apk')
device.installPackage('E:\\JAVA\\monkeyrunner\\Test1\\ThinkDrive_new.apk')

参数可以为绝对路径,也可为相对路径

卸载设备或模拟器中的APK

参数为APK包名

device.removePackage('cn.richinfo.thinkdrive') 

以下是简单的monkeyrunner实际应用中常用的方法:

1、com.android.monkeyrunner.MonkeyRunner.alert
执行当前脚本弹出一个警示对话框,用户关闭对话框后脚本才结束。
用法:
MonkeyRunner.alert(message,title,okTitle)
message:会话弹出的内容
title:会话标题,默认为alert
okTitle:会话确认按钮,默认为ok
返回值:nothing。

2、com.android.monkeyrunner.MonkeyDevice.broadcastIntent
对设备发送一个广播信号。
用法:MonkeyDevice.broadcastIntent(uri,action,data,mimetype,categories,extras,component,flags)
uri:信号的uri
action:
data
mimetype
categories
extras
component
flags
返回值: nothing

3、com.android.monkeyrunner.MonkeyRunner.choice
显示一个对话框,让用户从一列选项中选择一个单一的选项。
用法:MonkeyRunner.choice(message,choices,title)
message:显示在对话框中的提示信息。
choices:一个迭代的包含一系列选择的python类型
title:对话框的标题,默认为input
返回值: If the user makes a selection and clicks the “OK” button, the method returns the 0-based index of the selection within the iterable. If the user clicks the “Cancel” button, the method returns -1.

4、com.android.monkeyrunner.MonkeyImage.convertToBytes
将图片转换为其他特殊的格式,将结果作为字符串返回,用这个方法将像素存取为特殊的格式,输出的字符串是一种更好的表现。
用法:MonkeyImage.convertToBytes(format)
format:目标格式,默认值为png。
返回值: 目标图片格式的字符串

5、com.android.monkeyrunner.MonkeyDevice.drag
在设备屏幕上模拟拖曳。(可将开始和结束坐标设为相同值模拟长按动作)
用法:MonkeyDevice.drag(start,end,duration,steps)
start:拖曳开始坐标 - The starting point for the drag (a tuple (x,y) in pixels)
end:拖曳结束坐标点- The end point for the drag (a tuple (x,y) in pixels
duration:持续时间 - Duration of the drag in seconds (default is 1.0 seconds)
steps:拖曳步骤- The number of steps to take when interpolating points. (default is 10)
Returns: returns nothing.

6、com.android.monkeyrunner.MonkeyDevice.getHierarchyViewer
获取设备的显示层次
用法:MonkeyDevice.getHierarchyViewer()
返回值:一个HierarchyViewer类型的对象。

7、com.android.monkeyrunner.MonkeyDevice.getProperty
在设备上给出变量的名称,返回变量的值。
用法:MonkeyDevice.getProperty(key)
key:变量的名称(key列表参加)
返回值:变量的值The variable’s value

8、com.android.monkeyrunner.MonkeyImage.getRawPixel
在x,y位置处获取一个单个的ARGB像素,参数x,y都是基于0坐标,表示一个像素尺寸,x向右增益,y向下增益,这个方法返回一个数组。
用法:MonkeyImage.getRawPixel(x,y)
x:x偏移
y:y偏移
Returns: A tuple of (A, R, G, B) for the pixel. Each item in the tuple has the range 0-255.

9、com.android.monkeyrunner.MonkeyImage.getRawPixelInt
同上.getRawPixel,只是返回的是一个整型。
用法:MonkeyImage.getRawPixelInt(x,y)
x:x偏移
y:y偏移
Returns: An unsigned integer pixel for x,y. The 8 high-order bits are A, followedby 8 bits for R, 8 for G, and 8 for B.

10、com.android.monkeyrunner.MonkeyImage.getSubImage
复制一个图片的矩形区域。
用法:MonkeyImage.getSubImage(rect)
rect:A tuple (x, y, w, h),x,y指定矩形区域的左上角,w为矩形宽,h为矩形高
返回:一个表示复制区域的图形对象。a MonkeyImage object representing the copied region.

11、com.android.monkeyrunner.MonkeyDevice.getSystemProperty
getProperty的同义。
用法:MonkeyDevice.getSystemProperty(key)
key:系统变量的名称。
返回:系统变量的值

12、com.android.monkeyrunner.MonkeyRunner.help
显示monkeyrunner的API。(实际实验中该方法没能成功生成help文档)
用法:MonkeyRunner.help(format)
format:The desired format for the output, either ‘text’ for plain text or ‘html’ for HTML markup.
Returns: A string containing the help text in the desired format

13、com.android.monkeyrunner.easy.By.id
id - The identifier of the object.
返回值: returns nothing.

14、com.android.monkeyrunner.MonkeyRunner.input
显示一个接受的对话框,用户点击对话按钮中的一个菜结束。
用法:MonkeyRunner.input(message,initialValue,title,okTitle,cancelTitle)
message:对话框显示的信息。The prompt message to display in the dialog.
initialValue:提供给用户的初始化值,默认为空字符串。The initial value to supply to the user. The default is an empty string)
title:对话标题,默认为input。The dialog’s title. The default is ‘Input’
okTitle:The text to use in the dialog’s confirmation button. The default is ‘OK’.
cancelTitle:The text to use in the dialog’s ‘cancel’ button. The default is ‘Cancel’.
返回: The test entered by the user, or None if the user canceled the input;。

15、com.android.monkeyrunner.MonkeyDevice.installPackage
在设备上安装应用包,如果该包已经存在,那么体会掉原来的。
用法:MonkeyDevice.intallPackage(path)
path:安装包在本地的路径已经文件名。
返回值:如果安装成功返回true,否则false。

16、com.android.monkeyrunner.MonkeyDevice.instrument
运行测试设备的指定包。
用法:MonkeyDevice.instrument(className,args)
className:测试设备要执行的类,格式为packagename/classname,
参数:A map of strings to objects containing the arguments to pass to this instrumentation (default value is None).默认为空
返回:单个键值对,key-value,key为steam,value为包含测试输出的字符串。

17、com.android.monkeyrunner.MonkeyRunner.loadImageFromFile
从电脑中加载一个图片文件
用法:MonkeyRunner.loadImageFromFile(path)
path:文件的路径,路径为电脑端路径。
返回:表示指定文件的MonkeyImage对象。

18、com.android.monkeyrunner.MonkeyDevice.press
给指定key发送一个key事件。
用法:MonkeyDevice.press(name,type)
name:keycode(参考android.view.KeyEvent)
type:触摸事件类型,如DOWN_AND_UP。
return:nothing。

19、com.android.monkeyrunner.MonkeyDevice.reboot
重启指定设备进入指定加载模式。
用法:MonkeyDevice.reboot(into)
into:bootloader, recovery, or None
return:nothing

20、com.android.monkeyrunner.MonkeyDevice.removePackage
从设备删除指定的包,包括相关的数据已经缓存。
用法:MonkeyDevice.removePackage(package)
package:指定要删除的包名
return:删除成功则返回true。

21、com.android.monkeyrunner.MonkeyImage.sameAs
Compare this MonkeyImage object to aother MonkeyImage object.
用法:MonkeyImage.sameAs(other,percent)
other:其他MonkeyImage对象
percent:百分比,取值为0.0-1.0
return:如果一致返回true

22、com.android.monkeyrunner.MonkeyDevice.shell
执行adb shell命令并返回结果。
用法:MonkeyDevice.shell(cmd)
cmd:要执行的adb shell命令
return:命令的输出

23、com.android.monkeyrunner.MonkeyRunner.sleep
暂停当前运行进程指定的时间。
用法:MonkeyRunner.sleep(seconds)
seconds:暂停时间
return:nothing

24、com.android.monkeyrunner.MonkeyDevice.startActivity
在设备上开始一个活动
用法:MonkeyDevice.startActivity(uri,action,data,mimetype,categories,extras,component,flags)
设置一个变量包含被测试程序的包名
package = ‘com.example.android.myapplication’
设置一个变量包含被测程序的Activity
activity = ‘com.example.android.myapplication.MainActivity’
设置一个组件包含以上两个部分
runComponent = package + ‘/’ + activity
运行该组件
device.startActivity(component=runComponent)
return:nothings

25:com.android.monkeyrunner.MonkeyDevice.takeSnapshot
获取设备的屏幕缓存
用法:MonkeyDevice.takeSnapshot()
return:一个MonkeyImage对象。

26、com.android.monkeyrunner.MonkeyDevice.touch
对指定位置发送一个触摸事件
用法:MonkeyDevice.touch(x,y,type)
x:
y:
type:触摸事件类型。
return:nothing

27、com.android.monkeyrunner.MonkeyDevice.type
从键盘键入指定的字符串,这相当于字符串的每一个字符都执行一次press(keycode,DOWN_AND_UP)方法。
用法:MonkeyDevice.type(message)
message:发送到键盘的字符串
Returns: returns nothing.

28、com.android.monkeyrunner.MonkeyRunner.waitForConnection
等待电脑连接到设备。
用法:MonkeyRunner.waitForConnection(timeout,deviceId)
timeout:等待的时间,默认为无限期
deviceId:指定设备名称的一个规定表达式
Returns: 一个表示已经连接的设备对象,A MonkeyDevice object

29、com.android.monkeyrunner.MonkeyDevice.wake
唤醒设备的屏幕
用法:MonkeyDevice.wake()
Returns: returns nothing.

30、com.android.monkeyrunner.MonkeyImage.writeToFile
将MonkeyImage对象写入一个文件,
用法:MonkeyImage.writeToFile(path,format)
path:输入的文件名,可选择的包含路径
format:目标格式,默认为png。
return:如果输出成功返回true。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值