python+appium自动化测试上下左右滑动操作封装
该网站上更新文章有延迟,如需查看关于python+appium文章,请前往testhome关注我哦。https://testerhome.com/topics/27802
手机屏幕坐标显示如下:手机左上角为(0,0),横向是x轴,纵向是y轴
swipe()函数介绍:swipe(x1, y1, x2, y2 , t):(x1, y1)为滑动起始点的坐标,(x2, y2)为滑动终点坐标,t为滑动时间,单位为ms
获取手机的屏幕大小,由于每部手机的屏幕分辨率不一样,所以同一个元素在不同手机上的坐标也是不一样的,滑动的坐标不能写死,可以同通过屏幕宽和高的比例计算
1.上滑:主要操作为图中B→A,x轴坐标不变,主要为y轴坐标由大变小,由屏幕纵向长度的75%变为25%
2.下滑:主要操作为图中A→B,x轴坐标不变,主要为y轴坐标由小变大,由屏幕纵向长度的25%变为75%
3.左滑:主要操作为图中D→C,y轴坐标不变,主要为x轴左边由大变小,由屏幕横向长度的75%变为25%
4.右滑:主要操作为图中C→D,y轴坐标不变,主要为x轴左边由小变大,由屏幕横向长度的25%变为75%
滑动操作具体脚本如下:
# 滑动操作
from appium import webdriver
class Slide:
driver: webdriver = None
# 获取屏幕大小
def get_screen_size(self):
return self.driver.get_window_size(self)
# 上滑
def swipe_up(self, t):
screen_size = self.get_screen_size()
x1 = screen_size['width'] * 0.5
y1 = screen_size['height'] * 0.75
y2 = screen_size['height'] * 0.25
self.driver.swipe(x1, y1, x1, y2, t)
# 下滑
def swipe_down(self, t):
screen_size = self.get_screen_size()
x1 = screen_size['width'] * 0.5
y1 = screen_size['height'] * 0.25
y2 = screen_size['height'] * 0.75
self.driver.swipe(x1, y1, x1, y2, t)
# 左滑
def swipe_left(self, t):
screen_size = self.get_screen_size()
x1 = screen_size['width'] * 0.75
y1 = screen_size['height'] * 0.5
x2 = screen_size['width'] * 0.25
self.driver.swipe(x1, y1, x2, y1, t)
# 右滑
def swipe_right(self, t):
screen_size = self.get_screen_size()
x1 = screen_size['width'] * 0.25
y1 = screen_size['height'] * 0.5
x2 = screen_size['width'] * 0.75
self.driver.swipe(x1, y1, x2, y1, t)