MonkeyImage API 实践全记录
1. 背景
鉴于网上使用MonkeyImage的实例除了方法sameAs外很难找到,所以本人把实践各个API的过程记录下来然自己有更感性的认识,也为往后的工作打下更好的基础。同时也和上一篇文章《MonkeyDevcie API 实践全记录》起到相互呼应的作用。
因为并没有MonkeyRunner的项目背景,所以这里更多的是描述各个API是怎么一回事,而不是描述在什么场景下需要用到。也就是说是去回答What,而不是How。 首先我们先看下官方给出的MonkeyImage的API描述,对比我现在反编译的最新的源码是一致的:
|
Return Type |
Methods |
Comment |
|
string |
convertToBytes (string format)Converts the current image to a particular format and returns it as a string that you can then access as an iterable of binary bytes. | |
|
tuple |
getRawPixel (integer x, integer y)Returns the single pixel at the image location (x,y), as an a tuple of integer, in the form (a,r,g,b). | |
|
integer |
getRawPixelInt (integer x, integer y)Returns the single pixel at the image location (x,y), as a 32-bit integer. | |
getSubImage (tuple rect)Creates a new MonkeyImage object from a rectangular selection of the current image. |
||
|
boolean |
sameAs (MonkeyImage other, float percent)Compares this MonkeyImage object to another and returns the result of the comparison. Thepercent argument specifies the percentage difference that is allowed for the two images to be "equal". |
|
|
void |
writeToFile (string path, string format)Writes the current image to the file specified by filename, in the format specified by format. |
2. String convertToBytes(string format)
2.1 示例
img = device.takeSnapshot()
png1 = img.convertToBytes()
png2 = img.convertToBytes()
bmp = img.convertToBytes('bmp')
jpg = img.convertToBytes('JPG')
gif = img.convertToBytes('gif')
raw = img.convertToBytes('raw')
invalid = img.convertToBytes('xxx')
#is the 2 pngs equal?
print "Two png is equal in bytes:",png1 == png2
#is the png equals to bmp?
print "png and bmp is equal in bytes:", png1 == bmp
#is the jpg eqals to the raw?
print "jpg and bmp is equals in bytes:",jpg == bmp
#is the jpg eqals to the xxx?
print "jpg is a valid argument:",jpg != invalid
#is the gif eqals to the xxx?
print "gif is a valid argument:",gif != invalid
#is the bmp eqals to the xxx?
print "bmp is a valid argument:",bmp != invalid
#is the raw equas to xxxx? aims at checking whether argument 'raw' is invalid like 'xxx'
print 'raw is a valid argument:',raw != invalid
#would invalid argument drop to png by default?
print 'Would invalid argument drop to png by default:',png1 == invalid
输出:
2.2 分析
3. tuple getRawPixel(integer x, integer y)和Integer getRawPixelInt (integer x, integer y)
3.1 示例
viewer = device.getHierarchyViewer()
note = viewer.findViewById('id/title')
text = viewer.getText(note)
print text.encode('utf-8') point = viewer.getAbsoluteCenterOfView(note)
x = point.x
y = point.y img = device.takeSnapshot() pixelTuple = img.getRawPixel(x,y)
pixelInt = img.getRawPixelInt(x,y)
print "Pixel in tuple:",pixelTuple
print "Pixel in int:", pixelInt
输出:
3.2 分析
4. MonkeyImage getSubImage(tuple rect)
4.1 示例
from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage
from com.android.monkeyrunner.easy import EasyMonkeyDevice,By
from com.android.chimpchat.hierarchyviewer import HierarchyViewer
from com.android.hierarchyviewerlib.models import ViewNode, Window
from java.awt import Point #from com.android.hierarchyviewerlib.device import #Connect to the target targetDevice
targetDevice = MonkeyRunner.waitForConnection() easy_device = EasyMonkeyDevice(targetDevice) #touch a button by id would need this
targetDevice.startActivity(component="com.example.android.notepad/com.example.android.notepad.NotesList") #invoke the menu options
MonkeyRunner.sleep(6)
#targetDevice.press('KEYCODE_MENU', MonkeyDevice.DOWN_AND_UP); '''
public ViewNode findViewById(String id)
* @param id id for the view.
* @return view with the specified ID, or {@code null} if no view found.
'''
#MonkeyRunner.alert("Continue?", "help", "Ok?") pic = targetDevice.takeSnapshot()
pic = pic.getSubImage((0,38,480,762)) newPic = targetDevice.takeSnapshot()
newPic = newPic.getSubImage((0,38,480,762)) print (newPic.sameAs(pic,1.0))
newPic.writeToFile('./shot1.png','png')
4.2 分析
- 打开NotePad的NotesList Activity
- 按下Menu Options按钮弹出“Add note”这个Menu Entry
- 截取一个屏幕
- 调用getSubImage来取得去掉屏幕最上面的状态栏(因为有时间不断变化,所以每截屏一次可能都会有所改变)和最下面的Menu Options的一个Image
- 再重复以上两个步骤取得另外一个Image
- 比较以上两个image是否相同
- 把第二个image写到本地。
5 boolean sameAs(MonkeyImage other, float percent)
5.1 示例
5.2 分析
6. void writeToFile (string path, string format)
6.1 示例
6.2 分析
| 作者 | 自主博客 | 微信服务号及扫描码 | CSDN |
| 天地会珠海分舵 | http://techgogogo.com | 服务号:TechGoGoGo扫描码: ![]() |
http://blog.csdn.net/zhubaitian |
MonkeyImage API 实践全记录的更多相关文章
- MonkeyDevcie API 实践全记录
1. 背景 使用SDK自带的NotePad应用作为实践目标应用,目的是对MonkeyDevice拥有的成员方法做一个初步的了解. 以下是官方列出的方法的Overview. Return Type ...
- Express+Mongoose(MongoDB)+Vue2全栈微信商城项目全记录(二)
用mogoose搭建restful测试接口 接着上一篇(Express+Mongoose(MongoDB)+Vue2全栈微信商城项目全记录(一))记录,今天单独搭建一个restful测试接口,和项目前 ...
- .Net Core Web Api实践之中间件的使用(一)
前言:从2019年年中入坑.net core已半年有余,总体上来说虽然感觉坑多,但是用起来还是比较香的.本来我是不怎么喜欢写这类实践分享或填坑记录的博客的,因为初步实践坑多,文章肯定也会有各种错误,跟 ...
- 【C#代码实战】群蚁算法理论与实践全攻略——旅行商等路径优化问题的新方法
若干年前读研的时候,学院有一个教授,专门做群蚁算法的,很厉害,偶尔了解了一点点.感觉也是生物智能的一个体现,和遗传算法.神经网络有异曲同工之妙.只不过当时没有实际需求学习,所以没去研究.最近有一个这样 ...
- ASP.NET Web API实践系列04,通过Route等特性设置路由
ASP.NET Web API路由,简单来说,就是把客户端请求映射到对应的Action上的过程.在"ASP.NET Web API实践系列03,路由模版, 路由惯例, 路由设置"一 ...
- 老李推荐:第3章3节《MonkeyRunner源码剖析》脚本编写示例: MonkeyImage API使用示例 1
老李推荐:第3章3节<MonkeyRunner源码剖析>脚本编写示例: MonkeyImage API使用示例 在上一节的第一个“增加日记”的示例中,我们并没有看到日记是否真的增加成功 ...
- 在CentOS6上配置MHA过程全记录
在CentOS6上配置MHA过程全记录 MHA(Master High Availability)是一款开源的MariaDB or MySQL高可用程序,为MariaDB or MySQL主从复制架构 ...
- 在CentOS7上通过RPM安装实现LAMP+phpMyAdmin过程全记录
在CentOS7上通过RPM安装实现LAMP+phpMyAdmin过程全记录 时间:2017年9月20日 一.软件环境: IP:192.168.1.71 Hostname:centos73-2.sur ...
- Dynamics CRM2016 Web API之创建记录
前篇介绍了通过primary key来查询记录,那query的知识点里面还有很多需要学习的,这个有待后面挖掘,本篇来简单介绍下用web api的创建记录. 直接上代码,这里的entity的属性我列了几 ...
随机推荐
- 于SharePoint经营SharePoint Designer建立
于SharePoint经营SharePoint Designer建立 SharePoint Designer 2010(SPD)它是一种强大的工具,帮助建立一个高速解决方案. 通过连接到现场,能够自由 ...
- 跨域 Iframe 通信解决方案(兼容 IE 系列浏览器。)
实现思路: 1.postMessage(IE8+, Firefox 3.1+, Opera 9+, Safari, and Chrome) 2.利用window.navigator共享信息,使支持IE ...
- 前端project师的修真秘籍(css、javascript和其他)
以我的经验,大部分技术,熟读下列四类书籍就可以. 入门,用浅显的语言和方式讲述正确的道理和方法,如head first系列 全面,巨细无遗地探讨每一个细节,遇到疑难问题时往往能够在这里得到理论解答,如 ...
- 使用live delegate on解决js后装html故障问题
今天写一个前端的东西.每学到更多的知识.几下就能写几行代码.代码行数十个.代码几个文件量--这是真的.一直以来研究的前端遇到的问题仍然在实践百度谷歌问答. 我今天遇到这样的问题:已经写js代码,正确a ...
- quick-cocos2d-x教程8:程序框架内lib文件夹分析
lib文件夹是如今全部文件夹中最复杂的,包括了整个运行所需的库文件.我们简单看下,在开发初期,不须要深入到这个库里去研究 cocos2d-x 文件夹就是c++版的原始文件 framework_prec ...
- 4.4、Libgdx用法查询执行环境相关性
(原版的:http://www.libgdx.cn/topic/46/4-4-libgdx%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95%E6%9F%A5%E8%AF%A2% ...
- Android4.4 蓝牙源代码段分析
最近GOOGLE发布时间Android4.4,我看了看源代码.4.4蓝牙打开过程或这部分的一些变化,判断蓝牙开关是从接口设置settings在里面switch开关,widget当然,它可以切换,也许启 ...
- Instll meld in windows
在linux下用meld感觉颇为不错,但是在windows上真是折腾老久.p4merge也试了试, 但还是meld用起来更顺手. 使用 https://wiki.gnome.org/Meld/Wind ...
- Notepad++ 删除空行
先选中要删部分文本内容,假设是整个文件那就全选Ctrl+A,然后使用Notepad++自带的Textfx插件,在长长的列表中找到Delete Blank Lines,点击就可以. 例如以下图:
- Maven+struts2+spring4+hibernate4的环境搭建
搭建Maven+struts2+spring4+hibernate4其实并不难!但开始弄的时候还是费了我好大的力气,老是出现这样那样的错误!好了,废话不多说,开始搭建开发环境. 一.Myeclipse ...
