命令行啟動和停用appium server
1.獲取系統類型和命令類型
#獲取系統的名稱,使用對應的指令
def getsystemstr():
system=platform.system()
if system=='Windows':
find_manage='findstr'
else:
find_manage='grep'
return [system,find_manage]
2.獲取已連接的設備列表:
#獲取設備列表
def get_device_list():
devices = []
result = subprocess.Popen("adb devices", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.readlines()
result.reverse()
for line in result[1:]:
if b"attached" not in line.strip():
devices.append(line.split()[0])
else:
break
return devices
3.根據系統和端口來啟動appium server
def start_appiumServer(port11,port12):
port13 = str(port11)
port14 = str(port12)
systemstr = SystemCheck.getsystemstr()[1]
#os.system("subst w: \"C:\Program Files (x86)\Appium\"")
#cmd1 = 'cd C:\\Program Files (x86)\\Appium\\'
#cmd2 = "start node C:\\Program Files (x86)\\Appium\\node_modules\\appium\\lib\\server\\main.js " \
#"--address 127.0.0.1 --port "+port11+"-bp 4724 -U " +deviceuuid
if systemstr == "Windows":
os.system("cd C:\\Program Files (x86)\\Appium\\ && start node node_modules\\appium\\lib\\server\\main.js --address 127.0.0.1 --port "+port13+ " -bp "+port14)
else:
os.system("/usr/local/bin/appium -a 127.0.0.1 -p "+ port13 + " -bp " +port14 +" &" )
time.sleep(2)
print("appium-server started")
4.根據系統和端口號關閉appium server
def kill_appiumServer(port13):
# 查找對應端口的pid
systemstr = systemstr = SystemCheck.getsystemstr()[1]
if systemstr == "Windows":
cmd_find = 'netstat -aon | findstr %s' % port13
#print(cmd_find)
result = os.popen(cmd_find)
text = result.read()
if text != "":
pid = text[-5:-1]
# 執行被占用端口的pid
cmd_kill = 'taskkill -f -pid %s' % pid
#print(cmd_kill)
os.popen(cmd_kill)
print("apppium-server killed")
else:
print("The appium-server port is not occupied and is available")
else:
cmd_find = 'lsof -i tcp:%s' % port13
#print(cmd_find)
result = os.popen(cmd_find)
text = result.read()
#print(text)
if text != "":
pid = text.split()[10]
print (pid)
# 執行被占用端口的pid
cmd_kill = 'kill -9 %s' % pid
print(cmd_kill)
os.popen(cmd_kill)
print("apppium-server killed")
else:
print("The appium-server port is not occupied and is available")
