Airtest自动化测试神器,教你实现Unity自动化测试
发布日期:2021-06-30 19:35:31
浏览次数:2
分类:技术文章
本文共 5274 字,大约阅读时间需要 17 分钟。
文章目录
Airtest Project
是网易游戏内部工具团队开发并开源的一款UI自动化测试工具。
一、Airtest官网
二、Poco-SDK下载
三、中文文档
四、Airtest 常用函数
1 获取屏幕尺寸
from poco.drivers.android.uiautomation import AndroidUiautomationPocoandroid_poco= AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)screen_with, screen_height = android_poco.get_screen_size()
2 引用其他air脚本
from airtest.core.api import usingusing("other_test.air") # 需要使用相对路径,不然找不到.airimport other_test
3 元素定位
根据对象名字定位
login_btn = poco("login_btn")# 按钮点击login_btn.click()
根据文本内容来定位
login_btn = poco(text="账号登录")login_btn.click()
用正则表达式来模糊定位
login_btn = poco(textMatches="^.*账号登录.*$")login_btn.click()
子元素
list_item = poco('list_root').child('list_item')
父节点
poco('node_item').parent()
兄弟节点
poco('user_level').sibling('user_name')
4 点击元素身上的某一点:focus、click
通过相对坐标,控制点击的具体位置。左上角(0, 0)
,右下角(1, 1)
node = poco(text='领取奖励')# 点击节点的中心点位置, 默认点击中心位置node.focus('center').click()# 点击节点的靠近左上角位置node.focus([0.1, 0.1]).click()# 点击节点的右下角位置node.focus([0.9, 0.9]).click()
5 等待元素出现或消失:wait_for_appearance、wait_for_disappearance
当使用wait_for_appearance
或wait_for_disappearance
时,建议处理PocoTargetTimeout
,并截图,以方便在报告中查看出错时的页面情况
try: # 元素出现 poco(text='领取奖励').wait_for_appearance(timeout=10) # 元素消失 # poco(text='领取奖励').wait_for_disappearance(timeout=10)except PocoTargetTimeout: snapshot(msg="超时: [领取奖励]元素出现")
6 拖动:drag_to
# 将star(星星)拖动到shell(贝壳)那里poco('star').drag_to(poco('shell'))
7 滑动:swipe
# 滑动指定坐标poco('Scroll View').swipe([0, -0.1]) # 向上滑动poco('Scroll View').swipe('up') # 向下滑动poco('Scroll View').swipe('down')
封装,水平滑动和垂直滑动,把不在屏幕内部的控件滑动到屏幕内,使之可被操作
#先计算屏幕宽和高from poco.drivers.android.uiautomation import AndroidUiautomationPocoandroid_poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)screen_width, screen_height = android_poco.get_screen_size()'''水平滑动,把不在屏幕内部的控件滑动到屏幕内,使之可被操作:param element: 控件元素:param area_left: 希望控件所在屏幕上的区域的左边缘:0~1:param area_right: 希望控件所在屏幕上的区域的右边缘:0~1:param swipe_pos_y: 滑动时点击的y坐标'''def horizontal_swipe_element_to_area(element, area_left=0.3, area_right=0.7, swipe_pos_y=320): swipe_pos_x = (area_left+area_right)*0.5*screen_width for i in range(50): cur_pos = element.get_position() cur_pos_x = cur_pos[0] if cur_pos_x < area_left or cur_pos_x > area_right: #如果在区域左边,则往右滑,如果在区域右边,则往左滑 swipe([swipe_pos_x ,swipe_pos_y], [2*swipe_pos_x -cur_pos_x*screen_width, swipe_pos_y]) else: print("swipe finish") break'''垂直滑动,把不在屏幕内部的控件滑动到屏幕内,使之可被操作:param element: 控件元素:param area_top: 希望控件所在屏幕上的区域的上边缘:0~1:param area_bottom: 希望控件所在屏幕上的区域的下边缘:0~1:param swipe_pos_x: 滑动时点击的x坐标'''def vertical_swipe_element_to_area(element, swipe_pos_x=600, area_top=0.3, area_bottom=0.7): swipe_pos_y = (area_bottom+area_top)*0.5*screen_height for i in range(50): cur_pos = element.get_position() cur_pos_y = cur_pos[1] if cur_pos_y < area_top or cur_pos_y > area_bottom: #如果在区域上边,则往下滑,如果在区域下边,则往上滑 swipe([swipe_pos_x,swipe_pos_y],[swipe_pos_x, 2*swipe_pos_y -cur_pos_y*screen_height]) else: print("swipe finish") break
8 连续滑动:swipe_along
# 获取当前手机设备dev = device() # 手指按照顺序依次滑过3个坐标dev.minitouch.swipe_along([(100, 100), (200, 200), (300, 300)])
9 按住n秒拖动: hold、to
# 点击ui1保持1秒,拖动到ui2并保持1秒,然后抬起ui1.start_gesture().hold(1).to(ui2).hold(1).up()
10 长按:long_click
# 长按,单位:秒poco(text="长按").long_click(duration=2.0)
11 判断元素是否存在:exists
login_btn = poco(text="账号登录")if login_btn.exists(): login_btn.click()
12 UI状态清除:invalidate
在poco
里选择出来的UI
都是代理对象,在执行同一个用例里,一个UI
控件选出来后能持续多长时间有效这个是要看Android
那回收UI
资源的策略的,每个厂商的差异比较大。
cocos2d-x
引擎的poco
,由于使用的是快照模式,获取到UI
状态后如果UI
状态确实发生了改变,需要调用invalidate()
进行重新获取。 btn = poco(text="领取奖励")btn.click()# 过了一段时间之后,再执行的时候,先调用invalidate进行重新获取代理对象btn.invalidate()btn.click()
13 两指挤压收缩操作:pinch
# 在给定的范围和持续时间下,在UI上两指挤压收缩操作poco.pinch(direction='in', percent=0.6, duration=2.0, dead_zone=0.1)
14 无效的操作异常:InvalidOperationException
from poco.exceptions import *try: # 点击屏幕外部,此时一定为报错,捕获到InvalidOperationException poco.click([1.1, 1.1])except InvalidOperationException: snapshot(msg="无效的操作")
15 节点或属性不存在异常:PocoNoSuchNodeException
from poco.exceptions import *node = poco("not existed node")try: node.click()except PocoNoSuchNodeException: snapshot(msg="节点不存在")try: node.attr('text')except PocoNoSuchNodeException: snapshot(msg="属性不存在")
16 等待超时异常:PocoTargetTimeout
from poco.exceptions import *try: poco("login_btn").wait_for_appearance(timeout=10)except PocoTargetTimeout: snapshot(msg="login_btn 等待超时")
17 对象已被清除:PocoTargetRemovedException
from poco.exceptions import *try: poco('login_btn').click()except PocoTargetRemovedException: snapshot(msg='login_btn 对象已被清除')
五、自定义poco属性
有时候,我们可能需要自定义一些属性,方便在poco中操作,比如自定义一个selfdefineAttr
属性,
PocoSDK
中的UnityNode.cs
添加想要的定义即可 public override object getAttr(string attrName){ switch(attrName) { case "selfdefineAttr": return "Hello selfdefineAttr"; }}
另外,如果想在Poco
渲染树中显示该属性,则在GetPayload()
函数中添加字段即可
private DictionaryGetPayload(){ Dictionary payload = new Dictionary () { { "selfdefineAttr", "Hello selfdefineAttr"} };}
最后,我们就可以在Airtest
中通过poco
的attr
方法获取到我们自定义的属性了
selfdefineAttr = poco('testNode').attr('selfdefineAttr')print(selfdefineAttr)
转载地址:https://linxinfa.blog.csdn.net/article/details/107840992 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年05月01日 01时18分57秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
一个数据项目
2019-04-30
基于JAVA_JSP电子书下载系统
2019-04-30
基于java出租车计价器设计与实现
2019-04-30
基于java的B2C的网上拍卖系统
2019-04-30
十二时辰篇:这该死的 996
2019-04-30
2021最新 上海互联网公司排名
2019-04-30
字节vs快手!取消大小周之战
2019-04-30
送一个闲置显示器!
2019-04-30
Oracle 行转列 pivot函数基本用法
2019-04-30
Oracle字符串分隔符替换(替换奇数个或偶数个)
2019-04-30
Oracle 利用 UTL_SMTP 包发送邮件
2019-04-30
Oracle 自定义函数实现split功能,支持超长字符串和clob类型的分隔
2019-04-30
Oracle 的循环中的异常捕捉和处理
2019-04-30
Oracle通过pivot和unpivot配合实现行列转换
2019-04-30
给Oracle数据库换一个1522端口的监听
2019-04-30
Excel表格数据生成ECharts图表
2019-04-30
阿里云短信服务python版,pyinstaller打包运行时缺少文件
2019-04-30
Oracle的pfile和spfile的一点理解和笔记
2019-04-30
WebService的简单案例记录(Java)
2019-04-30