Appium 并发测试之 python 启动 appium 服务

python 启动appium服务,需要使用subprocess模块,该模块可以创建新的进程,并且连接到进程的输入,输出,错误等管道信息,并且可以获取进程的返回值

测试场景

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

Appium服务器端口:4723 bp端口:4724

Appium服务器端口:4725 bp端口:4726

可以看到appium服务器端口和bp端口是相差一位的

什么是bp端口?

bp端口就是bootstrap port,是appium和手机之间的通讯端口,

如果不能指定到,则无法运行多台设备

1.启动单个appium服务

import subprocess

from appium import webdriver

from selenium.webdriver.support.ui import WebDriverWait

from time import sleep

import time

def appium_start(host, port):

指定bp端口号

bootstrap_root = str(port+1)

把cmd弹窗输入的命令,直接些在这里

cmd = 'start /b appium -a ' + host + ' -p ' +str(port)+' -bp ' +str(bootstrap_root)

去掉 /b 即可打开cmd弹窗运行

cmd = 'start appium -a ' + host + ' -p ' +str(port)+' -bp ' +str(bootstrap_root)

#打印输入的cmd命令以及时间

print('%s at %s' %(cmd, time.ctime()))

subprocess.Popen(cmd, shell= True, stdout=open('./appium_log/'+str(port)+'.log','a'), stderr=subprocess.STDOUT)

if name == 'main':

host = '127.0.0.1'

运行一个端口

port = 4723

appium_start(host, port)

启动校验

1.启动后生成日志文件

2.启动后我们需要检验服务是否启动成功

首先查看有没有生成对应的log文件,查看log里面的内容

使用如下命令查看,cmd输入
netstat -ano | findstr 端口号
netstat 命令解释

netstat命令是一个监控TCP/IP网络的非常有用的工具,它可以显示路由表,、实际的网络练连接以及每一个网络接口设备的状态信息.输入netstat -ano回车。可以查看本机开放的全部端口;输入命令netsat -h 可以查看全部参数含义

关闭Appium服务

关闭进程有2中方式,具体如下:

1.通过 netstat 命令找到对应的 Appium 进程 pid 然后可以在系统任务管理器去关闭进程;win7 系统任务管理器 PID 显示

打开电脑的任务管理器 ---> 详细信息,找到对应 pid 后,关闭进程即可

使用如下命令来关闭:
taskkill -f -pid appium进程pid

注意:pid即通过上面netstat命令查看的状态信息

关闭成功后如图所示:

二、启动多个 appium 服务

只需要在执行环境使用循环调用即可。

import subprocess

from appium import webdriver

from selenium.webdriver.support.ui import WebDriverWait

from time import sleep

import time

def appium_start(host, port):

指定bp端口号

bootstrap_root = str(port+1)

把cmd弹窗输入的命令,直接些在这里

cmd = 'start /b appium -a ' + host + ' -p ' +str(port)+' -bp ' +str(bootstrap_root)

去掉 /b 即可打开cmd弹窗运行

cmd = 'start appium -a ' + host + ' -p ' +str(port)+' -bp ' +str(bootstrap_root)

#打印输入的cmd命令以及时间

print('%s at %s' %(cmd, time.ctime()))

subprocess.Popen(cmd, shell= True, stdout=open('./appium_log/'+str(port)+'.log','a'), stderr=subprocess.STDOUT)

if name == 'main':

host = '127.0.0.1'

运行一个端口

port = 4723

for i in range(2):

port = 4723 + 2* i

appium_start(host, port)

启动成功后显示:

用以上方法启动两个 appium 服务,由于显示启动时间较快,看不出区别,实际两个 appium 服务不是同时启动的

会生成两个日志文件:

三、多进程并发启动 appium 服务

python 多进程并发启动 appium 服务需要导入 multiprocessing 多进程模块

多进程并发启动多个appium服务

import multiprocessing

import subprocess

from time import ctime

import time

def appium_start_sync(host, port):

指定bp端口号

bootstrap_root = str(port+1)

把cmd弹窗输入的命令,直接些在这里

cmd = 'start /b appium -a ' + host + ' -p ' +str(port)+' -bp ' +str(bootstrap_root)

去掉 /b 即可打开cmd弹窗运行

cmd = 'start appium -a ' + host + ' -p ' +str(port)+' -bp ' +str(bootstrap_root)

#打印输入的cmd命令以及时间

print('%s at %s' %(cmd, time.ctime()))

subprocess.Popen(cmd, shell= True, stdout=open('./appium_log/'+str(port)+'.log','a'), stderr=subprocess.STDOUT)

构建进程组

appium_process = []

host = '127.0.0.1'

运行一个端口

port = 4723

加载appium进程

for i in range(2):

port = 4723 + 2 * i

appium_sync = multiprocessing.Process(target=appium_start_sync, args=(host, port))

appium_process.append(appium_sync)

测试函数,在手机运行过程中可以注释

if name == 'main':

并发启动appium服务

for appium in appium_process:

appium.start()

for appium in appium_process:

appium.join()

多进程并发启动多个appium服务

import multiprocessing

import subprocess

from time import ctime

import time

class AppiumStartSync:

def appium_start_sync(host, port):

指定bp端口号

bootstrap_root = str(port+1)

把cmd弹窗输入的命令,直接些在这里

cmd = 'start /b appium -a ' + host + ' -p ' +str(port)+' -bp ' +str(bootstrap_root)

去掉 /b 即可打开cmd弹窗运行

cmd = 'start appium -a ' + host + ' -p ' +str(port)+' -bp ' +str(bootstrap_root)

#打印输入的cmd命令以及时间

print('%s at %s' %(cmd, time.ctime()))

subprocess.Popen(cmd, shell= True, stdout=open('./appium_log/'+str(port)+'.log','a'), stderr=subprocess.STDOUT)

构建进程组

appium_process = []

host = '127.0.0.1'

运行一个端口

port = 4723

加载appium进程

for i in range(2):

port = 4723 + 2 * i

appium_sync = multiprocessing.Process(target=appium_start_sync, args=(host, port))

appium_process.append(appium_sync)

测试函数,在手机运行过程中可以注释

if name == 'main':

appium_start = AppiumStartSync()

并发启动appium服务

for appium in appium_start.appium_process:

appium.start()

for appium in appium_start.appium_process:

appium.join()

相关推荐
zone773914 分钟前
003:RAG 入门-LangChain 读取图片数据
后端·python·面试
用户83562907805128 分钟前
在 PowerPoint 中用 Python 添加和定制形状的完整教程
后端·python
用户962377954481 小时前
🚀 docx2md-picgo:Word 文档图片一键上传图床工具
python·markdown
zone77391 天前
001:简单 RAG 入门
后端·python·面试
F_Quant1 天前
🚀 Python打包踩坑指南:彻底解决 Nuitka --onefile 配置文件丢失与重启报错问题
python·操作系统
允许部分打工人先富起来1 天前
在node项目中执行python脚本
前端·python·node.js
IVEN_1 天前
Python OpenCV: RGB三色识别的最佳工程实践
python·opencv
haosend1 天前
AI时代,传统网络运维人员的转型指南
python·数据网络·网络自动化
曲幽1 天前
不止于JWT:用FastAPI的Depends实现细粒度权限控制
python·fastapi·web·jwt·rbac·permission·depends·abac
IVEN_2 天前
只会Python皮毛?深入理解这几点,轻松进阶全栈开发
python·全栈