Appium滑动屏幕(1)——swipe

方法:swipe(self, start_x, start_y, end_x, end_y, duration=None)

参数:start_x, start_y : 表示开始滑动时的初始坐标,即从6哪里开始滑动;

      end_x,   end_y : 表示滑动后的坐标,即滑动到哪里;

      duration : 表示滑动过程的时间间隔,单位以毫秒计算。模拟操作时,我们最好设置这个时间间隔,避免由  于代码运行太快,但是真机或者模拟器反应比较慢,导致操作失败。

坐标说明:屏幕左上角为起点,坐标为(0,0),起点往右为X轴,起点以下为Y轴。

思路:如果我们要模拟左滑的操作,初始点坐标就在右边(大概屏幕宽的3/4,高的1/2),然后向左滑动,在左边结束,结束点坐标大概是屏幕宽1/4,高1/2。

实现:

1、获取手机屏幕的宽和高(为了兼容不同手机的分辨率):

def get_phone_size(driver):   

 """获取手机屏幕的大小"""  

  width = driver.get_window_size()['width'] 

 # 获取手机屏幕的宽  

  height = driver.get_window_size()['height']  

# 获取手机屏幕的高    

return width, height

2、左滑代码:

def swipe_left(driver, duration=300):   

 """左滑操作"""   

 width, height = get_phone_size(driver)  

  start_x, start_y = 3/4 * width, 1/2 * height   

 end_x, end_y = 1/4 * width ,1/2 * height   

 driver.swipe(start_x, start_y,end_x, end_y, duration)

3、同理,可得右滑、上滑和下滑代码:

def swipe_right(driver, duration=300):   

 """右滑操作"""   

width, height = get_phone_size(driver)    

start_x, start_y = 1/4 * width, 1/2 * height    

end_x , end_y = 3/4 * width, 1/2 * height    

driver.swipe(start_x,start_y,end_x, end_y, duration)


def swipe_up(driver, duration=300):   

 """上滑操作"""

width, height = get_phone_size(driver)

start_x , start_y = 1/2 * width ,3/4 * height    

end_x , end_y = 1/2 * width , 1/4 * height    print(end_y)    

driver.swipe(start_x, start_y, end_x, end_y, duration)


def swipe_down(driver, duration=300):  

  """下滑操作"""   

 width, height = get_phone_size(driver)    

start_x, start_y = 1/2 * width, 1/4 * height    

end_x , end_y = 1/2 * width, 3/4 * height   

driver.swipe(start_x, start_y,end_x, end_y ,duration)

4、根据实际方向滑动,调用运行上述代码即可,例如我们需要上滑:

time.sleep(1)#如果不等待,swipe操作不顺畅

swipe_up(driver)#上滑操作

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容