monkeyRunner
MonkeyRunner工具是使用Jython(使用Java编程语言实现的Python)写出来的,它提供了多个API,通过monkeyrunner API 可以写一个Python的程序来模拟操作控制Android设备app,测试其稳定性并通过截屏可以方便地记录出现的问题。
MonkeyRunner 和 monkey没有直接关系,monkey是在设备直接运行adb shell命令生成随机事件来进行测试的。而monkeyrunner则是通过API发送特定的命令和事件通过工作站来控制设备。
优势:
1.多设备控制:API可以跨多个设备,一次启动全部模拟器来实施测试套件;
2.功能测试:为应用自动执行一次功能测试,然后观察输出结果的截屏。
2.可扩展自动化:因为monkeyrunner是一个API工具包,你可以开发基于Python模块的整个系统来控制Android设备;
API
MonkeyRunner工具主要有三个类:MonkeyRunner、MonkeyDevice、MonkeyImage.
类 |
主要方法 |
MonkeyRunner |
alert() waitForConnection(float timeout,string deviceid) sleep() help |
MonkeyDevice |
installPackage() starActivity(package+'/'+activity) touch(integer x,integer y, integer type) drag(tuple start,tuple end,float duration,integer steps) press(string keycode,dictionary type) takesnapshot getProperty |
MonkeyImage |
ConvertoBytes getSubImage sameAs(MonkeyImage other,float percent) 可通过截图方法判断两次结果是否一致 writetoFile(string path,string format) |
monkeyRunner示例代码:
from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage
from com.android.monkeyrunner.easy import EasyMonkeyDevice,By device = MonkeyRunner.waitForConnection() print '******Case1: Use MonkeyDevice and MonkeyImage to check claculator result******' print '---- start Calculator App' device.startActivity('com.android.calculator2/.Calculator') print '---- calculator 3*8 with press method' device.press('KEYCODE_3',MonkeyDevice.DOWN_AND_UP)
device.press('KEYCODE_NUMPAD_MULTIPLY',MonkeyDevice.DOWN_AND_UP)
device.press('KEYCODE_8',MonkeyDevice.DOWN_AND_UP)
device.press('KEYCODE_EQUALS',MonkeyDevice.DOWN_AND_UP) image = device.takeSnapshot()
subimage = image.getSubImage((300,50,356,234)) print '---- calculator 4*6 with touch method'
device.touch(100,600,MonkeyDevice.DOWN_AND_UP)
device.touch(600,600,MonkeyDevice.DOWN_AND_UP)
device.touch(400,600,MonkeyDevice.DOWN_AND_UP)
device.touch(400,1000,MonkeyDevice.DOWN_AND_UP) image2 = device.takeSnapshot()
subimage2 = image.getSubImage((300,50,356,234)) if (subimage2.sameAs(subimage,0.8)):
print '[PASS] the result of 3*8 and 4*6 is equal!'
else:
print '[Fail] the result of 3*8 and 4*6 is not equal!' print '******Case2: Use EasyMonkeyDevice to check claculator result******' print '---- calculator 5*7 with EasyMonkeyDevice touch' easy = EasyMonkeyDevice(device)
easy.touch(By.id('id/digit5'),MonkeyDevice.DOWN_AND_UP)
easy.touch(By.id('id/mul'),MonkeyDevice.DOWN_AND_UP)
easy.touch(By.id('id/digit7'),MonkeyDevice.DOWN_AND_UP)
easy.touch(By.id('id/equal'),MonkeyDevice.DOWN_AND_UP) hv=device.getHierarchyViewer()
view = hv.findViewById('id/display')
str =view.children[1].namedProperties.get('text:mText').toString().encode('utf8') if (str == ''):
print '[PASS] the result of 5*7 is correct!'
else:
print '[Fail] the result of 5*7 is correct! the result is -- ' +str easy.touch(By.id('id/clear'),MonkeyDevice.DOWN_AND_UP)
device.press('KEYCODE_BACK',MonkeyDevice.DOWN_AND_UP)
最新文章
- 【J2EE入门】13个规范
- volatile 关键字
- springMvc源码学习之:spirngMVC获取请求参数的方法2
- EDM营销算法:python自动批量发邮件
- [原]Django调试工具--django-debug-toolbar
- 由“大数据量Excel入库高效方式”瞥见“并联系统”之优势
- 【Linux高频命令专题(1)】sort
- EventBus使用小记
- (hdu)1042 N! 大数相乘
- 【video】m3u8
- HTML&;CSS基础学习笔记1.28-给网页添加一个css样式
- ASIHTTPRequest 详解 例子
- sql 时间和字符串 取到毫秒级
- nginx做下载限速
- windows 多任务与进程
- vue通过自定义指令 v-py 将名字转拼音
- <;a>;标签中href=";javascript:;";** 为什么 style不用src**
- MHA-Failover(GTID,Auto_Position=0)
- mysql 索引无法使用问题
- VS2012 生成项目报 ";Lc.exe已退出,代码为-1"; 错误
热门文章
- android4.4 evaluateJavascript 到android2.X上不能调用的问题
- mysql + php 中文乱码 全是? 解决方法
- 获取当前外网IP地址
- crm操作销售订单实体
- Hadoop - YARN 概述
- 采用ADM2582E/ADM2587E实现完全半/全双工的RS-485/RS-422接口隔离
- ffmpeg强制使用TCP方式推流到EasyDarwin开源流媒体服务器进行直播
- python中的括号以及元组和列表的区别
- 统计 与 数学 induction 归纳 deduction 演绎 吴喜之老师
- hibernate属性配置