命令行启动和停用appium server(兼容Mac和windows)

命令行启动和停用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")
原文地址:https://www.cnblogs.com/Sandy-1128/p/sandy1128-appiumserver.html