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

相关推荐
o_insist5 分钟前
LangChain1.0 实现 PDF 文档向量检索全流程
人工智能·python·langchain
脑洞AI食验员10 分钟前
智能体来了:用异常与文件处理守住代码底线
人工智能·python
曲幽23 分钟前
FastAPI登录验证:用OAuth2与JWT构筑你的API安全防线
python·fastapi·web·jwt·token·oauth2
幻云201024 分钟前
Next.js指南:从入门到精通
开发语言·javascript·人工智能·python·架构
CCPC不拿奖不改名32 分钟前
网络与API:从HTTP协议视角理解网络分层原理+面试习题
开发语言·网络·python·网络协议·学习·http·面试
nervermore99036 分钟前
3.2 django框架
python
Learner1 小时前
Python异常处理
java·前端·python
hui函数1 小时前
Python系列Bug修复|如何解决 pip install 安装报错 Backend ‘setuptools.build_meta’ 不可用 问题
python·bug·pip
谢的2元王国1 小时前
prompt工程逐渐成为工作流的重要一部分:以下是一套多节点新闻处理外加事实增强的文章报告日志记录
python