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)
只是簡單舉個例子,在實際使用中把方法封裝到公共類,然后在需要的業務模塊中再進行調用即可,持續學習中