python 基于Appium控制多設(shè)備并行執(zhí)行
前言:
如何做到,控制多設(shè)備并行執(zhí)行測試用例呢。
思路篇
我們?nèi)ハ胂?,我們可以獲取參數(shù)的信息,和設(shè)備的信息,那么我們也可以針對每臺設(shè)備開啟不一樣的端口服務(wù)。那么每個服務(wù)都對應(yīng)的端口,我們在獲取設(shè)備列表的時候,要和 每個服務(wù)對應(yīng)起來,這樣,我們開啟一個進(jìn)城池,我們在進(jìn)程池里去控制設(shè)備,每個進(jìn)程池 控制不一樣的設(shè)備即可。
實(shí)現(xiàn)篇
首先實(shí)現(xiàn)對應(yīng)的參數(shù)篇和對應(yīng)的設(shè)備端口,
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
接下來,我們?nèi)?#8203;寫一個端口開啟服務(wù)。
class RunServer(threading.Thread):#啟動服務(wù)的線程 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
我們開啟服務(wù)了,接下來,我們怎樣根據(jù)不同進(jìn)程執(zhí)行測試用例。
def runcase(devics): #執(zhí)行測試用例 pass def run(deviceslist:list): pool = Pool(len(deviceslist)) for i in deviceslist: pool.map(runcase, i) pool.close() pool.join()
接下來,就是我們?nèi)ソM合形成最后的執(zhí)行的代碼。
最終代碼展示
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):#啟動服務(wù)的線程 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): #執(zhí)行測試用例 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控制多設(shè)備并行執(zhí)行的詳細(xì)內(nèi)容,更多關(guān)于Appium控制多設(shè)備并行執(zhí)行的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python實(shí)現(xiàn)滑塊拼圖驗(yàn)證碼詳解
驗(yàn)證碼作為一種自然人的機(jī)器人的判別工具,被廣泛的用于各種防止程序做自動化的場景中。傳統(tǒng)的字符型驗(yàn)證安全性已經(jīng)名存實(shí)亡的情況下,各種新型的驗(yàn)證碼如雨后春筍般涌現(xiàn),今天給大家分享一篇Python實(shí)現(xiàn)滑塊驗(yàn)證碼2022-05-05Python隨機(jī)生成一個6位的驗(yàn)證碼代碼分享
這篇文章主要介紹了Python隨機(jī)生成一個6位的驗(yàn)證碼代碼分享,本文直接給出代碼實(shí)例,需要的朋友可以參考下2015-03-03anaconda虛擬環(huán)境默認(rèn)路徑的更改圖文教程
在Anaconda中如果沒有指定路徑,虛擬環(huán)境會默認(rèn)安裝在anaconda所安裝的目錄下,這篇文章主要給大家介紹了關(guān)于anaconda虛擬環(huán)境默認(rèn)路徑更改的相關(guān)資料,需要的朋友可以參考下2023-10-10pytorch簡單實(shí)現(xiàn)神經(jīng)網(wǎng)絡(luò)功能
這篇文章主要介紹了pytorch簡單實(shí)現(xiàn)神經(jīng)網(wǎng)絡(luò),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-09-09使用python 寫一個靜態(tài)服務(wù)(實(shí)戰(zhàn))
今天小編就為大家分享一篇使用python 寫一個靜態(tài)服務(wù)(實(shí)戰(zhàn)),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06python web框架Flask實(shí)現(xiàn)圖形驗(yàn)證碼及驗(yàn)證碼的動態(tài)刷新實(shí)例
在本篇文章里小編給大家整理的是關(guān)于python web框架Flask實(shí)現(xiàn)圖形驗(yàn)證碼的相關(guān)知識點(diǎn),有需要的朋友們參考下。2019-10-10