Airtest自动化实战

最近领导让做小程序的自动化,原本想用appium的,但是appium搭环境太麻烦了,所以调研了下airtest,本文主要讲解一下airtest的实战笔记

一、AirTest介绍

  • 官方文档:https://airtest.doc.io.netease.com/
  • Airtest是一款基于 Python 的、跨平台的UI自动化测试框架。因为它基于 图像识别 的原理,所以适用于所有 Android、 iOS和 Windows 应用。因此,不论是手机上的app还是游戏,或者是Windows电脑上面的应用或者游戏等等,都可以用Airtest框架。

二、环境准备

  • adb环境搭建

https://blog.csdn.net/weixin_43927138/article/details/90477966

  • AIrTest Ide下载 Ide已经集成了python和pocoui环境,可以直接用

https://airtest.netease.com/

  • 本地python环境搭建

pip install airtest
pip install pocoui
注意:jinja2版本依赖有版本限制最后 安装3.0
pip install jinja2==3.0

  • 自动化准备
  1. 确保手机已经开启开发者模式
  2. 部分手机需要开启“允许通过USB安装应用”等权限
  3. 使用usb线连接手机,并允许电脑对手机进行调试
  4. 在AirTest Ide中refresh ADB 点击对应设备connect即可
from airtest.core.api import *
# 在Android中,有一个平台独有的接口list_app可以列出所有安装过的应用
dev = device()  # 先获取到当前设备对象,这里的dev即是一个Android对象
print(dev.list_app())  # 然后就可以调用平台独有接口了

三、Ide使用

image.png

四、Api使用

  • touch 点击元素

参数:
v: 点击的图片或者坐标
times:点击次数 默认1次
duration: 按住时间 默认0.01s
right_click:只对web ui有效
image.png

  • wait 等待元素出现,出现则返回中心坐标,等不到则返回TargetNotFountErro

参数:
v:点击的图片或者坐标
timeout:等待超时的时间,默认20s
interval:每次寻找的时间间隔,默认0.5s
intervalfunc: 图片没有找到就执行后面的函数,默认为None
image.png

  • swipe 滑动屏幕

参数:
v1:点击的图片或坐标
v2:图片或者坐标,v1滑到v2
vector:[x,y]录制的自动生成,记录滑动比例;向右滑动为X轴正向,向下有Y轴正向
duration:滑动持续的时长,默认0.5s
image.png

  • exist 判断元素是否存在

参数:
v:元素图片
返回值:
如果存在就返回图片中心坐标,不存在就返回False
image.png

  • text 输入文本

参数:
text:输入的文本内容
enter:完成输入后自动执行enter操作,默认为True
search:完成输入后强行进行search操作,默认为False
image.png

  • keyevent 模拟键盘操作

对照表:https://www.cnblogs.com/vip136510786/p/14705567.html
常用:
KEYCODE_HOME : 3
KEYCODE_BACK : 4
KEYCODE_ENTER : 66
KEYCODE_DEL : 67
用法:keyevent(“HOME”) or keyevent(“3”)

  • snapshot 截图可以在报告中展示

参数:
filename:保存截图未指定的文件
msg:描述测试点,可以在报告中呈现

  • sleep 暂停时间等待

参数
sesc:等待的时间:默认为1s
sleep(2)

  • assert_exists

参数:
v:图片
msg:描述测试点,会记录到报告中
返回值
存在则返回图片的中心坐标,不存在就raise AssertionError
image.png

  • assert_not_exists

参数:
v:图片
msg:描述测试点,会记录到报告中

  • assert_equal

参数:
first:第一个对比条目
second:第二个对比条目
msg:描述测试点,在测试报告中显示

  • assert_not_equal

参数:
first:第一个对比条目
second:第二个对比条目
msg:描述测试点,在测试报告中显示

五、命令行运行Airtest脚本

# 运行脚本 --device后面接被测设备  --log 后面接日志存放路径
airtest run air_test.air --device android://127.0.0.1:5037/手机设备号 --log ./log

# 报告  第一个参数:脚本   --log_root接log地址  outfile: 报告保存路径
airtest report ./air_test.air --log_root ./log --outfile ./report.html

更多使用命令行运行脚本信息,请参考文档

六、airtest其他api

其他核心api:https://www.cnblogs.com/songzhenhua/p/15315212.html

  • 获取屏幕分辨率
# airtest 获取当前屏幕分辨率
width = G.DEVICE.display_info['width']
height = G.DEVICE.display_info['height']
  • 使用相对分辨率进行滑动
# airtest 获取当前屏幕分辨率
width = G.DEVICE.display_info['width']
height = G.DEVICE.display_info['height']
# 使用分辨率进行向下滑动
swipe(v1=(0,height/5),v2=(0,height*4/5))
  • 通过包名启动app
# 启动程序  start_app(包名)
start_app('com.tencent.mm')

七、poco使用

