Appium-屏幕滑动操作
应用背景:
在app日常使用的过程中,我们常常会对屏幕、界面进行滑动的操作,例如首次进入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)
滑动解析:
1.A->B:水平滑动
2.C->D: 垂直滑动
3.任意方向的滑动
获取坐标:
1.打开手机开发者模式的指针位置
2.查看uiautomatorviewer.bat的位置信息
3.python获取屏幕的长和宽
实践应用:
#获取屏幕的尺寸
def get_size(self): #获取屏幕尺寸 x=self.get_window_size()['width'] y=self.get_window_size()['height'] return x, y#向左滑动
#向左滑动
def swipeLeft(self): logging.info('swipeLeft') l=self.get_size() x1 = int(l[0] * 0.95) y1 = int(l[1] * 0.5) x2 = int(l[0] * 0.25) self.swipe(x1, y1, x2, y1, 1000)
#向左滑动2次
for i in range(2):
swipeLeft()
sleep(0.5)
#向上滑动
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)
只是简单举个例子,在实际使用中把方法封装到公共类,然后在需要的业务模块中再进行调用即可,持续学习中