2.Airtest 常用函数(持续更新)

一、异常类

建议对每一个可能出现异常的地方都进行异常处理,并截图,最后看报告时方便迅速定位问题

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)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章