zoukankan      html  css  js  c++  java
  • 安卓自动化测试工具MonkeyRunner之使用ID进行参数化,以及List选择某项和弹出框点击确定的写法

    一、List选择某项的操作步骤:

     1、通过父结点得出列表各子项

     2、将选择项的文本与列表中的子项进行比较

     3、计算出选择项的坐标位置

    截取实例:

    from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice
    from com.android.monkeyrunner.easy import EasyMonkeyDevice
    from com.android.monkeyrunner.easy import By
    from com.android.chimpchat.hierarchyviewer import HierarchyViewer
    from com.android.hierarchyviewerlib.device import ViewNode

    choice = "mText=11:01-14:00"  #需要选择的项
    device = MonkeyRunner.waitForConnection()
    package = 'com.umetrip.android.msky'
    activity = '.activity.ticketbooking.TicketSearchActivity'
    runComponent = package + '/' + activity
    device.startActivity(component=runComponent)
    MonkeyRunner.sleep(3.0)
    easy_device = EasyMonkeyDevice(device)
    hierarchy_viewer = device.getHierarchyViewer()
    task_node = hierarchy_viewer.findViewById('id/content')
    task_high = task_node.paddingTop   #得到手机任务栏的高度
    easy_device.touch(By.id('id/ticket_time1'),MonkeyDevice.DOWN_AND_UP) #点击ticket_time1后,弹出列表选择框
    MonkeyRunner.sleep(1.0)
    root_node = hierarchy_viewer.findViewById('id/content')
    list_node = hierarchy_viewer.findViewById('id/select_dialog_listview') #得到list结点信息
    #length  = len(list_node.children)
    items_node = list_node.children #返回此list有几个选择项
    screen_high = device.getProperty('display.height') #得到屏幕Y轴高度
    current_high= root_node.height #得到当前list的弹出框的高度
    for item in items_node:
        mtext = (item.namedProperties.get('mText').toString()).encode('utf8') #得到list子项的文本内容:注意的是由于jython暂不支持中文,此处子项文本内容若是中文便无法运行成功
            if choice == mtext:          
             id = (item.id).encode('utf8')
            #print 'xxx',id,item.width,item.height
            item_point = HierarchyViewer.getAbsolutePositionOfView(item) #得到需要选择的子项的X,Y轴坐标:注意的是此处坐票是相对于弹出框来说的,而不是针对于整个手机屏幕的坐标
            print 'Item point absolute position is: ',item_point.x,item_point.y,current_high,screen_high.encode('utf8')
            top_space = int(((int(screen_high)-task_high) - current_high)/2)  #计算出弹出框顶部与应用程序界面顶部之间的间隔距离
            print 'top space is: ',top_space
            device.touch(item_point.x,item_point.y+top_space+task_high,'DOWN_AND_UP')   #需要选择项的高度相对于手机屏幕的Y轴坐标为:任务栏高度+间隔距离+相对于list弹出框的高度
            print 'clicked it'
    print 'OVER'

    如下图:

    二、弹出框的确定按钮与list的计算方法类似,上述的list中选择某项只计算了Y轴坐标,而事实上还应该计算X轴的坐标,最后算出弹出框中确定按钮最中心的坐标位置。

    截取实例:

    def touchOkPanel():
        hierarchy_viewer = device.getHierarchyViewer()
        task_node = hierarchy_viewer.findViewById('id/content') # get the parent view node
        task_high = task_node.paddingTop  #得到任务栏高度
        root_node = hierarchy_viewer.findViewById('id/content')
        ok_node = hierarchy_viewer.findViewById('id/button1')
        ok_point = HierarchyViewer.getAbsolutePositionOfView(ok_node) #得到确定键相对于弹出框的X,Y轴坐标
        screen_high = device.getProperty('display.height') #得到屏幕Y轴高度
        screen_width = device.getProperty('display.width') #得到屏幕X轴高度
        current_high = root_node.height  #得到当前弹出框的Y轴高度   注意:屏幕上所见到的弹出框实际Y轴高度大于当前显示的高度,可以通过上一节说的HierarchyViewer.bat查看
        current_width = root_node.width #得到当前弹出框的X轴高度
        ok_center_high = int(ok_node.height/2)    #得到确定键的中心X,Y轴高度
        ok_center_width = int(ok_node.width/2)    
        print 'OK button point absolute position is: ',ok_point.x,ok_point.y,ok_center_high,current_high,screen_high.encode('utf8'),
            current_width,screen_width.encode('utf-8')
        top_space = int(((int(screen_high)-task_high) - current_high)/2) #计算出任务栏与弹出框之间的Y间隔距离
        left_space = int((int(screen_width)-current_width)/2)  #计算出应用程序左侧与弹出框之间的X间隔距离
        print 'top space is: ',top_space,left_space
        device.touch(ok_point.x+left_space+ok_center_width,ok_point.y+ok_center_high+top_space+task_high,'DOWN_AND_UP')  #X轴=间隔距离+X相对坐标+确定键中心X,Y轴=间隔距离+Y相对坐标+确定键中心Y
        print 'clicked OK button'

    如下图:

    三、参数化

    1、使用xml文件存储输入参数

    2、使用python文件解析xml文件获得各组参数

    3、调用各组参数运行

    截取登录实例:

    login.xml:

    <xml>
     <login>
       <name></name>
       <pass>red123</pass>
     </login>
     <login>
       <name>yhl</name>
       <pass>2345</pass>
     </login>
     <login>
       <name>redlin</name>
       <pass>red123</pass>
     </login>
    </xml>

    XMLParse.py:

    import sys
    from xml.etree.ElementTree import ElementTree

    def getparameter():
        parameters = []
        tree = ElementTree()
        print 'start parse xml'
        tree.parse('D:\monkeyrunner\PPTscript\login.xml') #get ElementTree object
        root = tree.getroot()
        for r in root.getiterator('login'):
            result = {}
            name = r.find('name').text
            password = r.find('pass').text
            print 'parse name--',name,'parse password---',password
            result['name'] = name
            result['password'] = password
            parameters.append(result)
        return parameters    
    if __name__ == '__main__':
        getparameter()

    testFromXML.py:

    from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice
    from com.android.monkeyrunner.easy import EasyMonkeyDevice
    from com.android.monkeyrunner.easy import By
    from com.android.chimpchat.hierarchyviewer import HierarchyViewer
    from com.android.hierarchyviewerlib.device import ViewNode
    import sys
    sys.path.append('D:\monkeyrunner\PPTscript')
    import XMLParse
    device = MonkeyRunner.waitForConnection()

    def doTest(name,pwd,test):

    #####省略

    if __name__ == "__main__":
         tests = []
         i = 1
         tests = XMLParse.getparameter() #get the test parameters
         if len(tests) > 0:
             print len(tests),tests
             for test in tests:
                 name = test['name']
                 password = test['password']
                 print 'name:',name,' password',password
                 doTest(name,password,i)
                 print 'the login test ',i,'is finished'
                 i = i+1
         print 'tests finished'

    一些常用的url地址:

    http://developer.android.com/reference/android/view/KeyEvent.html

    http://source-android.frandroid.com/sdk/monkeyrunner/src/com/android/monkeyrunner/

    http://source-android.frandroid.com/sdk/chimpchat/src/com/android/chimpchat/hierarchyviewer/HierarchyViewer.java

    http://www.java2s.com/Open-Source/Android/android-core/platform-sdk/com/android/hierarchyviewerlib/device/ViewNode.java.htm

     

  • 相关阅读:
    axios 配置
    vue 配置App.js
    vue 挂载方式
    常用的js
    vuex
    vue搭建环境
    JS中 toString() & valueOf()
    html-webpack-plugin 中使用 title选项设置模版中的值无效
    webpack为什么加载不了css?
    visual studio for mac 安装文件
  • 原文地址:https://www.cnblogs.com/rosepotato/p/3528380.html
Copyright © 2011-2022 走看看