Appium Python 啓動多個 Appium server 進程後自動關閉

Appium Python 啓動多個 Appium server 進程後自動關閉 

問題描述

因爲須要作多設備併發運行自動化用例,所以首先須要使用腳本啓動多個Appium server,代碼實現以下:java

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)

運行結果

控制檯信息:shell

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
相關文章
相關標籤/搜索