zoukankan      html  css  js  c++  java
  • Android 自己主动化測试(3)<monkeyrunner> 依据ID查找对象&touch&type (python)

        我在之前的两篇文章中用java来实现过 Android 自己主动化測试(1)怎样安装和卸载一个应用(java)Android 自己主动化測试(2)依据ID查找对象(java)。 可是本质上都是用monkeyrunner相应的java lib 来实现的。可是相关的文档很少。假设真的要用monkeyrunner来做功能性的自己主动化測试。强烈还是推荐使用python语言



    1、monkey runner

    The monkeyrunner tool provides an API for writing programs that control an Android device or emulator from outside of Android code. 
    With monkeyrunner, you can write a Python program that installs an Android application or test package, runs it, 
    sends keystrokes to it, takes screenshots of its user interface, and stores screenshots on the workstation. 
    The monkeyrunner tool is primarily designed to test applications and devices at the functional/framework level and for running unit test suites, 
    but you are free to use it for other purposes.
    monkeyrunner 工具提供了一个从Android源代码外部敲代码控制一个Android设备或者模拟器的API。
    你能够用monkeyrunner写一个Python程序,来装一个Android应用和測试包。执行它,发key事件,截屏,存在本地。
    monkeyrunner工具的设计起源于在功能/框架层面来測试应用和设备。和跑单元測试的測试套件


    2、例子实现

    2.1代码实现功能: 进入robot dream的页面mainActivity,找到id为center_image 的button发生点击事件,然后对弹出的对话框中再次找到id为button1的button,进行二次确认。

    from com.android.monkeyrunner.easy import EasyMonkeyDevice, By
    from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
    
    if __name__ == '__main__':
    
        import codecs
        codecs.register(lambda name: codecs.lookup('utf-8') if name == 'cp65001' else None)
    
        print ('test start')
        device = MonkeyRunner.waitForConnection()
    
        easyMonkey = EasyMonkeyDevice(device)
        print ('start robot dream mainActivity')
    
        device.shell('am start com.robot.dream/com.robot.dream.mainActivity')
    
        MonkeyRunner.sleep(3)
        #easyMonkey.touch(MonkeyDevice.DOWN_AND_UP, By.id("id/center_image"));
    
        by = By.id("id/center_image");
        print (by)
        easyMonkey.touch(by,MonkeyDevice.DOWN_AND_UP)
    
        MonkeyRunner.sleep(3)
    
        hierachy_view = device.getHierarchyViewer()
        print(hierachy_view)
    
        view_node = hierachy_view.findViewById('id/center_image')
        items_node = view_node.children
        print (len(view_node.children))
    
        print ('touch 2')
        easyMonkey.touch(by,MonkeyDevice.DOWN_AND_UP)
    
        MonkeyRunner.sleep(3)
    
        by2 = By.id('id/button1')
        print (by2)
        print (easyMonkey.visible(by2))
    
        vn2 = hierachy_view.findViewById('id/button1')
        print (dir(vn2))
        print (getattr(vn2, 'name')) 
        print (vn2.id)
    
        easyMonkey.touch(by2, MonkeyDevice.DOWN_AND_UP)
    
        MonkeyRunner.sleep(3)
    


    2.2 代码实现功能: 进入robot dream的登陆页面LoginActivity,依据id为login_account找到登陆编辑框TextEditor输入usernameabc, 依据id为login_pwd找到password编辑框TextEditor输入passwordabc123, 最后找到id为login_button的登陆按钮。成功登陆

    from com.android.monkeyrunner.easy import EasyMonkeyDevice, By
    from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
    
    if __name__ == '__main__':
        import codecs
        codecs.register(lambda name: codecs.lookup('utf-8') if name == 'cp65001' else None)
    
        print ('test start')
        device = MonkeyRunner.waitForConnection()
        easyMonkey = EasyMonkeyDevice(device)
    
        print ('start login')
        device.shell('am start com.robot.dream/com.robot.dream.LoginActivity')
    
        MonkeyRunner.sleep(3)
    
        by = By.id("id/login_account")
        print (easyMonkey.visible(by))
        easyMonkey.type(by,'abc')
        MonkeyRunner.sleep(1)
        device.press('KEYCODE_ENTER')
        MonkeyRunner.sleep(1)
        device.press('KEYCODE_BACK')
        MonkeyRunner.sleep(1)
    
        by2 = By.id('id/login_pwd')
        print (easyMonkey.visible(by2))
        easyMonkey.type(by2,"abc123")
        MonkeyRunner.sleep(1)
        device.press('KEYCODE_ENTER')
        MonkeyRunner.sleep(1)
        device.press('KEYCODE_BACK')
        MonkeyRunner.sleep(1)
    
        by3 = By.id('id/login_button')
        print (easyMonkey.visible(by3))
        easyMonkey.touch(by3,MonkeyDevice.DOWN_AND_UP)
        MonkeyRunner.sleep(1)
    
        device.press('KEYCODE_BACK')
    


    3、总结:

    使用monkeyrunner 使用id来查找控件。使用上EasyMonkeyDevice的Touch、Type等几个基本操作,就能够完毕非常多主要的功能性測试了。


    使用monkeyrunner 的优点,測试程序执行的环境是真实的场景下的环境。

    使用monkeyrunner的局限性,无法像单元測试那样通过Assert来进行功能校验。


    4、后面会介绍些 Android monkeyrunner &Android UI Test &Android Code Coverage Test

  • 相关阅读:
    odoo 自定义视图
    Odoo 模型之间的关系 笔记
    C#中计算两点之间连线的角度
    Jquery中1.6.x中新的方法prop()方法
    VS2010快捷键说明
    将DATAtable转换成 json格式
    在IIS中执行EXE文件时的问题
    WebDev.WebServer40.exe已停止工作
    sqllite developer过期解决方案
    c#的DateTime.Now函数详解
  • 原文地址:https://www.cnblogs.com/cxchanpin/p/6769010.html
Copyright © 2011-2022 走看看