Appium swipe實現屏幕滑動


 在 Appium 中提供 swipe() 方法來模擬用戶滑動屏幕。

 swipe() 實現過程 是先通過在屏幕上標記兩個坐標,然后再從開始坐標移動到結束坐標。

 先看下 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`
        """

 start_x:開始坐標 x 軸

 start_y:開始坐標 y 軸

 end_x:結束坐標 x 軸

 end_y:結束坐標 y 軸

 duration:開始坐標移動到結束坐標的時間,默認 None

 

坐標原點位於屏幕左上角

 

 一段簡單代碼:

from appium import webdriver

desired_caps = {
                'platformName': 'Android',
                'deviceName': '192.168.41.101:5555',
                'platformVersion': '9.0',
                # apk包名
                'appPackage': 'com.gem.tastyfood',
                # apk的launcherActivity
                'appActivity': 'com.gem.tastyfood.LaunchActivity'
                }

if __name__=='__main__':
    driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
    x = driver.get_window_size()["width"]
    y = driver.get_window_size()["height"]
    #向左滑動
    driver.swipe(x*0.9,y*0.5,x*0.1,y*0.5,duration=2000)

 

 可以將 左滑、右滑、上滑、下滑 寫成方法,方便調用:

from appium import webdriver

desired_caps = {
                'platformName': 'Android',
                'deviceName': '192.168.41.101:5555',
                'platformVersion': '9.0',
                # apk包名
                'appPackage': 'com.gem.tastyfood',
                # apk的launcherActivity
                'appActivity': 'com.gem.tastyfood.LaunchActivity'
                }

# 向左滑動。y軸保持不變,X軸:由大變小
def swipe_left(driver,star_x=0.9,stop_x=0.1,duration=2000):
    x1 = int(x*star_x)
    y1 = int(y*0.5)
    x2 = int(x*stop_x)
    y2 = int(y*0.5)
    driver.swipe(x1,y1,x2,y2,duration)

# 向右滑動。y軸保持不變,X軸:由小變大
def swipe_right(driver,star_x=0.1,stop_x=0.9,duration=2000):
    x1 = int(x*star_x)
    y1 = int(y*0.5)
    x2 = int(x*stop_x)
    y2 = int(y*0.5)
    driver.swipe(x1,y1,x2,y2,duration)

# 向上滑動。x軸保持不變,y軸:由大變小
def swipe_up(driver,star_y=0.9,stop_y=0.1,duration=2000):
    x1 = int(x*0.5)
    y1 = int(y*star_y)
    x2 = int(x*0.5)
    y2 = int(y*stop_y)
    driver.swipe(x1,y1,x2,y2,duration)

# 向下滑動。x軸保持不變,y軸:由小變大
def swipe_down(driver,star_y=0.1,stop_y=0.9,duration=2000):
    x1 = int(x*0.5)
    y1 = int(y*star_y)
    x2 = int(x*0.5)
    y2 = int(y*stop_y)
    driver.swipe(x1,y1,x2,y2,duration)


if __name__=='__main__':
    driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
    x = driver.get_window_size()["width"]
    y = driver.get_window_size()["height"]
    swipe_left(driver)
    swipe_right(driver)
    swipe_up(driver)
    swipe_down(driver)

 

 

 


免責聲明!

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



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