ui-auto-playwright/run.py

114 lines
5.8 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# !/usr/bin/env Python3
# -*- coding: utf-8 -*-
# @Author : 向洋
# @FILE : run.py
# @Software : PyCharm
# desc
import argparse
import os
import pytest
from common.report_handle import AllureReportBeautiful
from common.file_hanlde import copy_file, zip_file
from common.logLib import log
from common.platform_handle import PlatformHandle
from common.send_result_handle import send_result
from config.global_vars import GLOBAL_VARS, ENV_VARS
from config.path_config import ALLURE_HTML_DIR, ALLURE_RESULTS_DIR, REPORT_DIR, CONF_DIR
from config.settings import RunConfig
def generate_allure_report():
"""
通过allure生成html测试报告并对报告进行美化
"""
# ----------------START: 判断运行的平台是linux还是windows执行不同的allure命令----------------
plat = PlatformHandle()
cmd = plat.allure
os.popen(cmd).read()
# ----------------END: 判断运行的平台是linux还是windows执行不同的allure命令 ----------------
# ----------------START: 美化allure测试报告 ------------------------------------------
# 设置打开的 Allure 报告的浏览器窗口标题文案
AllureReportBeautiful(allure_html_path=ALLURE_HTML_DIR).set_windows_title(
new_title=ENV_VARS["common"]["project_name"])
# 修改Allure报告Overview的标题文案
AllureReportBeautiful(allure_html_path=ALLURE_HTML_DIR).set_report_name(
new_name=ENV_VARS["common"]["report_title"])
# 在allure-html报告中往widgets/environment.json中写入环境信息
env_info = ENV_VARS["common"]
env_info["run_env"] = GLOBAL_VARS.get("host", None)
AllureReportBeautiful(allure_html_path=ALLURE_HTML_DIR).set_report_env_on_html(
env_info=env_info)
# ----------------END: 美化allure测试报告 ------------------------------------------
# ----------------START: 压缩allure测试报告方便后续发送压缩包------------------------------------------
# 复制http_server.exe以及双击打开Allure报告.bat以便windows环境下直接打开查看allure html报告
allure_config_path = os.path.join(CONF_DIR, "allure_config")
copy_file(src_file_path=os.path.join(allure_config_path,
[i for i in os.listdir(allure_config_path) if i.endswith(".exe")][0]),
dest_dir_path=ALLURE_HTML_DIR)
copy_file(src_file_path=os.path.join(allure_config_path,
[i for i in os.listdir(allure_config_path) if i.endswith(".bat")][0]),
dest_dir_path=ALLURE_HTML_DIR)
attachment_path = os.path.join(REPORT_DIR, f'autotest_report.zip')
zip_file(in_path=ALLURE_HTML_DIR, out_path=attachment_path)
# ----------------END: 压缩allure测试报告方便后续发送压缩包------------------------------------------
return ALLURE_HTML_DIR, attachment_path
def run(env, report, browser_type, send):
try:
# 设置当前运行的浏览器驱动类型
RunConfig.browser_type = browser_type
log.debug(f"命令行参数传递的驱动类型:{RunConfig.browser_type}")
# delete_dir_file(BAK_DOWN_DIR)
# 根据指定的环境参数将运行环境所需相关配置数据保存到GLOBAL_VARS
GLOBAL_VARS["env_key"] = env.lower()
if ENV_VARS.get(env.lower()):
GLOBAL_VARS.update(ENV_VARS[env.lower()])
arg_list = ['test_cases']
# arg_list = [rf'{os.path.join(ROOT_DIR, "test_cases", "user_module", "test_login.py")}']
# arg_list = [r'test_cases\user_module\test_login.py::TestLogin::test_login_by_user']
# ------------------------ pytest执行测试用例 ------------------------
arg_list.extend(['-sv', f"--maxfail={RunConfig.max_fail}", f"--reruns={RunConfig.rerun}",
f"--reruns-delay={RunConfig.reruns_delay}", '-pno:warnings'])
if report.lower() == "allure":
arg_list.extend([f'--alluredir={ALLURE_RESULTS_DIR}', '--clean-alluredir'])
# print(arg_list)
pytest.main(args=arg_list)
report_path, attachment_path = generate_allure_report()
else:
# 测试报告路径
report_path = os.path.join(REPORT_DIR, "autotest_report.html")
attachment_path = report_path
# 优化测试报告的css路径
pytest_html_config_path = os.path.join(CONF_DIR, "pytest_html_config")
report_css = os.path.join(pytest_html_config_path, "pytest_html_report.css")
# pytest运行的参数
arg_list.extend(['--self-contained-html', f'--html={report_path}', f"--css={report_css}"])
# 使用pytest运行测试用例
pytest.main(args=arg_list)
# ------------------------ 发送测试结果 ------------------------
send_result(report_path=report_path, report_type=report, attachment_path=attachment_path, send_type=int(send))
except Exception as e:
log.exception(e)
raise e
if __name__ == '__main__':
# 定义命令行参数
parser = argparse.ArgumentParser(description="框架主入口")
parser.add_argument("-report", default="allure", help="选择需要生成的测试报告pytest-html, allure")
parser.add_argument("-env", default="test", help="输入运行环境test 或 pro 等")
parser.add_argument("-browser", default="chrome-headless",
help="浏览器驱动类型配置支持如下类型chrome, chrome-headless, firefox, firefox-headless, webkit")
parser.add_argument("-send", default=5, help="0表示不发送任何通知 1代表Lark(飞书)通知, 2代表钉钉通知 3代表企业微信通知 4代表邮件通知 5代表所有途径都发送通知")
args = parser.parse_args()
run(env=args.env, report=args.report, browser_type=args.browser, send=args.send)