python 基於Appium控制多設備並行執行
前言:
如何做到,控制多設備並行執行測試用例呢。
思路篇
我們去想下,我們可以獲取參數的信息,和設備的信息,那麼我們也可以針對每臺設備開啟不一樣的端口服務。那麼每個服務都對應的端口,我們在獲取設備列表的時候,要和 每個服務對應起來,這樣,我們開啟一個進城池,我們在進程池裡去控制設備,每個進程池 控制不一樣的設備即可。
實現篇
首先實現對應的參數篇和對應的設備端口,
def startdevicesApp(): l_devices_list=[] port_list=[] alldevices=get_devices() if len(alldevices)>0: for item in alldevices: port=random.randint(1000,6000) port_list.append(port) desired_caps = { 'platformName': 'Android', 'deviceName': item, 'platformVersion': getPlatForm(item), 'appPackage': get_apkname(apk_path), # 包名 'appActivity': get_apk_lautc(apk_path), # apk的launcherActivity 'skipServerInstallation': True, "port":port } l_devices_list.append(desired_caps) return l_devices_list,port_list
接下來,我們去寫一個端口開啟服務。
class RunServer(threading.Thread):#啟動服務的線程 def __init__(self, cmd): threading.Thread.__init__(self) self.cmd = cmd def run(self): os.system(self.cmd) def start(port_list:list): def __run(url): time.sleep(10) response = urllib.request.urlopen(url, timeout=5) if str(response.getcode()).startswith("2"): return True for i in range(0, len(port_list)): cmd = "appium -p %s " % ( port_list[i]) if platform.system() == "Windows": # windows下啟動server t1 =RunServer(cmd) p = Process(target=t1.start()) p.start() while True: time.sleep(4) if __run("http://127.0.0.1:" + port_list[i]+ "/wd/hub/status"): break
我們開啟服務瞭,接下來,我們怎樣根據不同進程執行測試用例。
def runcase(devics): #執行測試用例 pass def run(deviceslist:list): pool = Pool(len(deviceslist)) for i in deviceslist: pool.map(runcase, i) pool.close() pool.join()
接下來,就是我們去組合形成最後的執行的代碼。
最終代碼展示
from appium import webdriver from androguard.core.bytecodes.apk import APK import os import random apk_path = "/Users/lileilei/Downloads/com.tencent.mobileqq_8.5.0_1596.apk" def get_devices() -> list: all_devices = [] cmd = "adb devices" reslut = os.popen(cmd).readlines()[1:] for item in reslut: if item != "\n": all_devices.append(str(item).split("\t")[0]) return all_devices def getPlatForm(dev: str) -> str: cmd = 'adb -s {} shell getprop ro.build.version.release'.format(dev) reslut = os.popen(cmd).readlines()[0] return str(reslut).split("\n")[0] def get_apkname(apk): a = APK(apk, False, "r") return a.get_package() def get_apk_lautc(apk): a = APK(apk, False, "r") return a.get_main_activity() import platform from multiprocessing import Process,Pool import time,urllib.request import threading class RunServer(threading.Thread):#啟動服務的線程 def __init__(self, cmd): threading.Thread.__init__(self) self.cmd = cmd def run(self): os.system(self.cmd) def start(port_list:list): def __run(url): time.sleep(10) response = urllib.request.urlopen(url, timeout=5) if str(response.getcode()).startswith("2"): return True for i in range(0, len(port_list)): cmd = "appium -p %s " % ( port_list[i]) if platform.system() == "Windows": # windows下啟動server t1 =RunServer(cmd) p = Process(target=t1.start()) p.start() while True: time.sleep(4) if __run("http://127.0.0.1:" + port_list[i]+ "/wd/hub/status"): break def startdevicesApp(): l_devices_list=[] port_list=[] alldevices=get_devices() if len(alldevices)>0: for item in alldevices: port=random.randint(1000,6000) port_list.append(port) desired_caps = { 'platformName': 'Android', 'deviceName': item, 'platformVersion': getPlatForm(item), 'appPackage': get_apkname(apk_path), # 包名 'appActivity': get_apk_lautc(apk_path), # apk的launcherActivity 'skipServerInstallation': True, "port":port } l_devices_list.append(desired_caps) return l_devices_list,port_list def runcase(devics): #執行測試用例 pass def run(deviceslist:list): pool = Pool(len(deviceslist)) for devices in deviceslist: pool.map(runcase, devices) pool.close() pool.join() if __name__=="__main__": l_devices_list,port_list=startdevicesApp() start(port_list) run(l_devices_list)
以上就是python 基於Appium控制多設備並行執行的詳細內容,更多關於Appium控制多設備並行執行的資料請關註WalkonNet其它相關文章!
推薦閱讀:
- pytest多線程與多設備並發appium
- python利用Appium實現自動控制移動設備並提取數據功能
- 詳解appium自動化測試工具(monitor、uiautomatorviewer)
- Appium的使用與入門(這款神器你值得擁有)
- Python+Appium自動化測試的實戰