搬运自codeskyblue:https://github.com/openatx/uiautomator2,推荐大家多多阅读原文档,记得star下哦~
1.点击特定对象中心位置:
d(text="Settings").click()
2.等待对象显示并点击,等待最多10s:
d(text="Settings").click(timeout=10)
3.当10s内对象出现就点击,默认0s:
clicked = d(text='Skip').click_exists(timeout=10.0)
4.点击并轮询对象直到消失,其中maxretry为最多点击次数,默认10;interval为轮询时间间隔,默认1:
d(text="Skip").click_gone(maxretry=10, interval=1.0)
5.长按指定的对象,duration为按住时间,timeout为超时时间:
d(text="Settings").long_click(duration=10,timeout=10)
6.拖动指定对象到x,y位置,0.5s完成:
d(text="Settings").drag_to(x, y, duration=0.5)
7.拖动指定对象到另一个对象位置,0.5s内完成:
d(text="Settings").drag_to(text="Clock", duration=0.5)
8.双指从(a,b),(c,d)滑动(a1,b1),(c1,d1),步长100:
d().gesture((a,b),(c,d), a1,b1),(c1,d1),steps=100)
9.从屏幕外侧向中心滑动,percent为左右起始位置占两边的比例:
d().pinch_out(percent=33, steps=100)
10.从屏幕中心向外侧滑动,percent为左右起始位置占两边的比例:
d().pinch_out(percent=33, steps=100)
11.等待对象:
d(text='相机').wait(10)
12.等待对象消失,最多等待10s:
d(text='相机').wait_gone(timeout=10)
13.滚动:
a.向上滚动:d(scrollable=True).scroll(steps=10)
b.向下滑动:d(scrollable=True).scroll.vert.backward()
c.水平向右滚动:d(scrollable=True).scroll.horiz.forward(steps=50)
d.水平向左滚动:d(scrollable=True).scroll.horiz.backward(steps=50)
e.水平滑动到最左边:d(scrollable=True).scroll.horiz.toBeginning(steps=100, max_swipes=1000)
f.水平滑动到最右边:d(scrollable=True).scroll.horiz.toEnd(steps=100, max_swipes=1000)
g.竖直滑动到结尾:d(scrollable=True).scroll.toEnd()
h.竖直滑动到开头:d(scrollable=True).scroll.toBeginning(steps=50)
i.滑动到指定位置(测试):d(scrollable=True).scroll.to(text ='测试')