appium+python:自己写的一个滑动控件的方式

时间:2023-03-09 16:09:44
appium+python:自己写的一个滑动控件的方式
#调用方式
roll_ele("ID","ele_id","7","up",3)#将控件分为7格,从底部倒数第二格向上滑动3格
#滑动控件的方法
def roll_ele(id,ele_id,size,direct,rollsize=1):
"""
参数id为定位方式,eled_id为具体控件的id或xpath等,size为需要把该控件分成几格,direct为滑动方向,up或down(上或下),rollsize为要滑动几格,该参数取值范围为(1,size-2)
"""
#以下是滑动该单据详情数据---------------------------------------------------------------------------- ele_ = Element(id,ele_id) #Element为自己包装的一个定位控件的类
start_loc = ele_.get_location_start() #获取控件的起点坐标,get_location_start()为自己写的的一个获取控件的起点坐标和结束坐标的方法,appium直接为element.location,返回的类型为dict,{'x':520,'y':1230} size_ele = ele_.get_ele_size() #获取控件的大小,get_ele_size()为自己写的一个获取控件大小的方法,appium直接为element.size,返回的类型为dict,如{'height':589,'width':270} #控件上方中心的坐标
ele_start = []
ele_start.append(float(start_loc['x'])+float(size_ele['width'])/2)
ele_start.append(float(start_loc['y'])) #控件下方中心的坐标
ele_end = []
ele_end.append(float(start_loc['x'])+float(size_ele['width'])/2)
ele_end.append(float(start_loc['y'])+float(size_ele['height'])) #控件列有size行,每次滑动1/size格,即size_ele['height']/size个长度
per_height = float(size_ele['height'])/size #每格的高度 #若为up,则滑动时,每次从倒数第二格坐标,滑动到上面rollsize格的坐标(因为直接从控件最底部滑动无法滑动,会失败);其他则都向下滑动
if direct=='up':#swipe_to为自己包装的driver.swipe()方法
swipe_to(ele_end[0],ele_end[1]-per_height,ele_end[0],ele_end[1]-per_height-rollsize*per_height,1000)
else:
swipe_to(ele_start[0],ele_start[1]+per_height,ele_start[0],ele_start[1]+per_height+rollsize*per_height,1000)
time.sleep(1)