小编给大家分享一下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): #执行测试用例 passdef run(deviceslist:list): pool = Pool(len(deviceslist)) for i in deviceslist: pool.map(runcase, i) pool.close() pool.join()
接下来,就是我们去组合形成最后的执行的代码。
最终代码展示
from appium import webdriverfrom androguard.core.bytecodes.apk import APKimport osimport randomapk_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_devicesdef 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 platformfrom multiprocessing import Process,Poolimport time,urllib.requestimport threadingclass 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"): breakdef 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_listdef runcase(devics): #执行测试用例 passdef 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控制多设备并行执行的示例”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网行业资讯频道!