Python基于Appium实现多设备并行测试执行方案
本文详解如何利用Python与Appium实现多台Android设备的并行测试。核心思路是为每台设备分配独立端口并启动独立Appium服务,通过Python多进程池(Process Pool)将设备列表与测试用例绑定,实现并发执行。文章提供了从设备获取、服务启动到用例执行的完整代码逻辑与关键实现步骤。
前言:
如何实现控制多台设备并行执行测试用例?
思路篇
我们可以获取设备信息,并为每台设备开启独立的端口服务。每个服务对应特定端口,在获取设备列表时,需将设备与对应服务关联。通过建立进程池,在进程池中控制不同设备,确保每个进程独立管理指定设备即可实现并行。
实现篇
首先实现参数配置及设备端口映射。
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控制多设备并行执行的资料请关注本站其它相关文章!
免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。









