由于需要做多设备并发运行自动化用例,因此首先需要使用脚本启动多个 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=subprocess.PIPE, 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
同时查看任务管理器,确实成功启动了 2 个 appium server,如下图所示:
但是,大约过 5 秒左右,两个 node.exe 进程就莫名其妙的直接关闭了(单独启动一个也是会过 5 秒左右直接关闭),不知是为何原因?
ps:单独开启 cmd 命令窗口执行命令:appium 启动服务器后并不会自动关闭。
Win7 64bit
Appium V1.4.6
Node.js V6.11.2