Appium移動端自動化測試之滑動封裝實戰(八)


desired_caps = { 'platformName': 'Android', 'platformVersion': '5.0.0.0', 'deviceName': '127.0.0.1:62001', 'appPackage': 'com.shanjian.originaldesign', 'appActivity':'.activity.other.Activity_In' } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps) #登錄 sleep(5) driver.find_element_by_id("com.shanjian.originaldesign:id/edit_Tel").clear() driver.find_element_by_android_uiautomator('new UiSelector().text("輸入手機號碼")').send_keys("15817252876") driver.find_element_by_id("com.shanjian.originaldesign:id/edit_Pwd").send_keys("123456") driver.find_element_by_android_uiautomator('new UiSelector().text("登錄")').click() sleep(5) driver.find_element_by_name("我的").click() sleep(5) print('開始向上滑動....') def swipeUp(t=1000,n=1): '''封裝向上滑動方法''' l = driver.get_window_size() print(l) #dict{'width': 720, 'height': 1280} x1 = l['width'] *  0.5 #獲取寬度x1 y1 = l['height'] * 0.75 #起始值y1 y2 = l['height'] * 0.25 #結束值y2 for  i  in range(n): driver.swipe(x1,y1,x1,y2,t) swipeUp(3000) sleep(3) print('開始向下滑動...') def swipeDown(t=None,n=1): '''向下滑動方法''' l = driver.get_window_size() print(l) # dict{'width': 720, 'height': 1280} x1 = l['width'] *  0.5 # 獲取寬度x1 y1 = l['height'] * 0.25 # 起始值y1 y2 = l['height'] * 0.75 # 結束值y2 for i in range(n): driver.swipe(x1, y1, x1, y2,t) swipeUp(3000) def swipeLeft(t=None,n=1): '''向左滑動方法''' l = driver.get_window_size() print(l) # dict{'width': 720, 'height': 1280} x1 = l['width'] *  0.75 # 獲取寬度x1 y1 = l['height'] * 0.5 # 起始值y1 x2 = l['height'] * 0.25 # 結束值y2 for i in range(n): driver.swipe(x1, y1, x2, y1,t) def swipeDown(t=None,n=1): '''向右滑動方法''' l = driver.get_window_size() print(l) # dict{'width': 720, 'height': 1280} x1 = l['width']  * 0.25 # 獲取寬度x1 y1 = l['height'] * 0.5 # 起始值y1 x2 = l['height'] * 0.75 # 結束值y2 for i in range(n): driver.swipe(x1, y1, x2, y1,t)

完整源碼:

''' :Args: - start_x - 開始滑動的 x 坐標 - start_y - 開始滑動的 y 坐標 - end_x  - 結束點 x 坐標 - end_y - 結束點 y 坐標 - duration - 持續時間,單位毫秒 :Usage: driver.swipe(100, 100, 100, 400) ''' 
from appium.webdriver.support import expected_conditions as EC from appium.webdriver.support.ui import WebDriverWait from time import sleep from appium import webdriver desired_caps = { 'platformName': 'Android', 'deviceName': '30d4e606', 'platformVersion': '4.4.2', 'appPackage': 'com.taobao.taobao', 'appActivity': 'com.taobao.tao.welcome.Welcome' } ''' 參數 1:driver 參數 2:t 是持續時間 參數 3:滑動次數 '''  driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps) sleep(8) def swipeUp(driver, t=500, n=1): '''向上滑動屏幕''' l = driver.get_window_size() x1 = l['width'] *  0.5 # x 坐標 y1 = l['height'] * 0.75 # 起始 y 坐標 y2 = l['height'] * 0.25 # 終點 y 坐標 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 # x 坐標 y1 = l['height'] * 0.25 # 起始 y 坐標 y2 = l['height'] * 0.75 # 終點 y 坐標 for i in range(n): driver.swipe(x1, y1, x1, y2, 1) 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.05
    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.05 y1 = l['height'] * 0.5 x2 = l['width']  * 0.75
    for i in range(n): driver.swipe(x1, y1, x2, y1, t) if __name__ == "__main__": print(driver.get_window_size()) sleep(8) swipeUp(driver,n=1) sleep(8) swipeDown(driver,n=1)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM