python 基于Appium控制多设备并行执行
作者:北漂的雷子 时间:2022-12-11 12:00:06
前言:
如何做到,控制多设备并行执行测试用例呢。
思路篇
我们去想下,我们可以获取参数的信息,和设备的信息,那么我们也可以针对每台设备开启不一样的端口服务。那么每个服务都对应的端口,我们在获取设备列表的时候,要和 每个服务对应起来,这样,我们开启一个进城池,我们在进程池里去控制设备,每个进程池 控制不一样的设备即可。
实现篇
首先实现对应的参数篇和对应的设备端口,
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)
来源:https://www.cnblogs.com/leiziv5/p/14225882.html
标签:python,Appium,并行执行
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SqlServer修改数据库文件及日志文件存放位置
2024-01-15 15:17:22
python判断字符串的前两个字母是否是"id"的示例代码
2021-05-02 20:39:12
Python dict的常用方法示例代码
2023-05-17 09:58:15
推荐技术人员一款Python开源库(造数据神器)
2023-11-10 19:57:45
![](https://img.aspxhome.com/file/2023/6/93716_0s.jpg)
MySQL Order By语法介绍
2024-01-19 03:29:48
python中的bisect模块与二分查找详情
2021-07-23 05:17:56
asp数据转换函数示例
2008-04-13 06:52:00
python通过http上传文件思路详解
2022-02-08 12:48:30
![](https://img.aspxhome.com/file/2023/9/79419_0s.png)
没编程基础可以学python吗
2023-11-27 23:12:49
PHP中array_slice函数用法实例详解
2023-06-20 20:18:04
JavaScript中尽量用局部变量的原因[译]
2009-02-20 13:45:00
![](https://img.aspxhome.com/file/UploadPic/20092/20/01-88s.jpg)
用python写asp详细讲解
2022-02-26 13:21:36
![](https://img.aspxhome.com/file/2023/5/78535_0s.jpg)
关于JavaScript中高阶函数的魅力详解
2024-04-18 09:29:40
django中静态文件配置static的方法
2022-07-29 08:52:51
5分钟快速掌握Python定时任务框架的实现
2023-05-31 10:19:45
![](https://img.aspxhome.com/file/2023/6/110196_0s.png)
Django 多语言教程的实现(i18n)
2022-12-18 22:41:39
![](https://img.aspxhome.com/file/2023/1/98811_0s.jpg)
简单的Python调度器Schedule详解
2021-09-15 09:49:19
详解vue页面首次加载缓慢原因及解决方案
2024-05-25 15:19:45
![](https://img.aspxhome.com/file/2023/6/123706_0s.png)
asp如何修改WINNT的登录密码?
2010-06-10 17:06:00
Python操作PDF实现制作数据报告
2022-05-09 21:41:51
![](https://img.aspxhome.com/file/2023/9/121069_0s.png)