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 |
||
boolean |
sameAs ( Compares this |
|
void |
writeToFile (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.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 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可运行程序的上一层文件夹。