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..

 

 

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