zoukankan      html  css  js  c++  java
  • MonkeyRunner 实现自动点击截屏后与本地图库进行对比输出

    先说下本人是菜鸟,通过网上资料学习,终于调通了MonkeyRunner 实现自动点击截屏后与本地图库进行对比输出,以后做静态UI测试就不需要眼睛盯着看图了,这一切交给MonkeyRunner了。

    首先通过monkeyrunner录制脚本 test.mr

    test.mr

    TOUCH|{'x':265,'y':1232,'type':'downAndUp',}
    WAIT|{'seconds':5.0,}
    TOUCH|{'x':355,'y':189,'type':'downAndUp',}
    WAIT|{'seconds':5.0,}
    TOUCH|{'x':85,'y':322,'type':'downAndUp',}
    WAIT|{'seconds':5.0,}
    TOUCH|{'x':371,'y':893,'type':'downAndUp',}
    WAIT|{'seconds':5.0,}
    TOUCH|{'x':371,'y':893,'type':'downAndUp',}
    WAIT|{'seconds':5.0,}
    TOUCH|{'x':47,'y':778,'type':'downAndUp',}
    WAIT|{'seconds':5.0,}
    TOUCH|{'x':657,'y':88,'type':'downAndUp',}
    WAIT|{'seconds':5.0,}

    然后通过python写自动截屏对比功能 monkey_snapshot.py

    #!/usr/bin/env monkeyrunner
    # Copyright 2010, The Android Open Source Project
    #coding=utf8
    import sys  
    from com.android.monkeyrunner import MonkeyDevice
    from com.android.monkeyrunner import MonkeyImage
    from com.android.monkeyrunner import MonkeyRunner 
    
    CMD_MAP = {  
        'TOUCH': lambda dev, arg: dev.touch(**arg),  
        'DRAG': lambda dev, arg: dev.drag(**arg),  
        'PRESS': lambda dev, arg: dev.press(**arg),  
        'TYPE': lambda dev, arg: dev.type(**arg),  
        'WAIT': lambda dev, arg: MonkeyRunner.sleep(**arg)  
        }
    
    def process_file(fp, device,photo_id):
        for line in fp:
            (cmd, rest) = line.split('|')
            try:
                rest = eval(rest)  
            except:
                print 'unable to parse options'
                continue
    
            if cmd not in CMD_MAP:
                print 'unknown command: ' + cmd
                continue
    
            CMD_MAP[cmd](device, rest)
            print cmd
            pic_name='./shot'+str(photo_id)+'.png'
            result = device.takeSnapshot()
    
            #compare picture
            result2 = MonkeyRunner.loadImageFromFile("E:MDshot"+str(photo_id)+".png")
            print "E:MDshot"+str(photo_id)+".png"
    
            flag = result.sameAs(result2,0.95)
            if (flag == False):
                print 'prapre to save new png '+pic_name
                result.writeToFile(pic_name,'png')
    
    
            photo_id+=1
        print "All PlayBack Action Captured,Total:"+str(photo_id)+' PNG Saved'
        print "==============================================================="
    
    def main():
        print  "Ok,Now StarRuning MyZXX MonkeyRuner SnopShoot Shell"
        print "==============================================================="
        device = MonkeyRunner.waitForConnection()
        package = 'com.qunsheng.md'
        activity = 'com.qunsheng.md.activity.WelcomeActivity'
        runComponent = package + '/' + activity
        device.startActivity(component=runComponent)
        MonkeyRunner.sleep(30)
        #device.press('KEYCODE_MENU','DOWN_AND_UP')
        #result = device.takeSnapshot()
        #result.writeToFile('./shot5.png','png')
    
        file = sys.argv[1]
        fp = open(file, 'r')
        photo_id=0
        process_file(fp, device,photo_id)
        fp.close();
    
    if __name__ == '__main__':
        main()

    最后脚本运行,将图片对比后95%部分都不相像的截图自动保存在当前目录

    monkeyrunner monkey_snapshot.py E:test.mr
  • 相关阅读:
    lua学习笔记(一)
    使用npm安装一些包失败了的看过来(npm国内镜像介绍)
    ubuntu和win7 64双系统 安装
    virtualbox共享文件夹
    今天来谈谈三大基础排序选择排序、冒泡排序、插入排序
    vue入门七之vuex的使用
    简单介绍下js的随机数的生成
    来简单谈谈JavaScript两个数的交换问题
    VUE入门六之过滤器
    VUE入门五之路由的使用
  • 原文地址:https://www.cnblogs.com/luolizhi/p/5641847.html
Copyright © 2011-2022 走看看