python+Appium自动化:python多线程多并发启动appium服务

Python启动Appium 服务

使用Dos命令或者bat批处理来手动启动appium服务,启动效率低下。如何将启动Appium服务也实现自动化呢?

这里需要使用subprocess模块,该模块可以创建新的进程,并且连接到进程的输入、输出、错误等管道信息,并且可以获取进程的返回值。

场景

使用Python启动2台appium服务,端口配置如下:

  • Appium服务器端口:4723,bp端口为4724
  • Appium服务器端口:4725,bp端口为4726

说明:bp端口( --bootstrap-port)是appium和设备之间通信的端口,如果不指定到时无法操作多台设备运行脚本

工程文件中新建一个文件夹

新建multi_subprocess.py

python 复制代码
# -*- coding: utf-8 -*-#

import subprocess
from time import ctime

def appium_start(host,port):
    # bp端口( --bootstrap-port)是appium和设备之间通信的端口
    bootstrap_port = str(port + 1)
    cmd = 'start /b appium -a ' + host + ' -p '+ str(port) + ' -bp ' + str(bootstrap_port)

    print('%s at %s' % (cmd, ctime()))
    subprocess.Popen(cmd, shell=True, stdout=open('../appiumlog/'+str(port)+'.log', 'a'), stderr=subprocess.STDOUT)

if __name__ == '__main__':
    host = '127.0.0.1'
    port = 4723
    appium_start(host,port)

效验是否成功 :

1.cmd中输入netstat -ano | findstr 端口号(4723)

  1. 在appiumlog路径中生成4723.log日志文件

终止appium服务

taskkill -f -pid appium进程,如下:

多个appium服务启动

python 复制代码
if __name__ == '__main__':
    host = '127.0.0.1'
    for i in range(2):
        port=4723+2*i
        appium_start(host,port)

多进程并发启动appium服务

上面还不是并发执行启动appium,因此需要使用多进程来实现并发启动。

同样需要引入multiprocessing多进程模块。

muti_appium_sync.py

python 复制代码
import multiprocessing
import subprocess
from time import ctime

def appium_start(host, port):

    bootstrap_port = str(port + 1)
    cmd = 'start /b appium -a ' + host + ' -p ' + str(port) + ' --bootstrap-port ' + str(bootstrap_port)

    print('%s at %s' % (cmd, ctime()))
    subprocess.Popen(cmd, shell=True,stdout=open('./appiumlog/'+str(port)+'.log', 'a'), stderr=subprocess.STDOUT)


#构建appium进程组
appium_process=[]

#加载appium进程
for i in range(2):
    host='127.0.0.1'
    port = 4723 + 2 * i
    appium=multiprocessing.Process(target=appium_start, args=(host, port))
    appium_process.append(appium)


if __name__ == '__main__':
    #并发启动appium服务
    for appium in appium_process:
        appium.start()
    for appium in appium_process:
        appium.join()

最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!

相关推荐
DanCheng-studio9 分钟前
毕设 基于机器视觉的驾驶疲劳检测系统(源码+论文)
python·毕业设计·毕设
carpell11 分钟前
【语义分割专栏】3:Segnet实战篇(附上完整可运行的代码pytorch)
人工智能·python·深度学习·计算机视觉·语义分割
一只小波波呀1 小时前
打卡第48天
python
zstar-_1 小时前
一套个人知识储备库构建方案
python
Amo Xiang2 小时前
《100天精通Python——基础篇 2025 第5天:巩固核心知识,选择题实战演练基础语法》
python·选择题·基础语法
江梦寻2 小时前
MacOS下Homebrew国内镜像加速指南(2025最新国内镜像加速)
开发语言·后端·python·macos·架构·策略模式
霖檬ing2 小时前
Python——MySQL远程控制
开发语言·python·mysql
miniwa2 小时前
Python编程精进:CSV 模块
python
Kookoos7 小时前
Dynamics 365 Finance + Power Automate 自动化凭证审核
运维·自动化·dynamics 365·power automate
老胖闲聊9 小时前
Python Copilot【代码辅助工具】 简介
开发语言·python·copilot