zoukankan      html  css  js  c++  java
  • monkeyRunner

     MonkeyRunner工具是使用Jython(使用Java编程语言实现的Python)写出来的,它提供了多个API,通过monkeyrunner API 可以写一个Python的程序来模拟操作控制Android设备app,测试其稳定性并通过截屏可以方便地记录出现的问题。

     MonkeyRunner 和 monkey没有直接关系,monkey是在设备直接运行adb shell命令生成随机事件来进行测试的。而monkeyrunner则是通过API发送特定的命令和事件通过工作站来控制设备。

    优势:

    1.多设备控制:API可以跨多个设备,一次启动全部模拟器来实施测试套件;

    2.功能测试:为应用自动执行一次功能测试,然后观察输出结果的截屏。

    2.可扩展自动化:因为monkeyrunner是一个API工具包,你可以开发基于Python模块的整个系统来控制Android设备;

    API

    MonkeyRunner工具主要有三个类:MonkeyRunner、MonkeyDevice、MonkeyImage.

    主要方法

    MonkeyRunner

    alert()

    waitForConnection(float timeout,string deviceid)

    sleep()

    help

    MonkeyDevice

    installPackage()

    starActivity(package+'/'+activity)

    touch(integer x,integer y, integer type) 

    drag(tuple start,tuple end,float duration,integer steps)

    press(string keycode,dictionary type) 

    takesnapshot

    getProperty

    MonkeyImage

    ConvertoBytes

    getSubImage

    sameAs(MonkeyImage other,float percent) 可通过截图方法判断两次结果是否一致

    writetoFile(string path,string format)

    monkeyRunner示例代码:

    from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage
    from com.android.monkeyrunner.easy import EasyMonkeyDevice,By
    
    device = MonkeyRunner.waitForConnection()
    
    print '******Case1: Use MonkeyDevice and MonkeyImage to check claculator result******'
    
    print '---- start Calculator App'
    
    device.startActivity('com.android.calculator2/.Calculator')
    
    print '---- calculator 3*8 with press method'
    
    device.press('KEYCODE_3',MonkeyDevice.DOWN_AND_UP)
    device.press('KEYCODE_NUMPAD_MULTIPLY',MonkeyDevice.DOWN_AND_UP)
    device.press('KEYCODE_8',MonkeyDevice.DOWN_AND_UP)
    device.press('KEYCODE_EQUALS',MonkeyDevice.DOWN_AND_UP)
    
    image = device.takeSnapshot()
    subimage = image.getSubImage((300,50,356,234))
    
    print '---- calculator 4*6 with touch method'
    device.touch(100,600,MonkeyDevice.DOWN_AND_UP)
    device.touch(600,600,MonkeyDevice.DOWN_AND_UP)
    device.touch(400,600,MonkeyDevice.DOWN_AND_UP)
    device.touch(400,1000,MonkeyDevice.DOWN_AND_UP)
    
    image2 = device.takeSnapshot()
    subimage2 = image.getSubImage((300,50,356,234))
    
    if (subimage2.sameAs(subimage,0.8)):
        print '[PASS] the result of 3*8 and 4*6 is equal!'
    else:
        print '[Fail] the result of 3*8 and 4*6 is not equal!'
        
    print '******Case2: Use EasyMonkeyDevice to check claculator result******'    
    
    print '---- calculator 5*7 with EasyMonkeyDevice touch'
    
    easy = EasyMonkeyDevice(device)
    easy.touch(By.id('id/digit5'),MonkeyDevice.DOWN_AND_UP)
    easy.touch(By.id('id/mul'),MonkeyDevice.DOWN_AND_UP)
    easy.touch(By.id('id/digit7'),MonkeyDevice.DOWN_AND_UP)
    easy.touch(By.id('id/equal'),MonkeyDevice.DOWN_AND_UP)
    
    hv=device.getHierarchyViewer()
    view = hv.findViewById('id/display')
    str =view.children[1].namedProperties.get('text:mText').toString().encode('utf8')
    
    
    if (str == '35'):
        print '[PASS] the result of 5*7 is correct!'
    else:
        print '[Fail] the result of 5*7 is correct! the result is -- ' +str
        
    easy.touch(By.id('id/clear'),MonkeyDevice.DOWN_AND_UP)
    device.press('KEYCODE_BACK',MonkeyDevice.DOWN_AND_UP)
    View Code
  • 相关阅读:
    20000+关注,开源两本硬核的原创电子书!
    Tail Latency学习
    Zabbix5.0 监控redis
    JAVA多线程(九) ForkJoin框架
    JAVA多线程(八) Condition源码分析
    程序员英语学习(二) 标点符号对应的英语单词汇总
    linux shell快速入门
    Ubuntu常用指令和快捷键汇总
    Win10常用快捷键汇总
    算法路漫漫(三) 荷兰国旗
  • 原文地址:https://www.cnblogs.com/stin/p/8359984.html
Copyright © 2011-2022 走看看