Airtest 學(xué)習(xí)筆記
模擬輸入
.touch
模擬點(diǎn)觸操作
touch(v, times=2, **kwargs)
參數(shù)v - 點(diǎn)觸的目標(biāo):Template對象或是絕對坐標(biāo)
例如:
# 絕對坐標(biāo)
>>> touch((100,200))
# 點(diǎn)擊圖片中心
>>> touch(Template(r"tp1150.png",target_pos=5))
# 相對坐標(biāo),如下為屏幕中心
>>> touch((0.5,0.5))
.swipe
模擬滑動(dòng)操作
swipe(v1, v2=None, vector=None, **kwargs)
參數(shù)v1, v2- 滑動(dòng)的目標(biāo):Template對象或是絕對坐標(biāo);
vector 滑動(dòng)向量。
例如:
>>> swipe(Template(r"tpl1606814865574.png"), vector=[-0.0316, -0.3311])
>>> swipe((100, 100), (200, 200))
.text
模擬文字輸入
text(text, enter=True, **kwargs)
例如:
# 輸入test,然后點(diǎn)擊回車/確認(rèn)
>>> text("test")
# 輸入test,不點(diǎn)回車/確認(rèn)
>>> 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 - 圖片質(zhì)量[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
等待在設(shè)備屏幕上找到匹配圖片
wait(v, timeout=None, interval=0.5, intervalfunc=None)
例如:
# 每次執(zhí)行失敗后執(zhí)行特定命令
>>> 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)
# 表達(dá)式是否為True
>>> assert_true(expr, msg='', snapshot=True)
>>> assert_false(expr, msg='', snapshot=True)
# 是否是同一個(gè)對象
>>> 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)
# 是否是該類的實(shí)例
>>> 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)