monkeyrunner 自动化测试 图片对比的实现

这个功能在网上看了好多人的代码,但是总是在image.writeToFile('D:/tmp/images/black.png','png')这一句出错.查了google的API也感觉没错呀。
后来自己动脑,改成了image.writeToFile('D:/tmp/images/black.png'),竟然就成功了。后面的图片后缀竟然是可以不用加的。而且
然后loadimage后面的后缀同样可选。我是这样写的 result = MonkeyRunner.loadImageFromFile('D:/tmp/images/black.png','png')

code:

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
import sys,time

device = MonkeyRunner.waitForConnection()
device.wake()

device.startActivity(action = "android.media.action.IMAGE_CAPTURE")
MonkeyRunner.sleep(3.0)
image = device.takeSnapshot()
MonkeyRunner.sleep(1.0)

if (image != None):
        image.writeToFile('D:/tmp/images/black.png')

cout = 1
while(cout < 1000):
        device.startActivity(action = "android.media.action.IMAGE_CAPTURE")
        MonkeyRunner.sleep(3.0)
        image = device.takeSnapshot()
        MonkeyRunner.sleep(1.0)
        result = MonkeyRunner.loadImageFromFile('D:/tmp/images/black.png','png')  
        MonkeyRunner.sleep(3.0)
        if(image.sameAs(result,0.8)):  
                print "success!"
        else:  
                print "fail!"
                break;
        cout += 1

这个脚本是用来测试系统调用摄像头是否成功的,但是由于摄像头取到的图片是一张照片,sameas的算法可能针对于手机屏幕的显示而对摄像头取到的图算不来,所以实际测试的时候失败了。

posted @ 2015-09-25 14:56  皮斯卡略夫  阅读(2213)  评论(0编辑  收藏  举报