在Appium中提供了三种滑动的方式,swipe滑动scroll滑动drag拖拽事件
除了这三种滑动方式外,我们还可以自定义一些滑动方式。
下面我们来看看这三种滑动方式。

(1)swipe滑动

从⼀个坐标位置滑动到另⼀个坐标位置,只能是两个点之间的滑动。
swipe()方法说明:

swipe(start_x, start_y, end_x, end_y, duration=None)

参数:
1.start_x:起点X轴坐标
2.start_y:起点Y轴坐标
3.end_x: 终点X轴坐标
4.end_y,: 终点Y轴坐标
5.duration : 滑动这个操作⼀共持续的时间⻓度(也可以称惯性),单位:ms(毫秒)
    (重点)什么是惯性,就是在上拉下滑的过程中,滑动动作结束后屏幕会还会滑一段距离,这个现象就是惯性。
    duration时间越短,duration=None,这样惯性滑动的距离会非常的多。
    duration时间越长,滑动这个动作越缓慢,惯性滑动的距离会变少。

业务场景:

  1. 进⼊设置
  2. 从坐标(148,659)滑动到坐标(148,248)

说明:
在一个手机界面上,左上角的坐标是(0,0)点。
横向是x轴坐标,纵向是y轴坐标。
在设置APP中,左右滑动无效,只能上下滑动,
所以只有y坐标在变化。
y坐标从小到大,是向下滑动(手势,而不以屏幕做参考),
y坐标从大到小,是向上滑动手势,而不以屏幕做参考)。

注意:
可以通过 driver.get_window_size()命令来获取手机屏幕大小。
坐标的选择不能在屏幕的坐标之外,
也不能选择在边界值上,要在屏幕坐标之内的范围选择。

练习:

"""
1.学习目标
    掌握swipe滑动方法使用
2.操作步骤
    swipe滑动方法,从一个坐标滑动到另一个坐标
    driver.swipe(start_x,start_y,end_x,end_y,duration=None)
3.需求
    在设置APP首页实现swipe方法滑动
    使用swipe方法在设置首页,实现向上滑动
        (x坐标不变,y坐标从大到小)
4.总结
    swipe()滑动说明
        操作对象  :  坐标
        操作过程有惯性有惯性
            消除惯性,添加duration值,
            当值越大,惯性越小,无限接近坐标差
"""

# 1.导入appium
import time
from appium import webdriver

# 2.创建Desired capabilities对象,添加启动参数
desired_caps = {
    "platformName": "Android",  # 系统名称
    "platformVersion": "7.1.2",  # 系统版本
    "deviceName": "127.0.0.1:21503",  # 设备名称
    "appPackage": "com.android.settings",  # APP包名
    "appActivity": ".Settings"  # APP启动名
}

# 3.启动APP
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)

# 获取屏幕大小
size = driver.get_window_size()
print("设备屏幕大小:", size)
# 设备屏幕大小: {'width': 810, 'height': 1440}

# 4.操作APP
# 使用swipe方法在设置首页,实现向上滑动
# x坐标不变,y坐标从大到小
# start_x = 0  # 不能选择屏幕的大小的边界值,否则会报错
# start_y = 1440  # 不能选择屏幕的大小的边界值,否则会报错
# end_x = 0
# end_y = 400

start_x = 300
start_y = 1000
end_x = 300
end_y = 500
driver.swipe(start_x, start_y, end_x, end_y, duration=5000)


# 6.关闭APP
time.sleep(3)
driver.quit()

(2)scroll滑动

从⼀个元素滑动到另⼀个元素,直到页⾯⾃动停⽌
scroll()方法说明:

scroll(origin_el, destination_el, duration)

参数
1.origin_el :滑动开始的元素
2.destination_el :滑动结束的元素
3.duration : 滑动效果的持续时间  单位ms(毫秒)

业务场景:

进⼊设置
模拟⼿指从存储菜单位置到WLAN菜单位置的上滑操作

代码实现:

# 定位到存储菜单栏
el1 = driver.find_element_by_xpath("//*[contains(@text,'存储')]")

# 定位到WLAN菜单栏
el2 = driver.find_element_by_xpath("//*[contains(@text,'WLAN')]")

# 执⾏滑动操作
driver.scroll(el1,el2)

练习:

"""
1.学习目标
    掌握scroll滑动方法(熟悉即可)
2.操作步骤
    scroll滑动   从一个元素滑动到另一个元素
    driver.scroll(origin_el,destination_el,duration=None)
        origin_el         起始元素
        destination_el    终止元素
        duration          滑动效果的持续时间  单位ms(毫秒)
3.需求
    实现scroll方法
    在设置首页从”存储“滑动到“蓝牙“

4. 总结
    scroll方法
        操作对象 :  元素
        操作过程有惯性,需要添加duration参数,
            参数值越大,惯性越小。
"""
# 1.导入appium
import time
from appium import webdriver

# 2.创建Desired capabilities对象,添加启动参数
desired_caps = {
    "platformName": "Android",  # 系统名称
    "platformVersion": "7.1.2",  # 系统版本
    "deviceName": "127.0.0.1:21503",  # 设备名称
    "appPackage": "com.android.settings",  # APP包名
    "appActivity": ".Settings"  # APP启动名
}

# 3.启动APP
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)

# 4.操作APP
# 定位”存储“和”蓝牙“
store = driver.find_element_by_android_uiautomator('new UiSelector().text("存储")')
blue_tooth = driver.find_element_by_android_uiautomator('new UiSelector().text("蓝牙")')

# 从”存储“滑动到”蓝牙“
driver.scroll(store, blue_tooth, duration=5000)

# 实际操作手机是手指在屏幕向上滑动,
# 点击存储的位置,向上滑动到蓝牙的位置,来查看页面中下方的内容。

# 6.关闭APP
time.sleep(3)
driver.quit()