一昆淡、前面我們都是用appium界面版啟動(dòng)服務(wù)的锰瘸,現(xiàn)在使用python腳本啟動(dòng)無(wú)界面版 appium服務(wù)
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
安裝成功昂灵,啟動(dòng)成功
2避凝、新建啟動(dòng)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):
'''
啟動(dòng)appium server
:param host:
:param port:
:return:
'''
#指定bp端口號(hào)
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):
"""
關(guān)閉 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]) # 獲取進(jìn)程號(hào)
os.popen(f'kill {p1}') # 結(jié)束進(jìn)程
with open(log_path+str(port)+'.log','a') as ft:
ft.write("關(guān)閉appium server " + str(port))
if __name__ == '__main__':
host = '127.0.0.1'
#運(yùn)行一個(gè)端口
port = 4723
appium_start(host,port)
appium_close(port)
#運(yùn)行2個(gè)端口
3、執(zhí)行測(cè)試前啟動(dòng)appium server,測(cè)試結(jié)束后關(guān)閉服務(wù)
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__':
#啟動(dòng)appium server
startappium.appium_start('127.0.0.1',4723)
pytest.main([
'-m','smoke', #篩選帶有smoke標(biāo)記的所有測(cè)試用例
"--reruns", "1", #將錯(cuò)誤的用例重跑1次
"--reruns-delay","1", #重跑case的間隔時(shí)間
'--clean-alluredir',
'--alluredir=' + result_path,
'test_suites/test_login.py',
])
os.system("allure generate --clean "+result_path+" --report-dir "+result_report) #轉(zhuǎn)換為html
# 關(guān)閉appium server
startappium.appium_close(4723)