51Testing软件测试论坛

标题: Appium滑动屏幕(1)——swipe [打印本页]

作者: lsekfe    时间: 2020-9-15 10:29
标题: 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、获取手机屏幕的宽和高(为了兼容不同手机的分辨率):
  1. def get_phone_size(driver):   

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

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

  4. # 获取手机屏幕的宽  

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

  6. # 获取手机屏幕的高   

  7. return width, height
复制代码

2、左滑代码:
  1. def swipe_left(driver, duration=300):   

  2. """左滑操作"""   

  3. width, height = get_phone_size(driver)  

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

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

  6. driver.swipe(start_x, start_y,end_x, end_y, duration)
复制代码
3、同理,可得右滑、上滑和下滑代码:
  1. def swipe_right(driver, duration=300):   

  2. """右滑操作"""   

  3. width, height = get_phone_size(driver)   

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

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

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



  7. def swipe_up(driver, duration=300):   

  8. """上滑操作"""

  9. width, height = get_phone_size(driver)

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

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

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



  13. def swipe_down(driver, duration=300):  

  14.   """下滑操作"""   

  15. width, height = get_phone_size(driver)   

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

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

  18. driver.swipe(start_x, start_y,end_x, end_y ,duration)
复制代码
4、根据实际方向滑动,调用运行上述代码即可,例如我们需要上滑:
  1. time.sleep(1)#如果不等待,swipe操作不顺畅

  2. swipe_up(driver)#上滑操作
复制代码

作者:静静地就好
链接:https://www.jianshu.com/p/db0e1a15cb93
来源:简书







欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2