滑动操作(swipe)
在Appium中模拟用户滑动操作需要使用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 - x-coordinate at which to start
- start_y - y-coordinate at which to start
- end_x - x-coordinate at which to stop
- end_y - y-coordinate at which to stop
- duration - (optional) time to take the swipe, in ms.
屏幕坐标:
原点坐标位于屏幕的左上角,x轴向右逐渐增大,y轴向下变大
案例
此案例直接时滑动屏幕,不对任何app操作,所以未设置appPacage等参数
from appium import webdriver
from time import sleep
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1.1'
desired_caps['deviceName'] = '127.0.0.1:21503'
webdr = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)
webdr.implicitly_wait(10)
# 封装获取屏幕大小的方法
screen_size= webdr.get_window_size() # 获取屏幕大小,返回结果为字典类型
# 封装向左滑动的方法
def swipeLeft():
x1 = int(screen_size['width']*0.9)
y = int(screen_size['height']*0.5)
x2 = int(screen_size['width']*0.1)
webdr.swipe(x1,y,x2,y,1000)
# 向上滑动
def swipeUp():
x = int(screen_size['width']*0.5)
y1 = int(screen_size['height']*0.9)
webdr.swipe(x,y1,x,0)
# 向下滑动
def swipeDown():
x = int(screen_size['width']*0.5)
y2 = int(screen_size["height"]*0.9)
webdr.swipe(x,0,x,y2)
# 向右滑动
def swipRight():
x1 = int(screen_size['width']*0.2)
x2 = int(screen_size['width']*0.9)
y = int(screen_size['height']*0.5)
webdr.swipe(x1,y,x2,y)
# 向左滑动两次
for i in range(2):
swipeLef