在的自动化测试过程中不可能只测试一个固定的有限界面,有的时候会遇到一些比较长的页面,这个时候就会用到滑动的操作,在 appium 中模拟用户滑动的操作需要使用 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 - 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.
:Usage:
driver.swipe(100, 100, 100, 400)
*/
1、Swipe 的分类
- 水平滑动
- 垂直滑动
- 任意方向滑动
滑动的轨迹图有:
2、综合案例:
-
测试场景
- 启动 豌豆荚,进入到 精选 界面
- 水平手动向左滑动2次
-
执行脚本
from capability import driver
import time
# 先获取屏幕的尺寸
def get_screen_size():
x = driver.get_window_size()['width']
y = driver.get_window_size()['height']
# 直接返回一个元组
return x,y
# 向左滑动
def swipe_left():
left = get_screen_size()
x1 = int(left[0] * 0.9)
y1 = int(left[1] * 0.6)
x2 = int(left[0] * 0.2)
driver.swipe(x1, y1, x2, y1, 3000)
# 向右滑动
def swipe_right():
right = get_screen_size()
x1 = int(right[0] * 0.2)
y1 = int(right[1] * 0.6)
x2 = int(right[0] * 0.9)
driver.swipe(x1, y1, x2, y1, 3000)
# 向上滑动
def swipe_up():
up = get_screen_size()
x1 = int(up[0] * 0.5)
y1 = int(up[1] * 0.9)
y2 = int(up[0] * 0.3)
driver.swipe(x1, y1, x1, y2, 3000)
# 向下滑动
def swipe_down():
down = get_screen_size()
x1 = int(down[0] * 0.5)
y1 = int(down[0] * 0.3)
y2 = int(down[1] * 0.9)
driver.swi