Appium+Python swipe 用法详解

API

def swipe(self, start_x, start_y, end_x, end_y, duration=None):
    """Swipe from one point to another point, for an optional duration.

    :Args:
     - start_x - x-coordinate at which to start
     - start_y - y-coordinate at which to start
     - end_x - x-coordinate at which to stop
     - end_y - y-coordinate at which to stop
     - duration - (optional) time to take the swipe, in ms.

    :Usage:
        driver.swipe(100, 100, 100, 400)
    """
    # `swipe` is something like press-wait-move_to-release, which the server
    # will translate into the correct action
    action = TouchAction(self)
    action \
        .press(x=start_x, y=start_y) \
        .wait(ms=duration) \
        .move_to(x=end_x, y=end_y) \
        .release()
    action.perform()
    return self
  • start_x - 滑动开始x轴坐标
  • start_y - 滑动开始y轴坐标
  • end_x - 滑动结束x轴偏移量
  • end_y - 滑动结束y轴偏移量
  • duration - (可选) 执行此次滑动时间,单位毫秒.

其中end_x和 end_y 为基于start_x和start_y的偏移量。最终在执行中的 to_x = start_x+end_x 并非end_x
duration 参数单位为ms(默认5毫秒) 注意1s =1000ms

示例:

获取屏幕尺寸

    def GetPageSize(self):
        x = self.driver.get_window_size()['width']
        y = self.driver.get_window_size()['height']
        return (x, y)

左滑

  • 技巧:左滑是从较大x值 --->较小x值,所以 to_x=sx+(-ex)

  • 技巧:左滑时y轴值基本无变化,所以ey=0

  • 技巧:sx的值一定大于屏幕尺寸的53%,否则虽向左滑动但不能生效切换页面

      def swipe_left(self):
          s = self.GetPageSize()
          sx = s[0] * 0.57
          sy = s[1] * 0.75
          ex = s[0] * 0.55
          ey = 0
          self.driver.swipe(sx, sy, -ex, ey, dt)
    
  •  

右滑

  • 技巧:sx的值一定不能大于屏幕尺寸的 46%,否则虽然向右滑动但不能生效切换页面

      def swipe_right(self):
          s = self.GetPageSize()
          sx = s[0] * 0.43
          sy = s[1] * 0.75
          ex = s[0] * 0.54
          ey = 0
          self.driver.swipe(sx, sy, ex, ey, dt)
    
  •  

上滑 (俗称上拉加载更多)

  • 技巧:上滑是从较大y值--->较小y值,所以to_y=sy+(-ey)

  • 技巧:上滑时x轴值基本无变化,所以ex = 0

  • 技巧:sx的值可s[0]范围内随意,sy和ey 需在 s[1]0.2 -- s[1]-s[1]0.4 之间取值,
    因为需要考虑:状态条、导航栏、底部功能栏等所占数值

     def swipe_up(self):
         s = self.GetPageSize()
         sx = s[0] * 0.43
         sy = s[1] * 0.45
         ex = 0
         ey = s[1] * 0.55
         self.driver.swipe(sx, sy, ex, -ey, dt)
    
  •  

下滑(俗称下拉刷新)

  • 技巧:sx的值可s[0]范围内随意,sy和ey 需在 s[1]*0.2 -- s[1]-s[1]*0.4 之间取值,
    因为需要考虑:状态条、导航栏、底部功能栏等所占数值

      def swipe_down(self):
          s = self.GetPageSize()
          sx = s[0] * 0.35
          sy = s[1] * 0.45
          ex = 0
          ey = s[1] * 0.55
          self.driver.swipe(sx, sy, ex, ey, dt)
    
  •  

其中如有错误,欢迎指正 讨论QQ群:658703911

你可能感兴趣的:(Python,appium)