一、異常類
建議對每一個可能出現異常的地方都進行異常處理,並截圖,最後看報告時方便迅速定位問題
1.InvalidOprationException
這個異常特指無效的操作,或者不起作用的操作
try:
poco.click([1.1, 1.1]) # click outside screen
except InvalidOperationException:
snapshot(msg="出現異常")
2.PocoNoSuchNodeException(經常遇到)
如果從一個不存在的UI空間讀取屬性或操作,就會出現該異常。
node = poco("not existed node")
try:
node.click()
except PocoNoSuchNodeException:
snapshot(msg="出現異常")
try:
node.attr('text')
except PocoNoSuchNodeException:
snapshot(msg="出現異常")
3.PocoTargetTimeout
這個異常只會在你主動等待UI出現或消失時拋出,和 PocoNoSuchNodeException 不一樣,當你的操作速度太快,界面來不及跟着變化的話,你只會遇到 PocoNoSuchNodeException 而不是 PocoTargetTimeout ,其實就是在那個UI還沒有出現的時候就想要進行操作。
try:
poco(text="demo").wait_for_appearance(timeout=10)
except PocoTargetTimeout:
snapshot(msg="出現異常")
4.PocoTargetRemovedException
如果操作速度遠遠慢於UI變化的速度,很可能會出現這個異常。當且僅當訪問或操作一個剛纔存在現在不在的UI元素時,纔會出現,並且一般不會出現。
try:
poco(text="demo").click()
except PocoNoSuchNodeException:
snapshot(msg="出現異常")
二、操作類
1.滾動查找元素(poco_swipe_to)
滾動查找元素,當找到元素後,滑動元素到頁面中間。
用法:poco_swipe_to(text=None, textMatches=None, poco=None)
# 滾動查找元素
def poco_swipe_to(text=None, textMatches=None, poco=None):
find_ele = False
find_element = None
if poco is None:
raise Exception("poco is None")
if text or textMatches:
swipe_time = 0
snapshot(msg="開始滾動查找目標元素")
if text:
find_element = poco(text=text)
elif textMatches:
find_element = poco(textMatches=textMatches)
while True:
snapshot(msg="找到目標元素結果: " + str(find_element.exists()))
if find_element.exists():
# 將元素滾動到屏幕中間
position1 = find_element.get_position()
x, y = position1
if y < 0.5:
# 元素在上半頁面,向下滑動到中間
poco.swipe([0.5, 0.5], [0.5, 0.5+(0.5-y)], duration=2.0)
else:
poco.swipe([0.5, 0.5], [0.5, 0.5-(y-0.5)], duration=2.0)
snapshot(msg="滑動元素到頁面中間: " + str(text) + str(textMatches) )
find_ele = True
break
elif swipe_time < 30:
poco.swipe([0.5, 0.8], [0.5, 0.4], duration=2.0)
# poco.swipe((50, 800), (50, 200), duration=500)
swipe_time = swipe_time + 1
else:
break
return find_ele
2.等待任一元素出現(poco.wait_for_any)
poco.wait_for_any(),等待到任一元素出現,返回UIObjectProxy。
check_list = [poco(text="可清理"), poco(text = '手機很乾淨')]
poco.wait_for_any(check_list, timeout=20)
3.等待所有元素(poco.wait_for_all)
poco.wait_for_all(),等待所有元素出現。
check_list = [poco(text="可清理"), poco(text = '手機很乾淨')]
poco.wait_for_all(check_list, timeout=20)