Airtest IDE 自动化测试6 - 断言存在( assert_exists)和 断言不存在(assert_not_exists)
前言
Airtest IDE 提供了四种断言快捷断言的方式
- assert_exists 断言存在
- assert_not_exists 断言不存在
- assert_equal 断言相等
- assert_not_equal 断言不相等
断言存在(assert_exists)
assert_exists(*args, **kwargs) 设备屏幕上存在断言目标
参数:
- v 要检查的目标
- msg 断言的简短描述,它将被记录在报告中
- AssertionError 如果断言失败
返回:
- 目标坐标
支持平台: Android, Windows, iOS
示例: assert_exists(Template(r"tpl1607324047907.png"), "assert exists")
使用示例,断言 "我的" 页面存在 "登录/注册" 按钮
运行用例后查看测试报告
断言不存在(assert_not_exists)
assert_not_exists 跟上面的 assert_exists 用法一样
断言我的页面不存在“新课推荐”
断言失败 AssertionError
当断言失败的时候,会抛出AssertionError异常,程序不会继续往下走了
在我的页面,查找“新课推荐”,故意让断言失败
如果出现断言失败,会出现AssertionError: Template(D:\airtest_code\a3.air\tpl1621832250338.png) does not exist in screen, message: 我的-存在登录/注册按钮
airtest.core.error.TargetNotFoundError: 'Picture Template(D:\\airtest_code\\a3.air\\tpl1621832250338.png) not found in screen'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "airtest\cli\runner.py", line 73, in runTest
File "site-packages\six.py", line 703, in reraise
File "airtest\cli\runner.py", line 70, in runTest
File "D:\airtest_code\a3.air\a3.py", line 12, in <module>
assert_exists(Template(r"tpl1621832250338.png", record_pos=(-0.39, 0.069), resolution=(720, 1280)), "我的-存在登录/注册按钮")
File "airtest\utils\logwraper.py", line 90, in wrapper
File "airtest\core\api.py", line 669, in assert_exists
AssertionError: Template(D:\airtest_code\a3.air\tpl1621832250338.png) does not exist in screen, message: 我的-存在登录/注册按钮
----------------------------------------------------------------------
Ran 1 test in 41.761s
FAILED (failures=1)
关于查找超时
当页面上找不到元素的时候,不会立马断言失败,此时会有个timeout时间,默认是20秒。
ST.FIND_TIMEOUT
是在setting.py文件配置的,默认查找超时是20s
# airtest.core.setting.py
class Settings(object):
DEBUG = False
LOG_DIR = None
LOG_FILE = "log.txt"
RESIZE_METHOD = staticmethod(cocos_min_strategy)
# keypoint matching: kaze/brisk/akaze/orb, contrib: sift/surf/brief
CVSTRATEGY = ["surf", "tpl", "brisk"]
if sys.version_info[:2] > (3, 7):
CVSTRATEGY = ["kaze", "tpl", "brisk"]
KEYPOINT_MATCHING_PREDICTION = True
THRESHOLD = 0.7 # [0, 1]
THRESHOLD_STRICT = None # dedicated parameter for assert_exists
OPDELAY = 0.1
FIND_TIMEOUT = 20
FIND_TIMEOUT_TMP = 3
PROJECT_ROOT = os.environ.get("PROJECT_ROOT", "") # for ``using`` other script
SNAPSHOT_QUALITY = 10 # 1-100 https://pillow.readthedocs.io/en/5.1.x/handbook/image-file-formats.html#jpeg
# Image compression size, e.g. 1200, means that the size of the screenshot does not exceed 1200*1200
IMAGE_MAXSIZE = os.environ.get("IMAGE_MAXSIZE", None)
SAVE_IMAGE = True
判断元素不存在,查找超时时间是FIND_TIMEOUT_TMP = 3
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2018-05-24 python笔记16-执行cmd指令(os.system和os.popen)