一、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

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

  1. Bootstrap -- 插件: 提示工具、弹出框、 警告框消息

    Bootstrap -- 插件: 提示工具.弹出框. 警告框消息 1. 提示工具(Tooltip)插件:根据需求生成内容和标记. 使用提示工具: <!DOCTYPE html> <h ...

  2. 自动化测试基础篇--Selenium弹出框alert

    摘自https://www.cnblogs.com/sanzangTst/p/7685304.html   不是所有的弹出框都叫alert,在使用alert方法前,先要识别出到底是不是alert.先认 ...

  3. 【如何使用jQuery】【jQuery弹出框】【jQuery对div进行操作】【jQuery对class,id,type的操作】【jquery选择器】

    1.如何使用jQuery jQuery是一个快速.简洁的JavaScript框架,是继Prototype之后又一个优秀的JavaScript代码库(或JavaScript框架).jQuery设计的宗旨 ...

  4. 自动化测试-12.selenium的弹出框处理

    前言 不是所有的弹出框都叫alert,在使用alert方法前,先要识别出到底是不是alert.先认清楚alert长什么样子,下次碰到了,就可以用对应方法解决. alert\confirm\prompt ...

  5. 安卓自动化测试工具一:Monkey

    一:monkey的用途:主要用于稳定性测试,模拟用户操作 二.monkey的基本使用 monkey文档地址:"<android_sdk>/docs/tools/help/monk ...

  6. 安卓自动化测试工具Monkey简单使用

    一.首先安装adb 地址:http://www.downza.cn/soft/219906.html安装到D盘下,安装的过程中自己注意下不要安装上全家桶.找到这个压缩包:解压到当前文件夹: 二.将ad ...

  7. 安卓开发之自定义一个view弹出框

    https://www.cnblogs.com/muyuge/p/6152167.html

  8. [安卓自动化测试] 001.UIAutomator初探

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  9. 自动化测试工具QTP和SilkTest横向PK(转)

    转自:http://www.uml.org.cn/Test/201405212.asp?artid=1686 众所周知,自动化测试工具曾几何时三足鼎立,Mercury QTP/WinRunner系.I ...

随机推荐

  1. 关于6410的sd卡和nandflash启动的区别

    今天在公司我们队长问我个问题,关于cortex的sd启动流程和nandflash的启动流程,一下想不起来了,中午闲来无事就整理了整理当初6410的两种启动方式的区别.在这里写一下.有不对的请指点,我对 ...

  2. 如何在cocos2dx lua的回调函数里面用self

    回调里的self是另一个不同的东西了,通常是触发回调的对象,或_G或nil ,视情况而定 我的 print(self) 输出 userdata function MyClass:sayFromCall ...

  3. Problem 1016 咒文卷轴 优先队列+前缀和+rmq

    题目链接: 题目 Problem 1016 咒文卷轴 Time Limit: 3000 mSec Memory Limit : 131072 KB 问题描述 小Y 是一个魔法师,有一天他获得了一卷神秘 ...

  4. 【BZOJ】【1103】【POI2007】大都市meg

    dfs序 模板题,进点+1出点-1,刚好对于不在路径上的点一进一出刚好抵消,由于本题要动态修改(变成公路以后+1-1都变成0)所以在序列上套一个树状数组即可. TLE:1.递归dfs给爆了……写了个手 ...

  5. PAT-乙级-1052. 卖个萌 (20)

    1052. 卖个萌 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 萌萌哒表情符号通常由“手”.“眼”. ...

  6. jstl删除session,choose,动态获取request当前工程路径

    1.jstl标签c:remove删除session request.getSession().setAttribute("ssmsg", "修改成功"); &l ...

  7. java基础知识回顾之javaIO类--File类应用:获取指定目录下面的指定扩展名的文件,将文件的绝对路径写入到目的文件当中

    /** * File文件综合应用 * 需求:获取指定目录下面,指定扩展名的文件,将文件的绝对路径写到文本文件当中. *  * 思路:1.需要深度遍历.--递归 * 2.遍历的过程中过滤指定扩展名的文件 ...

  8. hdu 1709 The Balance

    母函数的特殊情况,左右两边都可以放,如样例1,2,9 母函数为(1+x+1/x)*(1+x^2+1/x^2)*(1+x^9+1/x^9) 化简为(1+x+x^2)*(1+x^2+x^4)*(1+x^9 ...

  9. 如何正确学习JavaScript

    不要这样学习JavaScript 不要一开始就埋头在成堆的JavaScript在线教程里 ,这是最糟糕的学习方法.或许在看过无数个教程后会有点成效,但这样不分层次结构地学习一个东西实在是十分低效,在实 ...

  10. lintcode 中等题:partition array 数组划分

    题目 数组划分 给出一个整数数组nums和一个整数k.划分数组(即移动数组nums中的元素),使得: 所有小于k的元素移到左边 所有大于等于k的元素移到右边 返回数组划分的位置,即数组中第一个位置i, ...