前言:

    如何做到,控制多设备并行执行测试用例呢。


思路篇

  我们去想下,我们可以获取参数的信息,和设备的信息,那么​我们也可以针对每台设备开启不一样的端口服务。那么每个服务都对应的端口,我们在获取设备列表的时候,要和 每个服务对应起来,这样,我们开启一个进城池,我们在进程池里去控制设备,​每个进程池 控制不一样的设备即可。


实现篇

  首先实现对应的参数篇和对应的设备端口,

    

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)

    文章首发在雷子说测试开发公众号

      

  

Appium自动化如何控制多设备并行执行的更多相关文章

  1. Appium自动化(10) - appium高级元素定位方式之 UI Automator API 的详解

    如果你还想从头学起Appium,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1693896.html 前言 前面介绍过根据id,clas ...

  2. appium自动化测试之UIautomatorviewer元素定位

    appium自动化测试之UIautomatorviewer元素定位 标签(空格分隔): uiautomatorviewer元素定位 前面的章节,已经总结了怎么搭建环境,怎样成功启动一个APP了,这里具 ...

  3. python3+Appium自动化02-Capability配置

    基本参数 参数 描述 实例 automationName 自动化测试引擎 Appium或 Selendroid platformName 手机操作系统 iOS, Android, 或 FirefoxO ...

  4. appium自动化的工作原理(1)

    用appium开发移动端自动化测试脚本这么长时间,还没有认证的了解下它的原理是什么,到底是如何实现的呢? 1.先看一个Appium加载的过程图解(来自:了解appium自动化的工作原理--https: ...

  5. 电脑控制Android设备的软件——Total Control

    最早开始搞Android开发时,为了调试方便,想找一个Android下的远程控制软件,支持在电脑端远程控制和同步显示Android设备.先后试了360手机助手.Mobizen.Vysor和Mirror ...

  6. 截取usb数据包,控制usb设备----Relay设备

    在项目开发当中,我们需要一个usb转继电器的设备当开关控制无线发射设备,采购部采购时并未详细了解Relay设备的运行环境就买了一批设备,之后发现设备厂家只提供了windows库,而我们是要在linux ...

  7. Appium同时运行多个设备

    为了提高测试效率,测试需要同时在多个android设备上运行,就需要启动多个appium. 启动appium时,为每个设备设置不同的端口号,并为driver设置该设备的udid.见如下实例,关键是红色 ...

  8. 安天透过北美DDoS事件解读IoT设备安全——Mirai的主要感染对象是linux物联网设备,包括:路由器、网络摄像头、DVR设备,入侵主要通过telnet端口进行流行密码档暴力破解,或默认密码登陆,下载DDoS功能的bot,运行控制物联网设备

    安天透过北美DDoS事件解读IoT设备安全 安天安全研究与应急处理中心(安天CERT)在北京时间10月22日下午启动高等级分析流程,针对美国东海岸DNS服务商Dyn遭遇DDoS攻击事件进行了跟进分析. ...

  9. Appium自动化部署及连接Appium服务

    Appium自动化部署: 1)安装appium桌面程序安装:超链接 2)安装客户端 pip install appium-python-client 3)安装服务器 安装 Nodejs 4)连接app ...

随机推荐

  1. 第8.33节 Python中__getattr__以及__getattr__与__ getattribute__的关系深入剖析

    一. 引言 前面几节分别介绍了Python中属性操作捕获的三剑客:__ getattribute__方法.__setattr__方法.__delattr__方法,为什么__ getattribute_ ...

  2. PyQt(Python+Qt)学习随笔:QListWidget获取指定项对应行的row方法

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QListWidget的row方法通过项作为参数,获取到对应项所在行的行号,语法如下: int ro ...

  3. PyQt(Python+Qt)学习随笔:Designer(设计师)中部件属性编辑的cursor(光标样式)属性

    部件(又称为组件或控件)的cursor属性保存该部件的鼠标光标形状,当鼠标位于该部件上时就会呈现该属性设置的光标形状,对应类型为枚举类型Qt.CursorShape,可取值的范围可以在Qt文档官网:h ...

  4. Java基础学习之流程控制语句(5)

    目录 1.顺序结构 2.选择结构 2.1.if else结构 2.2.switch case结构 3.循环结构 3.1.while结构 3.2.do while结构 3.3.for结构 3.3.1.普 ...

  5. idea2020.2.x/2020.3.x最新破解版方法教程无限永久重置插件激活码

    idea是一个java开发工件,相信我所有的朋友都用过.本教程教你做到完美,安全,永久.破解 idea2020.2.x和idea2020.3.x的所有版本绝对是100% 激活,支持Windows Ma ...

  6. SpringBoot 拦截器和自定义注解判断请求是否合法

    应用场景举例: 当不同身份的用户请求一个接口时,用来校验用户某些身份,这样可以对单个字段数据进行精确权限控制,具体看代码注释 自定义注解 /** * 对比请求的用户身份是否符合 * @author l ...

  7. RMAN迁移数据库(不改变文件目录)

    1.目标库创建相应目录mkdir -p /u01/app/oracle/oradata/orclmkdir -p /u01/app/oracle/fast_recovery_area/ORCLmkdi ...

  8. collectd+infludb+grafana实现tomcat JVM监控

    前提条件:已安装好java环境,tomcat,influxdb和collectd.本文暂不提供以上内容的安装步骤 系统环境:centos7 原理:开启tomcat的jmx端口,使用collectd的c ...

  9. 【JVM专题】JVM从概述到调优图文详解,含思维脑图深度剖析!

    JVM概述 JVM 是一种用于计算机设备的规范,它是一个虚构的计算机的软件实现,简单的说,JVM 是运行 byte code 字节码程序的一个容器. 它有一个解释器组件,可以实现 JAVA 字节码和计 ...

  10. Docker(四):Docker安装Redis

    查找Redis镜像 镜像仓库 https://hub.docker.com/ 下拉镜像 docker pull redis 查看镜像 docker images 创建Redis容器 运行Redis镜像 ...