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()

相关推荐
AC赳赳老秦1 分钟前
OpenClaw + 云数据库运维:自动备份、扩容、迁移 RDS/MySQL 云数据库
运维·开发语言·数据库·人工智能·python·mysql·openclaw
Tbisnic3 分钟前
AI大模型学习第十二天:Coze工作流与智能体开发
人工智能·python·ai·大模型·智能体·coze
冷小鱼3 分钟前
高级研发编码习惯:从规范到艺术,再到AI+时代的人机协同
java·开发语言·python·编码习惯
matrixmind812 分钟前
HTTPX:Python 下一代 HTTP 客户端
python·其他·http·httpx
怪兽学LLM19 分钟前
LeetCode 21 合并两个有序链表:彻底理解虚拟头节点(Dummy)套路
python·leetcode·链表
XLYcmy20 分钟前
一个基于 Python 的轻量级 LLM(大语言模型)API 客户端程序:从API交互到LLM应用架构
服务器·python·ai·llm·prompt·agent·token
程序员佳佳21 分钟前
四个月长期实测:自建 Milvus、FAISS、原生向量 API 和向量引擎中转方案,到底怎么选?
人工智能·windows·python·gpt·milvus·faiss
shimly12345623 分钟前
python3 venv 是啥?
python
aqi0030 分钟前
15天学会AI应用开发(六)使用离线大模型对文本生成摘要
人工智能·python·ai编程