一、MonkeyRunner:Android SDK中自带的一个黑盒测试工具在PC端通过Android API控制设备的运行
支持Python脚本,可以实现Monkey无法实现的一些逻辑控制
包含:MonkeyRunner:通用的一些方法的模块、MonkeyDevice:控制设备和模拟器相关操作的模块、MonkeyImage:屏幕头像功能相关的模块。
1、MonkeyRunner:SDK–>tools–>MonkeyRunner.bat
导入MonkeyRunner,
from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage
help的使用方法:MonkeyRunner.help("text")--这里可能会报not resources的错误,此时需要把help的一些资源resources目录放到tools/lib目录下即可。
content=MonkeyRunner.help("html")
f=open("help.html","w")
f.write(content)
f.close() - -将help文件生成html格式,保存到tools目录下
alert使用方法:
MonkeyRunner.alert('MonkeyRunner alert','aaaaaa','OK')
choice使用方法:
MonkeyRunner.choice('MonkeyRunner choice',['test1','test2'],'imooc')
input使用方法:
MonkeyRunner.input('MonkeyRunner input','init str','imooc','OK','Cancel')
二、MonkeyDevice模块(如果出现连接失败,则显示adb devices检查连接状态):
from com.android.monkeyrunner import MonkeyRunner as mr,MonkeyDevice as md
1.与设备进行通讯:
device = mr.waitForConnection()
2.安装app应用:
device.installPackage("D:/app/app_name.apk"),路径
3.查看包名:
device.shell("pm list package | grep pkg_name")已安装的某的应用的app名
4.删除应用
device.removePackage("pak_name"),参数为包名
5.启动应用:(以启动计算器为例)
package = "com.android.calculator2"
activity = “com.android.calcculator2.Calculator”
runComponent = package +'/' +activity
device.startActivity(runComponent)
计算器按键操作:
device.press("KEYCODE_7",md.DOWN_AND_UP)
device.type("+")
device.touch(400,500,md.DOWN_AND_UP)
6.获取属性:(分辨率)
device.getProperty("display.height")
device.getProperty("display.width")
三、MonkeyImage模块:
from com.android.monkeyrunner import MonkeyRunner as mr,MonkeyImage as mi
device = mr.waitForConnection()
image = device.takeSnapshot() 获取MonkeyImage对象
1.获取的图像以字节码的形式返回:
image.convertToBytes()
2.获取给定坐标的像素点值:
image.getRawPixel(100,100)
3.获取坐标点整型的返回值
image.getRawpixelInt(100,100)
4.截取图片:190 196
subimage=inage.getSubImage((356,456,190,196))坐标和高度宽度
subimage.writeToFile('ytt.png','png')名称和格式
比较两个图片相似度:
subimage1.sameAs(subimage2,0.9)相似度值为0.9