app自動化測試框架(八、啟動appium服務)

一、前面我們都是用appium界面版啟動服務的,現在使用python腳本啟動無界面版 appium服務

1、安裝 npm install -g appium

lxdeMacBook-Pro-2:~ lx$ appium
[Appium] Welcome to Appium v1.21.0
[Appium] Appium REST http interface listener started on 0.0.0.0:4723

安裝成功,啟動成功

2、新建啟動appium腳本:
參考:https://www.cnblogs.com/zouzou-busy/p/11440587.html

image.png

import subprocess
import os
import platform

cur_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))
log_path = os.path.join(os.path.dirname(cur_path), 'logs/autotest_app/appium_log/')
if not os.path.exists(log_path):os.makedirs(log_path)

platform_type = platform.system()

def appium_start(host,port):
    '''
    啟動appium server
    :param host:
    :param port:
    :return:
    '''
    #指定bp端口號
    bootstrap_port = str(port+1)
    if platform_type == 'Darwin': #mac
        cmd = 'appium -a ' + host+' -p '+str(port) +' -bp '+ str(bootstrap_port)
        subprocess.Popen(cmd,shell=True,stdout=open(log_path+str(port)+'.log','a'),stderr=subprocess.STDOUT)

def appium_close(port):
    """
    關閉 appium server
    :param port:
    :return:
    """
    if platform_type == 'Darwin':  # mac
        p = os.popen(f'lsof -i tcp:{port}')
        p0 = p.read()
        if p0.strip() != '':
            p1 = int(p0.split('\n')[1].split()[1])  # 獲取進程號
            os.popen(f'kill {p1}')  # 結束進程
            with open(log_path+str(port)+'.log','a') as ft:
                ft.write("關閉appium server " + str(port))

if __name__ == '__main__':
    host = '127.0.0.1'
    #運行一個端口
    port = 4723
    appium_start(host,port)
    appium_close(port)
    #運行2個端口

3、執(zhí)行測試前啟動appium server,測試結束后關閉服務

import pytest,os
from common import startappium

cur_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))
result_path = cur_path + '/web/autotest/ui/result/'
result_report = cur_path + '/web/autotest/ui/result_report/'
if not os.path.exists(result_path):os.makedirs(result_path)
if not os.path.exists(result_report):os.makedirs(result_report)

if __name__ == '__main__':

    #啟動appium server
    startappium.appium_start('127.0.0.1',4723)

    pytest.main([
        '-m','smoke', #篩選帶有smoke標記的所有測試用例
        "--reruns", "1",  #將錯誤的用例重跑1次
        "--reruns-delay","1", #重跑case的間隔時間
        '--clean-alluredir',
        '--alluredir=' + result_path,
        'test_suites/test_login.py',
    ])
    os.system("allure generate --clean "+result_path+" --report-dir "+result_report) #轉換為html

    # 關閉appium server
    startappium.appium_close(4723)
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容