Appium Python 启动多个 Appium server 进程后自动关闭

Appium Python 启动多个 Appium server 进程后自动关闭 

问题描述

由于需要做多设备并发运行自动化用例,因此首先需要使用脚本启动多个Appium server,代码实现如下:

import socket
import subprocess


class AppiumServer():

def check_port(self, host, port):
"""检测端口是否被占用"""
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((host, int(port)))
s.shutdown(2)
print('port %s is uesd !' %port)
return False
except:
print('port %s is available!' %port)
return True

def start_appium(self,host,port):
"""启动appium 服务"""
erromessage=""
appium_server_url=""
bootstrap_port=str(port+1)

try:
if self.check_port(host,port):

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

# p = subprocess.call(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
p = subprocess.Popen(cmd, shell=True, stdout=open('E:/logs.log','a'), stderr=subprocess.STDOUT)
p.wait()

appium_server_url = 'http://' + host + ':' + str(port) + '/wd/hub'
print(appium_server_url)

except Exception as msg:
erromessage=str(msg)

return appium_server_url,erromessage


if __name__ == '__main__':
s=AppiumServer()
s.start_appium('127.0.0.1',4723)
s.start_appium('127.0.0.1',4725)

运行结果

控制台信息:

port 4723 is available!
start /b appium -a 127.0.0.1 -p 4723 --bootstrap-port 4724
http://127.0.0.1:4723/wd/hub
port 4725 is available!
start /b appium -a 127.0.0.1 -p 4725 --bootstrap-port 4726
http://127.0.0.1:4725/wd/hub

Process finished with exit code 0
posted @ 2019-08-16 13:59  南松丶  阅读(711)  评论(0编辑  收藏  举报