From d99285c96b21577e95a4b47477b221edce7fc584 Mon Sep 17 00:00:00 2001 From: defnngj Date: Thu, 16 Jan 2025 01:49:11 +0800 Subject: [PATCH] :sparkles: supper local style #57 --- XTestRunner/config.py | 37 ++++++++++++++++++++++++++++++++ XTestRunner/html/template.html | 10 ++++----- XTestRunner/htmlrunner/runner.py | 9 +++++++- 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/XTestRunner/config.py b/XTestRunner/config.py index 33b73c5..37a37aa 100644 --- a/XTestRunner/config.py +++ b/XTestRunner/config.py @@ -1,4 +1,6 @@ +import os.path import time +import shutil class Config: @@ -42,3 +44,38 @@ def inner(cls): return cls return inner + + +def static_file(is_local_style=False, report_path: str = None) -> dict: + """ + static file path + :param is_local_style: + :param report_path: + :return: + """ + if is_local_style is True: + if report_path is None: + raise FileNotFoundError("report path is null") + + report_dir = os.path.dirname(report_path) + dst_static_dir = os.path.join(report_dir, "static") + root_dir = os.path.dirname(os.path.abspath(__file__)) + src_static_dir = os.path.join(root_dir, "html", "static") + # copy static file + shutil.copytree(src_static_dir, dst_static_dir, dirs_exist_ok=True) + # local static file + jquery_url = echarts_url = "static/js/" + css_url = "static/css/" + png_url = "static/images/" + else: + jquery_url = "https://libs.baidu.com/jquery/2.0.0/" + echarts_url = "https://cdnjs.cloudflare.com/ajax/libs/echarts/5.4.2/" + css_url = png_url = "https://telegraph-image-cq2.pages.dev/" + + static_dir = { + "jquery_url": jquery_url, + "echarts_url": echarts_url, + "css_url": css_url, + "png_url": png_url + } + return static_dir diff --git a/XTestRunner/html/template.html b/XTestRunner/html/template.html index 90ce8d5..e7b6fd6 100644 --- a/XTestRunner/html/template.html +++ b/XTestRunner/html/template.html @@ -5,11 +5,11 @@ {{ title }} - - - - - + + + + + {{ stylesheet }} diff --git a/XTestRunner/htmlrunner/runner.py b/XTestRunner/htmlrunner/runner.py index 4d112e1..42e8fd3 100644 --- a/XTestRunner/htmlrunner/runner.py +++ b/XTestRunner/htmlrunner/runner.py @@ -8,7 +8,7 @@ from jinja2 import Environment, FileSystemLoader from XTestRunner.htmlrunner.result import _TestResult from XTestRunner.htmlrunner.multi_language import language_tag -from XTestRunner.config import RunResult, Config +from XTestRunner.config import RunResult, Config, static_file from XTestRunner.version import get_version from XTestRunner._email import SMTP from XTestRunner._dingtalk import DingTalk @@ -154,12 +154,14 @@ def __init__(self, rerun=0, language="en", logger=None, + local_style=False, **kwargs): self.stream = stream self.verbosity = verbosity self.rerun = rerun self.run_times = 0 self.logger = logger + self.local_style = local_style Config.language = language if title is None: self.title = DEFAULT_TITLE @@ -322,8 +324,13 @@ def generate_report(self, test, result): version = get_version() heading = self._generate_heading(base, statistics) report = self._generate_report(result) + static = static_file(self.local_style, self.stream.name) html_content = template.render( + jquery_url=static["jquery_url"], + echarts_url=static["echarts_url"], + css_url=static["css_url"], + png_url=static["png_url"], title=saxutils.escape(self.title), version=version, stylesheet=stylesheet,