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

安卓自动化测试工具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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值