poco api
# 点击 click
poco(‘btn_back’, type=‘Button’).click()
# 长按 long_click  duration 按键时间
poco(‘star_single’).long_click(duration=5)
# 双击double_click(focus=None, sleep_interval=None) focus 可以传坐标
poco(‘star_single’).double_click()
# 右键点击rclick(focus=None, sleep_interval=None)对web端有效 


# 获取元素属性 attr(attr_name)
poco(‘star_single’).attr(name)
"""
可获取的属性如下
visible: whether or not it is visible to user
text: string value of the UI element
type: the type name of UI element from remote runtime
pos: the position of the UI element
size: the percentage size [width, height] in range of 0~1 according to the screen
name: the name of UI element
…: other sdk implemented attributes
"""


# 判断对象是否存在 exists()
invisible_obj = poco(‘result_panel’, type=‘Layer’)
print(invisible_obj.exists()) # => False. This UI is not visible to user.

# 拖拽元素  drag_to(target, duration=2.0)  target 可以为元素或坐标
poco(‘star’).drag_to(poco(‘shell’))

# 滑动屏幕
poco(‘Scroll View’).swipe([0, -0.1])
poco(‘Scroll View’).swipe(‘up’) # the same as above, also have down/left/right
# 获取元素的坐标 get_position()
x, y = poco(‘Scroll View’).get_position()
end = [x, y - 0.1]
dir = [0, -0.1]
# 滑动
poco.swipe([x, y], end) # drag from point A to point B
poco.swipe([x, y], direction=dir) # drag from point A toward given direction and length

# scroll 滚动
scroll(direction='vertical', percent=0.6, duration=2.0)
"""
    参数含义:
    direction (str) – 滚动的方向
    percent (float) – 根据方向滚动所选UI高度或宽度的距离百分比
    duration (float) – 滚动时长
"""

# 获取元素name属性 get_name()
poco(text='淘宝').get_name() 

# 获取元素size get_size()
poco(textMatches='.*?淘宝'). get_size()

# 获取父节点parent()
# 获取当前元素的后代元素 offspring(name=None, **attrs)
"""
name – query expression of attribute “name”
attrs – other query expression except for the name
"""

# 获取单个子节点元素 child(name=None, **attrs)
poco("plays").child("playBasic").offspring("star_single")
# 获取当前元素下的所有子元素children()
poco("plays").child()


"""
设置属性
# set_text('text_value')设置当前元素的text属性值
# setattr('attr_name', 'attr_value') 设置当前元素的属性值
 
"""

# 等待元素 wait(timeout=3) 等待元素出现后 会返回该元素对象
# 等待元素出现 wait_for_appearance(timeout=120) 等待元素出现 超过时间未出现会报错
start_btn = poco(‘start’)
start_btn.wait_for_disappearance(

# wait_for_any 等待任意一个元素出现后 往下执行
first = poco(‘first’)
second = poco(‘second’)
third = poco(‘third’)
fish = poco.wait_for_any([first, second, third])

# wait_for_all 等待全部元素元素出现后才会往下执行
first = poco(‘first’)
second = poco(‘second’)
third = poco(‘third’)
poco.wait_for_all([first, second, third])

    
# 双指操作 pinch()
pinch(direction='in', percent=0.6, duration=2.0, dead_zone=0.1)
"""
direction (str) – in: 双指往里 out 双指往外
percent (float) – 收缩和展开的百分比
duration (float) – 整个过程持续的时间
dead_zone (float) – 挤压内圆半径。不应大于%
"""
poco定位方法
  1. 根据属性定位

    # 获取text为京东购物的元素
    poco(text="京东购物").wait_for_appearance(timeout=5)
    # 使用层级定位
    poco("plays").child("playBasic").offspring("star_single")
    
  2. 相对选择器

    # 使用层级定位  
    # offspring 当前节点的所有后代元素 offspring("star_single") 后代中的star_single节点
    # child 当前节点的子元素
    poco("plays").child("playBasic").offspring("star_single")
    
  3. 空间顺序选择器

    """
    如果元素的位置发生了变化,那么下标序号仍然是按照选择的那一瞬间所确定的值。
    即,如果选择时①号元素现在去到了③号的位置,那么还是要用 poco(...)[0] 来访问,
    而不是 poco(...)[2]
    """
    name0 = poco("Content").child(type="Text")[0].get_name()
    name1 = poco("Content").child(type="Text")[1].get_name()
    name2 = poco("Content").child(type="Text")[2].get_name()
    
  4. 正则表达式定位元素 !! 推荐使用

    # 非正则匹配
    poco(text="手机淘宝").click()
    # 使用正则匹配text
    poco(textMatches=".*淘宝").click()
    # 非正则匹配
    poco(name="com.netease.cloudmusic:id/portalTitle",text="每日推荐")
    # 正则匹配
    poco(nameMatches=".*portalTitle",textMatches=".*推荐")
    
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值