前言
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