二、Appium-python-UI自動化之頁面-上下滑動、左右滑動swipe方法操作

1.首先看app中怎麼劃分橫縱座標

 

 

2.swipe函數

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 (int): x-coordinate at which to start
            start_y (int): y-coordinate at which to start
            end_x (int): x-coordinate at which to stop
            end_y (int): y-coordinate at which to stop
            duration (:obj:`int`, optional): time to take the swipe, in ms.

        Usage:
            driver.swipe(100, 100, 100, 400)

        Returns:
            `appium.webdriver.webelement.WebElement`
        """
        # `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
解析:
swipe(self, start_x, start_y, end_x, end_y, duration=None)
swipe(開始橫座標,開始縱座標,結束橫座標,結束縱座標,持續的時間,單位毫秒)




重點:在執行swipe之前一定要強制sleep  3-4s,不然會出現:wipe did not complete successfully報錯




3.各種滑動
  3.1 因爲每個手機的座標可能都不一樣,這裏我們可以通過先獲取手機屏幕的長和寬,然後再次計算需要滑動的座標位置,
    
def get_myWindow_size(self):

    '''
    獲取手機長寬
    :return:
    '''

    x = self.driver.get_window_size()['width']  # 獲取x軸的長度
    y = self.driver.get_window_size()['height']  # 獲取y軸的長度

    return x,y

    3.2 向下滑動,即x軸不變,y軸減小

def swipeDown(self):

    '''
    頁面向下滑動
    :return:
    '''

    size = self.get_myWindow_size()
    MyLog.logger().info("size :" +str(size[0])+','+str(size[1]))
    x1 = int(size[0] * 0.5)  # size[0]取元組的第一個值,*0.5表示中間的點
    y1 = int(size[1] * 3/4)  # size[1]取元組的第二個值,*0.1表示距離底部近
    y2 = int(size[1] * 1/6)
    time.sleep(4)
    self.driver.swipe(x1, y1, x1, y2, 1000)

   3.3 向下滑動,即x軸不變,y軸增大

  

def swipeUp(self):

    '''
    頁面向上滑動
    :return:
    '''

    size = self.get_myWindow_size()
    MyLog.logger().info("size :" +str(size[0])+','+str(size[1]))
    x1 = int(size[0] * 0.5)  # size[0]取元組的第一個值,*0.5表示中間的點
    y1 = int(size[1] * 1/6)  # size[1]取元組的第二個值,*0.1表示距離底部近
    y2 = int(size[1] * 3/4)
    time.sleep(4)
    self.driver.swipe(x1, y1, x1, y2, 1000)

   3.4 向右滑動 ,即x減小,y軸不變

def swipRight(self):

    '''
    頁面向右滑動
    :return:
    '''

    size = self.get_myWindow_size()
    MyLog.logger().info("size :" +str(size[0])+','+str(size[1]))
    x1 = int(size[0] * 0.5)  # size[0]取元組的第一個值,*0.5表示中間的點
    x2 = int(size[0] * 0.25)  # size[0]取元組的第一個值,*0.5表示中間的點
    y1 = int(size[1] * 3/4)  # size[1]取元組的第二個值,*0.1表示距離底部近
    y2 = int(size[1] * 1/6)
    time.sleep(4)
    self.driver.swipe(x1, y1, x2, y1, 1000)

   3.5 向左滑動,即x軸增大,y軸不變

def swipLeft(self):

    '''
    頁面向左滑動
    :return:
    '''

    size = self.get_myWindow_size()
    MyLog.logger().info("size :" +str(size[0])+','+str(size[1]))
    x1 = int(size[0] * 0.25)  # size[0]取元組的第一個值,*0.5表示中間的點
    x2 = int(size[0] * 0.75)  # size[0]取元組的第一個值,*0.5表示中間的點
    y1 = int(size[1] * 3/4)  # size[1]取元組的第二個值,*0.1表示距離底部近
    y2 = int(size[1] * 1/6)
    time.sleep(4)
    self.driver.swipe(x1, y1, x2, y1, 1000)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章