安卓自动化测试工具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地址:
安卓自动化测试工具MonkeyRunner之使用ID进行参数化,以及List选择某项和弹出框点击确定的写法的更多相关文章
- Bootstrap -- 插件: 提示工具、弹出框、 警告框消息
Bootstrap -- 插件: 提示工具.弹出框. 警告框消息 1. 提示工具(Tooltip)插件:根据需求生成内容和标记. 使用提示工具: <!DOCTYPE html> <h ...
- 自动化测试基础篇--Selenium弹出框alert
摘自https://www.cnblogs.com/sanzangTst/p/7685304.html 不是所有的弹出框都叫alert,在使用alert方法前,先要识别出到底是不是alert.先认 ...
- 【如何使用jQuery】【jQuery弹出框】【jQuery对div进行操作】【jQuery对class,id,type的操作】【jquery选择器】
1.如何使用jQuery jQuery是一个快速.简洁的JavaScript框架,是继Prototype之后又一个优秀的JavaScript代码库(或JavaScript框架).jQuery设计的宗旨 ...
- 自动化测试-12.selenium的弹出框处理
前言 不是所有的弹出框都叫alert,在使用alert方法前,先要识别出到底是不是alert.先认清楚alert长什么样子,下次碰到了,就可以用对应方法解决. alert\confirm\prompt ...
- 安卓自动化测试工具一:Monkey
一:monkey的用途:主要用于稳定性测试,模拟用户操作 二.monkey的基本使用 monkey文档地址:"<android_sdk>/docs/tools/help/monk ...
- 安卓自动化测试工具Monkey简单使用
一.首先安装adb 地址:http://www.downza.cn/soft/219906.html安装到D盘下,安装的过程中自己注意下不要安装上全家桶.找到这个压缩包:解压到当前文件夹: 二.将ad ...
- 安卓开发之自定义一个view弹出框
https://www.cnblogs.com/muyuge/p/6152167.html
- [安卓自动化测试] 001.UIAutomator初探
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- 自动化测试工具QTP和SilkTest横向PK(转)
转自:http://www.uml.org.cn/Test/201405212.asp?artid=1686 众所周知,自动化测试工具曾几何时三足鼎立,Mercury QTP/WinRunner系.I ...
随机推荐
- 在一个Activity里面的TextView上面添加网页链接,启动后到另一个Activity里面!
可以添加很多的属性,样式或者是什么的,目前要完成的功能是 点击TextView里面的某个文字链接,进入另外一个Activity里面!例如你可以做微博里面的 @XXX: 点击后进入他的个人主页! 下面都 ...
- ffmpeg 错误码
av_read_frame, av_write_frame等 经常会返回负值也即写数据包失败.不同的负值代表不同的含义,可以根据错误码定义,定位问题. #define EPERM 1 /* Opera ...
- 使用XAMPP本地安装Wordpress博客
最近一直在研究博客,也知道了大名鼎鼎的wordpress,因此也希望动手尝试一下,看看跟网站提供的博客有何区别. 第一个问题:能什么安装wordPress,能否用tocmat? 虽然问题很可笑,但是之 ...
- 本地不安装oracle-client,使用pl/sql developer连接数据库
一.问题描述 本地未安装oracle-client端,由于机器资源有限,希望通过pl/sql developer进行远程数据库连接.单纯的安装pl/sql developer无法远程连接数据库. 二. ...
- 项目中libevent几个问题
几个问题: .libevent到底用的是select还是iocp,然后是如何突破64限制的 typedef struct fd_set { u_int fd_count; /* how many ar ...
- .net 对称加密DESCryptoServiceProvider
1.生成密钥以加密和解密数据 DESCryptoServiceProvider 基于一种对称加密算法.对称加密需要密钥和初始化矢量 (IV) 来加密数据.要解密该数据,您必须拥有此同一密钥和 IV.您 ...
- 2013 Multi-University Training Contest 1 I-number
水题,注意不要去掉前导0…… ;}
- C Primer Plus 第4章 字符串和格式化输入/输出 编程练习
1. #include <stdio.h> int main(void) { ]; ]; printf("请输入您的名字: "); scanf("%s&quo ...
- Bash的脚本参数
$0:脚本名字.此变量包含地址,可以使用basename $0获得脚本名称.$1:第一个参数$2,$3,$4,$5,…一次类推. $# 传递到脚本的参数个数$* 以一个单字符串显示所有向脚本传递的参数 ...
- .NET复习笔记
.NET 基础知识点汇总 课前知识储备. 一.C#与.NET的区别? 1..NET/dotnet:一般指.Net Framework框架,一种平台,一种技术 2.C#(sharp):一种编程语言,可以 ...