Airtest+Poco快速上手Android (ui定位)测试

2,066 阅读4分钟

简介¶ AirtestIDE 是一个跨平台的UI自动化测试编辑器,适用于游戏和App。

自动化脚本录制、一键回放、报告查看,轻而易举实现自动化测试流程 支持基于图像识别的Airtest框架,适用于所有Android/iOS/Windows应用 支持基于UI控件搜索的Poco框架,适用于Unity3d,Cocos2d与Android/iOS App等多种平台 能够运行在Windows和MacOS上

官网介绍 airtest.doc.io.netease.com/tutorial/1_…

环境搭载——一键安装,解压即可用,粗暴解决环境搭建问题(python2.7+robotframework-64bit及AirtestIDE app一键安装) 链接:pan.baidu.com/s/1nRJLGxhM… 提取码:nzoc

QQ图片20210618102550.png

啥也不说了,直接上代码

`# -- encoding=utf8 -- author = "Administrator" import unittest from airtest.core.api import * from airtest.cli.parser import cli_setup

if not cli_setup(): auto_setup(file, logdir=True, devices=["android://127.0.0.1:5037/CLB7N19301001007?cap_method=MINICAP_STREAM&&ori_method=MINICAPORI&&touch_method=MINITOUCH",]) #作者
author = "NOVO"

from poco.drivers.android.uiautomation import AndroidUiautomationPoco from poco.drivers.unity3d import UnityPoco #唤醒poco初始化 poco = UnityPoco() poco = AndroidUiautomationPoco() #poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)

keyevent("POWER")#电源键

script content

print("start...")

#打开app(包名) start_app("com.miaogonghao.pdd")

#关闭

stop_app('package_name')

# #返回首页

# keyevent("HOME")

#安卓的提示——第二次不一定会用(判断True执行,false跳过) if poco("com.android.permissioncontroller:id/permission_allow_button").wait(2).exists(): poco("com.android.permissioncontroller:id/permission_allow_button").click() #首次登录——隐私协议、通用条款
if poco("com.miaogonghao.pdd:id/selectBtn").wait(3).exists(): sleep(0.5) poco("com.miaogonghao.pdd:id/tvAgreeTips").click() sleep(0.5) keyevent("KEYCODE_BACK")#返回键 poco("com.miaogonghao.pdd:id/selectBtn").click() poco("com.miaogonghao.pdd:id/btnSure").click()

if poco(text="我的").wait(10).exists(): poco(text="我的").click() sleep(0.5)

#判断(断言——存在) assert_equal(poco(text="我的").exists(), True, "我的页面存在")

#判断(断言——不存在) assert_not_equal(poco(text="推荐").exists(), True, "不在首页")

#判断存在 if poco(text="登录/注册").exists(): poco(text="登录/注册").click() sleep(0.5) poco(text="密码登录").click() sleep(1) passport = "1868888888888" password = "#*************" sleep(1) poco(text="请输入手机号码").wait(10).click() sleep(3) text(passport) poco(text="输入密码").click() sleep(1) text("hao123com") sleep(0.5) poco(text="登录").click()

if poco(text="首页").wait(8).exists(): poco(text="首页").click()

if poco(text="推荐").exists(): poco(text="关注").click()

poco(name='').swipe('up')

poco(text="美食").click()
snapshot()

#上下滑动

获取设备的高度和宽度

width, height = device().get_current_resolution()

校准滑动的起点和终点

start_pt1 = (width / 2,height * 0.8)#0.9高度 end_pt1 = (width / 2,height * 0.2)

上滑N次:

slide = 5 for i in range(slide): swipe(start_pt1, end_pt1,1000) sleep(0.8) # 等待设备的响应

获取设备的高度和宽度

width, height = device().get_current_resolution()

校准滑动的起点和终点

start_pt2 = (width *0.9,height / 2) end_pt2 = (width *0.1,height / 2)

左滑N次:

slide = 2 for i in range(slide): swipe(start_pt2 , end_pt2,1000) sleep(0.8) # 等待设备的响应

#点击搜索按钮 #className = android.widget.ImageView poco("com.miaogonghao.pdd:id/search").click()#根据id=search if poco(text="请输入搜索关键字").wait(10).exists(): keyevent("KEYCODE_0") poco(text="请输入搜索关键字").click() sleep(2) text("女") if poco(text="用户").wait(10).exists(): poco(text="用户").click()
keyevent("KEYCODE_BACK")#返回键

keyevent("KEYCODE_VOLUME_DOWN")#音量减小

keyevent("KEYCODE_VOLUME_UP")#音量增大

keyevent("POWER")#电源键

wake()#唤醒设备

home()#返回键

shell("svc wifi disable")#关闭wifi

shell("svc wifi enable")#打开wifi

# 清除应用数据:clear_app()

clear_app("com.miaogonghao.pdd")

#先定位到该元素(精确定位、模糊定位都可以)

textName=poco(textMatches="^我的任务.*$")

poco("com.miaogonghao.pdd:id/add").click() poco(text="搞笑").click() poco(text="搞笑").click() poco("android.widget.LinearLayout").offspring("com.miaogonghao.pdd:id/recyclerview2").child("com.miaogonghao.pdd:id/title")[0].child("com.miaogonghao.pdd:id/bg").click() poco("android.widget.LinearLayout").offspring("com.miaogonghao.pdd:id/recyclerview2").child("com.miaogonghao.pdd:id/title")[0].child("com.miaogonghao.pdd:id/bg").click() poco("android.widget.LinearLayout").offspring("com.miaogonghao.pdd:id/recyclerview2").child("com.miaogonghao.pdd:id/title")[2].child("com.miaogonghao.pdd:id/bg").click() poco(text="北美洲").click() poco(text="非洲").click() poco("android.widget.LinearLayout").offspring("com.miaogonghao.pdd:id/recyclerview2").child("com.miaogonghao.pdd:id/title")[4].child("com.miaogonghao.pdd:id/bg").click() poco("android.widget.LinearLayout").offspring("com.miaogonghao.pdd:id/recyclerview2").child("com.miaogonghao.pdd:id/title")[3].child("com.miaogonghao.pdd:id/bg").click() poco("android.widget.LinearLayout").offspring("com.miaogonghao.pdd:id/recyclerview2").child("com.miaogonghao.pdd:id/title")[3].child("com.miaogonghao.pdd:id/bg").click() poco(text="欧洲").click() poco("android.widget.LinearLayout").offspring("com.miaogonghao.pdd:id/recyclerview2").child("com.miaogonghao.pdd:id/title")[1].child("com.miaogonghao.pdd:id/bg").click() poco("android.widget.LinearLayout").offspring("com.miaogonghao.pdd:id/recyclerview2").child("com.miaogonghao.pdd:id/title")[1].child("com.miaogonghao.pdd:id/bg").click() #保存 poco("com.miaogonghao.pdd:id/save").click() sleep(2) #播放按钮 if poco("com.miaogonghao.pdd:id/iv_play").wait(2).exists(): poco("com.miaogonghao.pdd:id/iv_play").click()

#点赞按钮 if poco("com.miaogonghao.pdd:id/iv_like").wait(2).exists(): poco("com.miaogonghao.pdd:id/iv_like").click()

#评论poco("com.miaogonghao.pdd:id/iv_comment").click() if poco("com.miaogonghao.pdd:id/iv_comment").wait(2).exists(): poco("com.miaogonghao.pdd:id/iv_comment").click() poco("com.miaogonghao.pdd:id/edit").click() sleep(1) comment = "特殊图" text(comment)#输入 sleep(1) poco(text = "发送").click() keyevent("KEYCODE_BACK")#返回键

#分享
if poco("com.miaogonghao.pdd:id/iv_share").wait(2).exists():
poco("com.miaogonghao.pdd:id/iv_share").click() poco("com.miaogonghao.pdd:id/exit").click() poco("com.miaogonghao.pdd:id/iv_share").click() poco("android.widget.LinearLayout").offspring("com.miaogonghao.pdd:id/design_bottom_sheet").offspring("com.miaogonghao.pdd:id/rv_share").child("android.widget.LinearLayout")[0].child("com.miaogonghao.pdd:id/tv_icon").click() sleep(3) keyevent("KEYCODE_BACK")#返回键 poco("com.miaogonghao.pdd:id/exit").click()

#点击用户名跳转 poco("com.miaogonghao.pdd:id/tv_nickname").click() if poco("com.miaogonghao.pdd:id/tv_nickname").wait(1).exists(): poco("com.miaogonghao.pdd:id/tv_nickname").click() sleep(2) keyevent("KEYCODE_BACK")#返回键

#首次安装app拍摄初始化授权
if poco("com.miaogonghao.pdd:id/record_btn").wait(2).exists():
poco("com.miaogonghao.pdd:id/record_btn").click() if poco("android:id/button1").wait(2).exists(): poco("android:id/button1").click() poco("com.android.permissioncontroller:id/permission_allow_button").click() poco("com.android.permissioncontroller:id/permission_allow_button").click()
keyevent("KEYCODE_BACK")#返回键

#拍摄 if poco("com.miaogonghao.pdd:id/record_btn").wait(2).exists():
poco("com.miaogonghao.pdd:id/record_btn").click() poco("com.miaogonghao.pdd:id/record_btn").click() sleep(15) poco("com.miaogonghao.pdd:id/record_btn").click() poco(text="完 成").click()

poco("com.miaogonghao.pdd:id/next_btn").click()#完成按钮

sleep(1)

poco("com.miaogonghao.pdd:id/upLoad").click()#发布按钮

sleep(1)
for i in range(3):
    keyevent("KEYCODE_BACK")#返回键
    sleep(0.3)

if poco(text="我的").wait(5).exists(): poco(text="我的").click() sleep(1) poco(text="粉丝").click() keyevent("KEYCODE_BACK")#返回键 sleep(0.5) poco(text="关注").click() keyevent("KEYCODE_BACK")#返回键 sleep(0.5) poco(text="喜欢").click() sleep(2) keyevent("KEYCODE_BACK")#返回键 sleep(0.5) poco(text="收藏").click() keyevent("KEYCODE_BACK")#返回键 sleep(0.5) poco(text="消息").click() keyevent("KEYCODE_BACK")#返回键 sleep(0.5) poco(text="消息").click() keyevent("KEYCODE_BACK")#返回键

sleep(0.5)

poco("com.miaogonghao.pdd:id/iv_qrcode_min").click()

keyevent("KEYCODE_BACK")#返回键

sleep(0.5)

poco("com.miaogonghao.pdd:id/iv_setting").click()

keyevent("KEYCODE_BACK")#返回键

# # #拖拽

from poco.drivers.android.uiautomation import AndroidUiautomationPoco

from poco.drivers.unity3d import UnityPoco

poco = UnityPoco()

poco = AndroidUiautomationPoco()

# 不选中UI执行swipe

x, y = poco(text="知识").get_position()

end = [x, y - 0.1]

dir = [-0.1, 0]

# 从点A滑动到点B

poco.swipe([x, y], end)

# 从点A沿着一定的方向滑动一段距离

poco.swipe([x, y], direction=dir)

# poco(text='美食').drag_to(poco(text='其他'))

swipe((200,250),(600,250))

#滑动视频

for i in range(10):

# poco("com.ss.android.ugc.aweme:id/ak2").swipe([0, -0.6])

# 后面的上划操作,改成上划屏幕的90%

poco("com.miaogonghao.pdd:id/tv_content").swipe([0, -0.9])

sleep(0.5)`