1. 背景
鉴于网上使用MonkeyImage的实例除了方法sameAs外非常难找到,所以本人把实践各个API的过程记录下来然自己有更感性的认识,也为往后的工作打下更好的基础。同一时候也和上一篇文章《》起到相互呼应的作用。
由于并没有MonkeyRunner的项目背景,所以这里很多其它的是描写叙述各个API是怎么一回事。而不是描写叙述在什么场景下须要用到。也就是说是去回答What,而不是How。
首先我们先看下官方给出的MonkeyImage的API描写叙述,对照我如今反编译的最新的源代码是一致的:
Return Type | Methods | Comment |
string | (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 | (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 | (integer x, integer y) Returns the single pixel at the image location (x,y), as a 32-bit integer. |
|
| (tuple rect) Creates a new |
|
boolean | ( Compares this |
|
void | (string path, string format) Writes the current image to the file specified by |
|
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.xy = point.yimg = device.takeSnapshot()pixelTuple = img.getRawPixel(x,y)pixelInt = img.getRawPixelInt(x,y)print "Pixel in tuple:",pixelTupleprint "Pixel in int:", pixelInt输出:
3.2 分析
4. MonkeyImage getSubImage(tuple rect)
4.1 演示样例
from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImagefrom com.android.monkeyrunner.easy import EasyMonkeyDevice,Byfrom com.android.chimpchat.hierarchyviewer import HierarchyViewerfrom com.android.hierarchyviewerlib.models import ViewNode, Windowfrom java.awt import Point#from com.android.hierarchyviewerlib.device import #Connect to the target targetDevicetargetDevice = MonkeyRunner.waitForConnection()easy_device = EasyMonkeyDevice(targetDevice) #touch a button by id would need thistargetDevice.startActivity(component="com.example.android.notepad/com.example.android.notepad.NotesList")#invoke the menu optionsMonkeyRunner.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 Optionsbutton弹出“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 分析
也就是说演示样例中的图片终于是保存在我的monkeyrunner可运行程序的上一层文件夹。
作者 | 自主博客 | 微信 | CSDN |
天地会珠海分舵 |
| 服务号:TechGoGoGo 扫描码:
|
|