• 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)
  • 相关阅读:
    如何复制百度文库中的文章转的,不用担心下载要币了[转]
    什么是中间件(转)
    android实用代码 (转)
    [Java]读取文件方法大全(转)
    Solaris下查看磁盘、内存、CPU使用程度
    Gene Ontology (GO) 简介
    如何在网上查某个基因的转录因子及启动子
    kmeans k均值聚类的弱点/缺点
    层次聚类
    什么是非负矩阵分解 NMF(Nonnegative Matrix Factorization )
  • 原文地址:https://www.cnblogs.com/shenh/p/11779267.html
Copyright © 2020-2023  润新知