MonkeyRunner解析

步骤;
from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice
devices = MonkeyRunner.waitForConnection()
devices.installPackage()
devices.startActivity('')
result = devices.takeSnapShot()
result.writeToFile('','png')


解析:调用MonkeyRunner的waitxxx方法之后,返回一个MonkeyDevice对象devices
通过devices对象,我们可以安装apk 启动activity 发送点击事件 以及截图 
也就是说重点在于MonkeyDevice这个对象api的使用
为了方便,我把这里人为分为3类  
事件类
    press
    touch
    drag
    type()
系统类
    void broadcastIntent (string uri, string action, string data, string mimetype, iterable categories dictionary extras, component component, iterable flags)
    installPackage(String path)
    removePackage(String package)
    instrument   启动单元测试
    reboot()
    wake()
    startActivity()
    MonkeyImage takeSnapshot()      截图得到结果后调用MonkeyImage的writeToFile()保存本地,这个类也提供一个图片相似比较的功能,作为自动化结果判断
    object shell(String cmd)       神器,模拟shell命令,自带返回值(commands.getoutput())  
信息类
    object getProperty(String key)    获取系统信息,包括屏幕密度 宽度等等
    object getSystemProperty(String key)  同getprop
    
这里再介绍一下MonkeyImage图片处理相关的一些api
    wirteToFile(String path,String format)
    sameAs(MonkeyImage other,float percent)
    getSubImage(tuple rect)    根据xywh截断图片
    getRawPexel(x,y)取得某个位置的像素  
    getRawPixelInt(x,y)  很少用
    convertToBytes  转为二进制,返回一个迭代器
重要的就是写到本地文件以及比较两个图片的相似度了,其它的裁剪取色没想到有什么用。    


最后补充一下MonkeyRunner的相关api吧,按照重要性排行
    waitForConnection()         返回MonkeyDevice,核心方法
    string input (string message, string initialValue, string title, string okTitle, string cancelTitle)   弹出输入框,输入字符
    integer choice (string message, iterable choices, string title)   Displays a dialog with a list of choices to the process running the current program.  弹出下拉框,返回的是int类型
    void alert (string message, string title, string okTitle)  警告框  
    sleep    线程停止x秒 
    help   不知道干啥的  :)
    
Table 1.Property variable names used with getProperty() and getSystemProperty().




总结一下:
MonkeyRunner使用比较简单,了解3个核心的类,也就是import的3个
MonkeyRunner | MonkeyDevice | MonkeyImage
MonkeyRunner提供了初始化的输入框,休眠,以及连接设备的方法
MonkeyDevice提供了界面操作,获取系统信息以及一些系统级别操作的功能(最牛逼的shell不要忘了)
MonkeyImage提供了图片导出,图片对比的功能
核心代码:
from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage
devices = MonkeyRunner.waitForConnection()
...
...
result = devices.takeSnapshot()
...


了解上面的重要作用以及api后,下面再来介绍另一个EasyMonkeyDevice,累了,不写了...
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值