airtest使用小技巧

1.获取下拉菜单的所有选项

由于下拉菜单中元素name值相同,采用列表循环

def check_waterFlowLevel():
    waterLevel_list = []
    position_dic = {}
    elements = poco("android.widget.RelativeLayout").offspring("com.eco.global.app:id/tv_left")
    print(elements)
    for i in elements:
        waterLevel_list.append(i.get_text())
    waterLevel_list = set(waterLevel_list)
    print("当前拖地水量的值有:" + str(waterLevel_list))
    
    # 获取水量的纵座标位置
    for j in waterLevel_list:
        position_dic[j] = poco(text=j).get_position()[1]
#     print("当前水量和其对应的纵座标位置为:",end="")
#     print(position_dic)
    # 判断水量的排序
    if position_dic['低'] < position_dic['中'] < position_dic['高']:
        print("当前排位正确")
    else:
        print("水量排位错误!")
    return position_dic

2.swipe的多种使用方式

2.1 加元素上滑

# 为进入系统,上滑
    poco(name='com.android.settings:id/category').swipe('up')

 

 2.2 不加元素上滑

x,y=poco.get_position()
dir=[0,-0.5]
poco.swipe([x,y],dir)

2.3 多次滑动,直到找到预期元素

  i = 0
    while True:
        if timezone_element.exists():
            timezone_element.click()
            break
#         设定最多滑动次数(因为系统中只有4个category元素)
        elif i < 5:
#             按category座标来滑动
            x,y=poco.get_position()
            dir=[0,-0.5]
            poco.swipe([x,y],dir)
            i+=1
        else:
            raise PocoNoSuchNodeException

2.4 只滑动固定元素,且设置滑动幅度

#   android:id/numberpicker_input[0]为小时,[1]为分钟
 poco(name='android.widget.NumberPicker').\
    child(name='android:id/numberpicker_input')[1].swipe([0,-0.2])

3. 引用模块

3.1 引用python文件的模块

引用poco中的异常

# 引用python包中poco模块的中的异常
import sys
sys.path.append("D:\python\Lib\site-packages\poco")
import exceptions

# 应用在poco的操作中
while True:
    if timezone_element.exists():
        timezone_element.click()
        break
#   设定最多滑动次数(因为系统中只有4个category元素)
    elif i < 5:
#       按category座标来滑动
        x,y=poco.get_position()
        dir=[0,-0.5]
            poco.swipe([x,y],dir)
        i+=1
    else:
        raise PocoNoSuchNodeException

引用airtest中的异常 

# 引用airtest中的异常
from airtest.core.error import *

# 应用在图像定位
 try:
     wait(Template(r"tpl1585821764025.png", record_pos=(0.003, 0.747), resolution=(720, 1440)),180)
except TargetNotFoundError:
     deboot_flag = 1

4. 判断文件

4.1 判断文件夹

# 如果测试报告文件夹TestReport不存在,就创建
report_path = os.getcwd()+'\\TestReport'
if not os.path.exists(report_path):
   os.mkdir(report_path)

4.2 判断文件

判断文件存在,就写入
if os.path.exists(report_timezone_not_found):
    with open(report_timezone_not_found, 'a+') as f:
          localtime = time.asctime(time.localtime(time.time()))
          # 写入本轮测试结束图标
          f.write('-'*20 + localtime + '-'*20 + '\n')

 

 

To be continued..

 

 

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