zoukankan      html  css  js  c++  java
  • MonkeyRunner实战-自动化操作计算器计算

    前面几张详细的讲了MonkeyRunner的模块,包括MonkeyRunner、MonkeyDevice、MonkeyImage,也包括easyMonkeyDevice,还有getHierarchyViewer这样的一些方法。

    这一章具体的使用python来编写自动化测试的脚本,利用MonkeyRunner完成自动化测试。

     Case1

    用press、touch方法分别触发计算器按键,并用MonkeyImage比较两次计算结果是否一致

    python脚本的编写如下:

    #第一次运算press

    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 calculator 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)

    easy = EasyMonkeyDevice(device)

    image = device.takeSnapshot() #把当前的界面保存到MonkeyImage对象里

    subimage = image.getSubImage(easy.locate(By.id('id/display')))

    #打开uiautomatorviewer查看保存结果文本框的坐标范围[0,75][912,426],由于结果只在区域有半部分,所以坐标可以变为[300,75][912,426]

    subimage = image.getSubImage((300,75,612,351)) #这样子就保存了第一次运算的结果

    #第二次运算touch

    print '---- calculator 4*6 with touch method'

    easy.touch(By.id('id/digit4'),MonkeyDevice.DOWN_AND_UP)

    easy.touch(By.id('id/mul'),MonkeyDevice.DOWN_AND_UP)

    easy.touch(By.id('id/digit6'),MonkeyDevice.DOWN_AND_UP)

    easy.touch(By.id('id/equal'),MonkeyDevice.DOWN_AND_UP)

    image2 = device.takeSnapshot()

    subimage2 = image2.getSubImage(easy.locate(By.id('id/display')))

    if (subimage2.sameAs(subimage,0.8)):

        print '[Pass] the resault of 3*8 and 4*6 is equal !'

    else:

        print '[Fail] the resault of 3*8 and 4*6 is not equal !'

    把以上的脚本编写好命名为calculator_mr.py保存到C:Usershou-00下面,然后把脚本拖到命令行执行

    Case2

    用EasyMonkeyDevice来获取按键并触发,再用HierarchyViewer获取对象属性校验结果正确性

     

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

    from com.android.monkeyrunner.easy import EasyMonkeyDevice,By               

    device = MonkeyRunner.waitForConnection()     

    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[0].namedProperties.get('text:mText').toString().split('=')[1].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)

  • 相关阅读:
    How to build Linux system from kernel to UI layer
    Writing USB driver for Android
    Xposed Framework for Android 8.x Oreo is released (in beta)
    Linux Smartphone Operating Systems You Can Install Today
    Librem 5 Leads New Wave of Open Source Mobile Linux Contenders
    GUADEC: porting GNOME to Android
    Librem 5 – A Security and Privacy Focused Phone
    GNOME and KDE Join Librem 5 Linux Smartphone Party
    Purism计划推出安全开源的Linux Librem 5智能手机
    国产系统之殇:你知道的这些系统都是国外的
  • 原文地址:https://www.cnblogs.com/ljf-hero/p/6632931.html
Copyright © 2011-2022 走看看