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

相关推荐
查理零世11 分钟前
【蓝桥杯集训·每日一题2025】 AcWing 6134. 哞叫时间II python
python·算法·蓝桥杯
紫雾凌寒21 分钟前
解锁机器学习核心算法|神经网络:AI 领域的 “超级引擎”
人工智能·python·神经网络·算法·机器学习·卷积神经网络
sun lover33 分钟前
conda简单命令
python·conda
Mike_188702783511 小时前
1688代采下单API接口使用指南:实现商品采集与自动化下单
前端·python·自动化
青铜念诗1 小时前
python脚本文件设置进程优先级(在.py文件中实现)
开发语言·python
Dyan_csdn2 小时前
【Python项目】文本相似度计算系统
开发语言·python
pianmian12 小时前
python绘图之回归拟合图
开发语言·python·回归
MiyamiKK572 小时前
leetcode_位运算 190.颠倒二进制位
python·算法·leetcode
伊一线天2 小时前
体验用ai做了个python小游戏
人工智能·python·pygame
高力士等十万人2 小时前
OpenCV二值化处理
python·opencv·计算机视觉