Appium-Python3--UI自動化-[-4-]-頁面-上下滑動、左右滑動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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM