咱們操做app的過程當中都會進行頁面滑動,那麼這個過程經過python怎麼實現呢?python
你們都很是的清楚咱們手動在app上是如何滑動的,而後自動化只是模仿了手工的方法去實現,咱們經過一個圖來分析web
從上往下滑動的時候,咱們一般會按(X1,Y1)這個座標,而後往下進行滑動,一直滑到(X2,Y2)這個座標app
從下往上滑動的時候,咱們會按(X2,Y2)這個座標,往上進行滑動到(X1,Y1)這個座標學習
固然了左往右進行滑動和從右往左進行滑動的思路都是同樣的了。測試
swipe是appium模塊中的一個方法,支持左右滑動和模擬點擊的功能spa
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 開始的X軸 - start_y - y-coordinate at which to start 開始的Y軸 - end_x - x-coordinate at which to stop 結束的X軸 - end_y - y-coordinate at which to stop 結束的Y軸 - duration - (optional) time to take the swipe, in ms. 持續的時間,單位毫秒 :Usage: driver.swipe(100, 100, 100, 400) """
咱們從上面的圖和swipe的源碼中知道,滑動的最主要的參數就是座標,那麼咱們如何通進行手機座標查詢呢?指針
咱們能夠進入到手機開發者模式當中,而後打開指針位置,這樣點擊哪裏就能夠看到哪裏的座標。code
咱們也能夠經過python的腳本幫咱們實現找到座標的方法orm
def get_window_size(self, windowHandle='current'): """ Gets the width and height of the current window. # 獲取屏幕的長和寬 :Usage: driver.get_window_size() ""
由於每一個手機的座標可能都不同,這裏咱們能夠經過先獲取手機屏幕的長和寬,而後再次計算須要滑動的座標位置blog
# coding:utf-8 from appium import webdriver import time desired_caps = { 'platformName': 'Android', # 測試版本 'deviceName': 'emulator-5554', # 設備名 'platformVersion': '5.1.1', # 系統版本 'appPackage': 'com.yipiao', #apk的包名 'appActivity': '.activity.LaunchActivity', # apk的launcherActivity } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) # 獲取設備的寬度 x=driver.get_window_size()['width'] # 獲取設備的長度 y=driver.get_window_size()['height'] print(x) print(y)
咱們經過智行火車票APP來實戰操做下左右滑動,由於咱們每一個手機的頁面不同,能夠經過算出來一個大概值進行匹配
# coding:utf-8 from appium import webdriver import time desired_caps = { 'platformName': 'Android', # 測試版本 'deviceName': 'emulator-5554', # 設備名 'platformVersion': '5.1.1', # 系統版本 'appPackage': 'com.yipiao', #apk的包名 'appActivity': '.activity.LaunchActivity', # apk的launcherActivity "noReset": True, # 不清空數據 } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) time.sleep(6) driver.find_element_by_xpath('//*[@text="下次再說"]').click() l = driver.get_window_size() x1 = l['width'] * 0.75 y1 = l['height'] * 0.5 x2 = l['width'] * 0.25 # 向左滑動 driver.swipe(x1,y1,x2,y1,500) print('已經向左滑動了')
在這裏安靜就不一個個爲你們演示了,小夥伴們能夠本身嘗試下
咱們能夠把滑動方法一個個封裝起來,這樣咱們作app自動化測試的過程須要用到的時候就能夠直接調用。
def swipeUp(driver, t=500, n=1): '''向上滑動屏幕''' l = driver.get_window_size() x1 = l['width'] * 0.5 y1 = l['height'] * 0.75 y2 = l['height'] * 0.25 for i in range(n): driver.swipe(x1, y1, x1, y2, t) def swipeDown(driver, t=500, n=1): '''向下滑動屏幕''' l = driver.get_window_size() x1 = l['width'] * 0.5 y1 = l['height'] * 0.25 y2 = l['height'] * 0.75 for i in range(n): driver.swipe(x1, y1, x1, y2,t) def swipLeft(driver, t=500, n=1): '''向左滑動屏幕''' l = driver.get_window_size() x1 = l['width'] * 0.75 y1 = l['height'] * 0.5 x2 = l['width'] * 0.25 for i in range(n): driver.swipe(x1, y1, x2, y1, t) def swipRight(driver, t=500, n=1): '''向右滑動屏幕''' l = driver.get_window_size() x1 = l['width'] * 0.25 y1 = l['height'] * 0.5 x2 = l['width'] * 0.75 for i in range(n): driver.swipe(x1, y1, x2, y1, t)
感受寫的對您有幫助的話,點個關注,持續更新~~國慶快樂~~放假了也不要忘記學習哦