from appium import webdriver import threading # 裝置一 def run_01(): desired = { "platformName": "Android", # 手機系統 "platformVersion": "7.1.2", # 手機系統版本 "deviceName": '127.0.0.1:62001', # 連線的裝置(adb devices) "automationName": "UiAutomator2", "appPackage": "", # app包名 "appActivity": "", # app的啟動頁面 "autoGrantPermissions": "true", # 預設允許app獲取相關許可權 "noReset": True # 保留登入模式 } driver_01 = webdriver.Remote(command_executor="http://127.0.0.1:4723/wd/hub", desired_capabilities=desired) # driver(driver_01) # 裝置二 def run_02(): desired = { "platformName": "Android", # 手機系統 "platformVersion": "11", # 手機系統版本 "deviceName": '90bf8faf', # 連線的裝置(adb devices) "automationName": "UiAutomator2", # 自動化測試框架 (1.4以上的appium不用寫) "appPackage": "", # app包名 "appActivity": "", # app的啟動頁面 "autoGrantPermissions": "true", # 預設允許app獲取相關許可權 "noReset": True # 保留登入模式 } driver_02 = webdriver.Remote(command_executor="http://127.0.0.1:4720/wd/hub", desired_capabilities=desired) # driver(driver_02)
不同的裝置啟動設定埠號是不一樣的,我這裡一個是4723,一個是4720.
def driver(drivers): # 元素操作方法 time.sleep(5) drivers.quit()
if __name__ == '__main__': threading.Thread(target=run_01).start() threading.Thread(target=run_02).start()
啟動指令:appium --allow-cors -p 埠號 -U 裝置名稱
我這邊是兩臺裝置,因此需要啟動兩個服務,啟動完之後就可以執行main函數了。
1)建立一個json檔案存放設定資訊
[{ "url": "http://127.0.0.1:4723/wd/hub", "desired": { "platformName": "Android", "platformVersion": "7.1.2", "deviceName": "127.0.0.1:62001", "automationName": "UiAutomator2", "appPackage": "", "appActivity": "", "autoGrantPermissions": "true", "noReset": true} }, { "url": "http://127.0.0.1:4720/wd/hub", "desired":{ "platformName": "Android", "platformVersion": "11", "deviceName": "90bf8faf", "automationName": "UiAutomator2", "appPackage": "", "appActivity": "", "autoGrantPermissions": "true", "noReset": true} }]
2)讀取設定資訊,這裡直接貼完整程式碼了。
def test_run(url, desired): driver_02 = webdriver.Remote(command_executor=url, desired_capabilities=desired) driver(driver_02) def driver(drivers): # 元素操作方法 time.sleep(5) drivers.quit() if __name__ == '__main__': with open("./appium.json", mode="r", encoding="utf-8") as f: data = list(json.load(f)) for i in range(0, len(data)): a = threading.Thread(target=test_run, args=(data[i]["url"], data[i]["desired"])) a.start()
文章來源:https://www.cnblogs.com/lihongtaoya/ ,請勿轉載