前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Appium自动化(十)如何控制多设备并行执行测试用例

Appium自动化(十)如何控制多设备并行执行测试用例

作者头像
雷子
发布2021-03-15 15:48:53
1K0
发布2021-03-15 15:48:53
举报
文章被收录于专栏:雷子说测试开发

Appium系列分享

Appium自动化(一)常用的API接口

Appium自动化(二)常用的API接口

Appium自动化(三)常用的API接口

Appium自动化(四)常用的API接口

Appium自动化(五)常用的API接口

Appium自动化(六)Appium启动app

Appium自动化(七)通过脚本自动化获取apk的包名和对应启动activity

Appium自动化(八)通过脚本自动化获取设备deviceName和platformVersion

Appium自动化(九)如何处理多设备的启动参数


前言

前面的文章呢,我们简单的去讲诉了一些api,并且我们讲了如何启动app进行测试,并且我们可以根据自动化获取我们的待测app的apkname和luanchactivity以及设备的一些参数信息, 并且我们扩充到多设备,那么问题来了,我们怎么多设备并行呢,这次给大家分享,如何做到,多设备并行。

思路篇

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

实现篇

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

代码语言:javascript
复制
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

接下来,我们去写一个端口开启服务。

代码语言:javascript
复制
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

我们开启服务了,接下来,我们怎样根据不同进程执行测试用例。

代码语言:javascript
复制
def runcase(devics):    #执行测试用例    passdef run(deviceslist:list):
    pool = Pool(len(deviceslist))    for i in deviceslist:        pool.map(runcase, i)    pool.close()    pool.join()

接下来,就是我们去组合形成最后的执行的代码。

代码展示片

代码语言:javascript
复制
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_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  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"):                    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_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)

代码没有展示测试用例的组织篇,在后续的分享中,会把测试用例的组织篇加进来。整体的思路没有那么难。

后记

上面展示的只是我对这个思路,也可以根据自己的实际的情况去寻找自己的思路。最终解决我们最后的实际问题即可。欢迎大家一起交流技术,我是雷子

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-01-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 雷子说测试开发 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档