一、前面我们都是用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
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、执行测试前启动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)
网友评论