Airtest 学习笔记
模拟输入
.touch
模拟点触操作
touch(v, times=2, **kwargs)
参数v - 点触的目标:Template
对象或是绝对坐标
例如:
# 绝对坐标
>>> touch((100,200))
# 点击图片中心
>>> touch(Template(r"tp1150.png",target_pos=5))
# 相对坐标,如下为屏幕中心
>>> touch((0.5,0.5))
.swipe
模拟滑动操作
swipe(v1, v2=None, vector=None, **kwargs)
参数v1, v2- 滑动的目标:Template
对象或是绝对坐标;
vector 滑动向量。
例如:
>>> swipe(Template(r"tpl1606814865574.png"), vector=[-0.0316, -0.3311])
>>> swipe((100, 100), (200, 200))
.text
模拟文字输入
text(text, enter=True, **kwargs)
例如:
# 输入test,然后点击回车/确认
>>> text("test")
# 输入test,不点回车/确认
>>> text("test", enter=False)
.keyevent
模拟按键输入
keyevent(keyname, **kwargs)
例如:
>>> keyevent("HOME")
# The constant corresponding to the home key is 3
>>> keyevent("3") # same as keyevent("HOME")
>>> keyevent("BACK")
>>> keyevent("KEYCODE_DEL")
.snapshot
屏幕截图
snapshot(filename=None, msg='', quality=None, max_size=None)
- filename - 保存的截图文件名
- msg - 截图描述
- quality - 图片质量[1,99]之间
- max_size - 图片最大尺寸
例如:
# Set the screenshot quality to 30
>>> ST.SNAPSHOT_QUALITY = 30
# Set the screenshot size not to exceed 600*600
# if not set, the default size is the original image size
>>> ST.IMAGE_MAXSIZE = 600
# The quality of the screenshot is 30, and the size does not exceed 600*600
>>> touch((100, 100))
# The quality of the screenshot of this sentence is 90
>>> snapshot(filename="test.png", msg="test", quality=90)
# The quality of the screenshot is 90, and the size does not exceed 1200*1200
>>> snapshot(filename="test2.png", msg="test", quality=90, max_size=1200)
.wait
等待在设备屏幕上找到匹配图片
wait(v, timeout=None, interval=0.5, intervalfunc=None)
例如:
# 每次执行失败后执行特定命令
>>> def notfound():
>>> print("No target found")
>>> wait(Template(r"tpl1607510661400.png"), intervalfunc=notfound)
断言
# 图案是否存在
>>> assert_exists(v, msg='')
>>> assert_not_exists(v, msg='')
# 值是否相等
>>> assert_equal(first, second, msg='', snapshot=True)
>>> assert_not_equal(first, second, msg='', snapshot=True)
# 表达式是否为True
>>> assert_true(expr, msg='', snapshot=True)
>>> assert_false(expr, msg='', snapshot=True)
# 是否是同一个对象
>>> assert_is(first, second, msg='', snapshot=True)
>>> assert_is_not(first, second, msg='', snapshot=True)
# 是否包含
>>> assert_in(first, second, msg='', snapshot=True)
>>> assert_not_in(first, second, msg='', snapshot=True)
# 是否是该类的实例
>>> assert_is_instance(obj, cls, msg='', snapshot=True)
>>> assert_not_is_instance(obj, cls, msg='', snapshot=True)
# 是否大于/大于等于
>>> assert_greater(first, second, msg='', snapshot=True)
>>> assert_greater_equal(first, second, msg='', snapshot=True)
# 是否小于/小于等于
>>> assert_less(first, second, msg='', snapshot=True)
>>> assert_less_equal(first, second, msg='', snapshot=True)