zoukankan      html  css  js  c++  java
  • 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)
  • 相关阅读:
    Python打包方法——Pyinstaller
    在线检测显示器屏幕尺寸
    python_分布式进程中遇到的问题
    软件测试面试题(一)
    Django在根据models生成数据库表时报 __init__() missing 1 required positional argument: 'on_delete'
    mac系统 安装 IPython
    京东自动抢茅台脚本 Python
    CMake使用总结(一)
    小白安装eclipse插件—>testNG
    离线安装eclipse-testNG插件
  • 原文地址:https://www.cnblogs.com/shenh/p/11779267.html
Copyright © 2011-2022 走看看