appium 啟動關閉appium服務


def send_cmd(cmd, encoding='utf-8'):
    '''
    cmd發送命令
    :param cmd:命令
    :param encoding: 編碼方式,默認utf-8,出現亂碼用gbk
    :return:
    '''
    res = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                           stderr=subprocess.PIPE, encoding=encoding)
    com = res.communicate()
    value = com[0]
    res.terminate()
    return value

def start_appium(port,name):
    bp = int(port) +1
    kill_appium_server(port=port)
    kill_appium_server(port=bp)
    appium_log = os.path.join(DirPath.Logs, 'Appium_' + name + '.log')
    # 后台執行
    cmd = "start appium -p " + str(port) + " -bp " + str(
        bp) + " -g " + appium_log + " --session-override -a 127.0.0.1 --command-timeout 300"
    log.info(cmd)
    os.popen(cmd)
    time.sleep(10)

def kill_appium_server(port):
    try:
        # 查找對應端口的pid
        cmd_find = 'netstat -aon | findstr %s' % port
        text = send_cmd(cmd_find, encoding="gbk")
        if text != '':
            res = text.split("\n")
            for i in res:
                if "LISTENING" in i:
                    pid = (i.split()[-1]).strip()
                    # 執行被占用端口的pid
                    cmd = 'taskkill /f /pid %s' % pid
                    res = send_cmd(cmd, encoding="gbk")
                    log.debug(str(res).strip())
                    if ("成功" in res) or ("SUCCESS".lower() in res.lower()):
                        log.debug("port %s appium server close" %str(port))
        else:
            return
    except:
        return

  

appium做安卓自動化的時候,需要用代碼啟動和關閉appium服務;

思路:找到服務對應的端口的進程,殺死進程;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM