在app应用日常使用过程中,会经常用到在屏幕滑动操作。如刷朋友圈上下滑操作、浏览图片左右滑动操作等。在自动化脚本该如何实现这些操作呢?
在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.
:Usage:
driver.swipe(100, 100, 100, 400)
滑动主要分为:
滑动轨迹图如下:
swipe.py
from time import sleep
from find_element.capability import driver
#获取屏幕尺寸
def get_size():
x=driver.get_window_size()['width']
y=driver.get_window_size()['height']
return x,y
#显示屏幕尺寸(width,height)
l=get_size()
print(l)
#向左滑动
def swipeLeft():
l=get_size()
x1=int(l[0]*0.9)
y1=int(l[1]*0.5)
x2=int(l[0]*0.1)
driver.swipe(x1,y1,x2,y1,1000)
if __name__ == '__main__':
for i in range(2):
swipeLeft()
sleep(0.5)
driver.find_element_by_id('com.tal.kaoyan:id/activity_splash_guidfinish').click()
把垂直上下滑动以及向右滑动的也封装并实践。
def swipeUp():
l = get_size()
x1 = int(l[0] * 0.5)
y1 = int(l[1] * 0.95)
y2 = int(l[1] * 0.35)
driver.swipe(x1, y1, x1, y2, 1000)
def swipeDown():
l=get_size()
x1 = int(l[0] * 0.5)
y1 = int(l[1] * 0.35)
y2 = int(l[1] * 0.85)
driver.swipe(x1, y1, x1, y2, 1000)
def swipeRight():
l=get_size()
y1 = int(l[1] * 0.5)
x1 = int(l[0] * 0.25)
x2 = int(l[0] * 0.95)
driver.swipe(x1, y1, x2, y1, 1000)