Airtest自动化测试框架常见问题与代码示例详解

Airtest自动化测试框架常见问题与代码示例详解

Airtest UI Automation Framework for Games and Apps Airtest 项目地址: https://gitcode.com/gh_mirrors/ai/Airtest

前言

Airtest是一款强大的跨平台UI自动化测试框架,广泛应用于游戏和App的自动化测试领域。本文将详细介绍Airtest框架在实际使用中的常见问题解决方案和代码示例,帮助开发者快速掌握框架的核心功能。

一、脚本初始化配置

1.1 auto_setup自动配置

auto_setup()是Airtest脚本中最常用的初始化方法,它可以自动配置运行环境的各种参数:

from airtest.core.api import auto_setup

# 基本用法
auto_setup(__file__)

# 完整参数示例
auto_setup(
    __file__,
    devices=["android://127.0.0.1:5037/emulator-5554"],
    logdir=True,
    project_root=r"D:\test",
    compress=90
)

参数说明:

  • basedir:脚本所在路径,通常使用__file__
  • devices:设备连接字符串列表
  • logdir:日志存储路径,True表示使用脚本所在目录
  • project_root:项目根目录
  • compress:截图压缩质量(1-100)

二、设备连接与管理

2.1 设备连接方式

Airtest支持多种设备连接方式:

from airtest.core.api import connect_device, init_device

# 使用URI字符串连接设备
connect_device("Android://127.0.0.1:5037/SJE5T17B17")
connect_device("iOS:///127.0.0.1:8100")
connect_device("Windows:///123456")

# 使用init_device连接
init_device(platform="Android", uuid="SJE5T17B17", cap_method="JAVACAP")

2.2 多设备管理

当需要操作多个设备时,可以使用以下方法:

from airtest.core.api import set_current, device

# 切换当前设备
set_current(0)  # 切换到第一个连接的设备
set_current("serialno1")  # 切换到指定序列号设备

# 获取当前设备实例
dev = device()
dev.swipe_along([[959, 418], [1157, 564]])

三、基础操作示例

3.1 坐标点击与滑动

from airtest.core.api import touch, swipe

# 绝对坐标点击
touch([100, 100])

# 基于模板图片点击
touch(Template(r"button.png"))

# 带参数的点击
touch([100, 100], times=2, duration=1)

# 滑动操作
swipe([378, 1460], [408, 892], duration=1)
swipe(Template(r"slider.png"), vector=[-0.03, -0.33])

3.2 应用管理

from airtest.core.api import start_app, stop_app, install, uninstall

# 应用启动与停止
start_app("com.example.app")
stop_app("com.example.app")

# 应用安装与卸载
install(r"path/to/app.apk")
uninstall("com.example.app")

四、输入与按键操作

4.1 文本输入

from airtest.core.api import text, keyevent

# 基本文本输入
touch(text_field)  # 先点击文本框
text("Hello Airtest")

# 不带回车输入
text("123", enter=False)

# Android平台搜索按钮
text("keyword", search=True)

4.2 按键操作

# Android按键
keyevent("HOME")
keyevent("BACK")

# Windows按键
keyevent("{DEL}")
keyevent("%{F4}")  # Alt+F4

# iOS按键(仅HOME键)
keyevent("HOME")

五、日志与报告

5.1 日志记录

from airtest.core.api import log

# 记录自定义日志
log("测试步骤开始", desc="重要操作", snapshot=True)

# 设置日志级别(减少输出)
import logging
logging.getLogger("airtest").setLevel(logging.ERROR)

5.2 报告生成

from airtest.report.report import simple_report, LogToHtml

# 简单报告
simple_report(__file__, logpath=True, output="log.html")

# 详细报告
h1 = LogToHtml(script_root=r'script_path', log_root=r"log_path")
h1.report()

六、截图与图像识别

6.1 截图操作

from airtest.core.api import snapshot
from airtest.core.settings import Settings as ST

# 基本截图
snapshot("home.jpg", "首页截图")

# 设置全局截图质量
ST.SNAPSHOT_QUALITY = 90
ST.IMAGE_MAXSIZE = 800  # 限制截图最大尺寸

6.2 局部截图与识别

from airtest.aircv import crop_image, match_in

# 截取屏幕局部区域
screen = device().snapshot()
local_area = crop_image(screen, (0, 160, 1067, 551))

# 在局部区域中查找目标
template = Template(r"icon.png")
pos = template.match_in(local_area)

七、断言验证

Airtest提供了多种断言方式:

from airtest.core.api import *

# 存在性断言
assert_exists(Template(r"success.png"), "验证成功提示"

# 不存在断言 
assert_not_exists(Template(r"error.png"), "验证无错误提示")

# 值断言
assert_equal(poco("title").get_text(), "首页", "验证标题文本")
assert_not_equal(actual, expected, "值不相等验证")

八、高级技巧

8.1 坐标转换

# 绝对坐标与相对坐标转换
def abs_to_rel(x, y):
    w, h = device().display_info['width'], device().display_info['height']
    return x/w, y/h

def rel_to_abs(x, y):
    w, h = device().display_info['width'], device().display_info['height']
    return x*w, y*h

8.2 脚本模块化

# 引用其他.air脚本中的函数
from airtest.core.api import using
using("common.air")
from common import common_function
common_function()

结语

本文详细介绍了Airtest自动化测试框架的常见使用场景和代码示例,涵盖了从基础操作到高级技巧的各个方面。掌握这些内容后,开发者可以更加高效地编写自动化测试脚本,应对各种测试需求。在实际项目中,建议结合官方文档和实际业务场景,灵活运用这些技术点。

Airtest UI Automation Framework for Games and Apps Airtest 项目地址: https://gitcode.com/gh_mirrors/ai/Airtest

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

富珂祯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值