airtest使用记录(长期更新)
1.引入相同路径下其他airtest脚本
# 引入 airtest.core.api from airtest.core.api import * # 引入 initial.air using("initial.air") # 引入 initial.air下initial.py里的class from initial import tChange,sChange,start,tLogin,sGuide,stop,cAndt,cTry,cAndtn,cTryn,c_img,special_check
2.兼容相对坐标之“touch”:
由于airtest的touch只支持传入绝对坐标,这里做了一点修改,使用touch_change替换脚本中用到的touch
class TouchChange: @staticmethod def touch_change(x,y): if x<=1.0 and y<=1.0: w,h=device().get_current_resolution() touch([x*w,y*h]) else: touch([x,y])
3.兼容相对坐标之“swipe”:
class SwipeChange: @staticmethod def swipe_change(x1,y1,x2,y2,time=None): if time: time=time else: time=0.5 w,h=device().get_current_resolution() swipe([x1*w,y1*h],[x2*w,y2*h],duration=time)
4.解决assert_exists和assert_not_exists抛错时终止脚本运行的问题
使用包装后的 check_try 替换 assert_exists
def check_try(v,info): try: assert_exists(v, info) except: log("error:找不到该UI")
使用包装后的 check_not_try 替换 assert_not_exists
def check_not_try(v,info): try: assert_not_exists(v, info) except: log("error:仍然能找到该UI")
5.查找特征UI并点击指定坐标
使用场景:分割查找操作与点击操作。查找唯一特征UI存在,并点击指定坐标,直到特征UI消失,最多重复times次。
class cAndt: @staticmethod def checkAndtouch(v,pos,times=None,info=None): if not info: info = '检查图片是否存在,若不存在则报错' else: info = info cTry.checkTry(v,info)#检查图片是否存在,若不存在报错 if not times: times = 4 else: times = times count = 1 while count<=times and exists(v): tChange.touchChange(pos[0], pos[1]) sleep(2.0) count+=1 if count > times:#循环times次后若图片仍存在,则报错 cTryn.checkTrynot(v,'该UI仍然存在')
6.点击目标坐标,直到查找到特征UI
使用场景:需要点击操作后才会出现用来判断的特征图片。查找唯一特征ui不存在,并点击目标坐标,直到特征UI出现,最多重复times次
class cAndtn: @staticmethod def checkAndtouchnot(v,pos,times=None): #log('开始检查图片') cTryn.checkTrynot(v,'检查图片是否不存在,若存在则报错') if not times: times = 4 else: times = times count = 1 while count<=times and not exists(v): #log('开始执行循环%s' %count) tChange.touchChange(pos[0], pos[1]) sleep(2.0) count+=1 if count > times:#循环times次后若图片仍未出现,则报错 cTry.checkTry(v,'该UI未出现')
7.屏幕局部截图(范围:x_min,y_min,x_max,y_max)
def screen_shots(x_min,y_min,x_max,y_max): w,h=device().get_current_resolution()#获取屏幕分辨率#相对坐标转换成绝对坐标 x_min=x_min*w y_min=y_min*h x_max=x_max*w y_max=y_max*h #截图 screen = G.DEVICE.snapshot() #图片剪裁(x_min=x_max,或者,y_min=y_max会报错) screen = aircv.crop_image(screen,(x_min,y_min,x_max,y_max)) #剪裁后的图片保存到日志目录 try_log_screen(screen) #返回截图 return screen
8.局部找图(文件名,相对坐标)
def area_check_img(filename,x_min,y_min,x_max,y_max): w,h=device().get_current_resolution()
# 确定查找范围 local_screen = c_img.screen_shots(x_min,y_min,x_max,y_max) template = Template(filename) pos = template.match_in(local_screen)#返回局部坐标 if pos: pos = [pos[0]+x_min*w,pos[1]+y_min*h]#全局坐标,需要转换成绝对坐标 return pos
9.图片对比返回相似度
使用场景例:装备升级每10级icon更换,根据相似度确认icon是否变换成功
def compare_not_similar(screen_01,screen_02,threshold=None,sign=None): if sign: sign = sign else: sign = "图片仍然存在" if threshold: threshold= threshold else: threshold =0.75 #对比 like_num = find_template(screen_01,screen_02,-1)['confidence'] log('相似度:'+str(like_num)) #对比结果处理 try: if like_num>threshold: raise Exception(sign) #except: except Exception as e: log(e,timestamp=time.time(), desc=sign, snapshot=True) return like_num
10.查找特征UI返回特征ui中心坐标指定偏移量的目标坐标
使用场景:需要操作的坐标与查找的特征UI在同一层级,并且目标坐标会随着特征ui的移动而按照一定偏移量移动。
def extend_check(v,length_x=None,length_y=None): #查找目标图片,返回目标图片坐标指定偏移量的坐标 w,h=device().get_current_resolution() if length_x: length_x=length_x else: length_x=0 if length_y: length_y=length_y else: length_y=0 img = exists(v) if not img: img = (0,0) try: if not img: img = (0,0) raise Exception('未找到该图片') except: log("未找到该图片", traceback.format_exc()) img = list(img) img[0]=img[0]+length_x*w img[1]=img[1]+length_y*h img = tuple(img) return img