def main(args): # script filepath path, name = script_dir_name(args.script) record_list = args.record or [] log_root = decode_path(args.log_root) or decode_path( os.path.join(path, LOGDIR)) static_root = args.static_root or STATIC_DIR static_root = decode_path(static_root) export = decode_path(args.export) if args.export else None lang = args.lang if args.lang in ['zh', 'en'] else 'en' plugins = args.plugins device = args.device package_name = args.package test_name = args.test_name print(static_root, export) # gen html report rpt = LogToHtml(path, log_root, static_root, export_dir=export, script_name=name, lang=lang, plugins=plugins, device=device, package_name=package_name, test_name=test_name) rpt.report(HTML_TPL, output_file=args.outfile, record_list=record_list)
def setup_by_args(args): # init devices if isinstance(args.device, list): devices = args.device elif args.device: devices = [args.device] else: devices = [] print("do not connect device") # set base dir to find tpl dirpath, _ = script_dir_name(args.script) # set log dir if args.log: args.log = script_log_dir(dirpath, args.log) print("save log in '%s'" % args.log) else: print("do not save log") # guess project_root to be basedir of current .air path project_root = os.path.dirname( args.script) if not ST.PROJECT_ROOT else None auto_setup(dirpath, devices, args.log, project_root)
def report(self, template_name=HTML_TPL, output_file=None, record_list=None): """ Generate the report page, you can add custom data and overload it if needed :param template_name: default is HTML_TPL :param output_file: The file name or full path of the output file, default HTML_FILE :param record_list: List of screen recording files :return: """ if not self.script_name: path, self.script_name = script_dir_name(self.script_root) if self.export_dir: self.script_root, self.log_root = self._make_export_dir() # output_file可传入文件名,或绝对路径 output_file = output_file if output_file and os.path.isabs(output_file) \ else os.path.join(self.script_root, output_file or HTML_FILE) if not self.static_root.startswith("http"): self.static_root = "static/" if not record_list: record_list = [ f for f in os.listdir(self.log_root) if f.endswith(".mp4") ] data = self.report_data(output_file=output_file, record_list=record_list) return self._render(template_name, output_file, **data)
def setup_by_args(args): # init devices if isinstance(args.device, list): devices = args.device elif args.device: devices = [args.device] else: devices = [] print("do not connect device") # set base dir to find tpl dirpath, _ = script_dir_name(args.script) # set log dir if args.log: args.log = script_log_dir(dirpath, args.log) print("save log in '%s'" % args.log) else: print("do not save log") # set snapshot quality if args.compress: compress = args.compress else: compress = ST.SNAPSHOT_QUALITY if args.no_image: ST.SAVE_IMAGE = False # guess project_root to be basedir of current .air path project_root = os.path.dirname( args.script) if not ST.PROJECT_ROOT else None auto_setup(dirpath, devices, args.log, project_root, compress)
def __init__(self, script_root, log_root="", static_root="", export_dir=None, script_name="", logfile=None, lang="en", plugins=None): self.log = [] self.script_root = script_root self.script_name = script_name if not self.script_name or os.path.isfile(self.script_root): self.script_root, self.script_name = script_dir_name( self.script_root) self.log_root = log_root or ST.LOG_DIR or os.path.join( ".", DEFAULT_LOG_DIR) self.static_root = static_root or STATIC_DIR self.test_result = True self.run_start = None self.run_end = None self.export_dir = export_dir self.logfile = logfile or getattr(ST, "LOG_FILE", DEFAULT_LOG_FILE) self.lang = lang self.init_plugin_modules(plugins)
def create_report_dir(platform) -> str: """ 创建报告生成的文件夹 :param platform: 区分不同的平台 :return: """ log_dir = "log/{0}/{1}".format(platform.lower(), (int(time.time()))) path, name = script_dir_name(__file__) log_path = os.path.join(os.path.dirname(os.path.dirname(path)), log_dir) return log_path
def simple_report(filepath, logpath=True, logfile=None, output=HTML_FILE): path, name = script_dir_name(filepath) if logpath is True: logpath = os.path.join(path, getattr(ST, "LOG_DIR", DEFAULT_LOG_DIR)) rpt = LogToHtml(path, logpath, logfile=logfile or getattr(ST, "LOG_FILE", DEFAULT_LOG_FILE), script_name=name) rpt.report(HTML_TPL, output_file=output)
def runTest(self): scriptpath, pyfilename = script_dir_name(self.args.script) pyfilepath = os.path.join(scriptpath, pyfilename) pyfilepath = os.path.abspath(pyfilepath) self.scope["__file__"] = pyfilepath with open(pyfilepath, 'r', encoding="utf8") as f: code = f.read() pyfilepath = pyfilepath.encode(sys.getfilesystemencoding()) try: exec(compile(code.encode("utf-8"), pyfilepath, 'exec'), self.scope) except Exception as err: log(err, desc="Final Error", snapshot=True) six.reraise(*sys.exc_info())
def report(task, log_path): """ 生成报告 :param task: 前端配置的任务 :param log_path:日志的路径 :return: """ path, name = script_dir_name(__file__) try: m = importlib.import_module('config') if task is not None: m.__author__ = task.name path, name = script_dir_name(m.__file__) except: try: m = importlib.import_module('library.author') if task is not None: m.__author__ = task.name path, name = script_dir_name(m.__file__) except: if task is not None: path, name = script_dir_name(sys.argv[0]) static_root = {} if task is not None: static_root['static_root'] = 'http://localhost:9000/static' rpt = LogToHtml(script_root=path, script_name=name, export_dir=log_path, **static_root, log_root=log_path, logfile=LOGFILE, lang="zh", plugins=["poco.utils.airtest.report"]) rpt.report(HTML_TPL) return name.replace('.py', '') + '.log'
def simple_report(filepath, logpath=True, logfile=LOGFILE, output=HTML_FILE): path, name = script_dir_name(filepath) print('***%s script_dir: %s' % (fname, path)) if logpath is True: logpath = os.path.join(path, LOGDIR) print('***%s report/log dir: %s' % (fname, logpath)) elif logpath: logpath = os.path.join(path, logpath) print('***%s log path: %s' % (fname, logpath)) if not os.path.exists(logpath): os.makedirs(logpath) output = os.path.join(logpath, output) print('***%s html report path: %s' % (fname, output)) rpt = LogToHtml(path, logpath, logfile=logfile, script_name=name) rpt.report(HTML_TPL, output_file=output)
def get_report(script, log_root=ST.LOG_DIR, outfile='log.html', static_root="", lang="zh", record_list=None): """生成airtest_report""" path, name = script_dir_name(script) rpt = LogToHtml(path, log_root, static_root=static_root, export_dir=report_path(), script_name=name, lang=lang, plugins=["poco.utils.airtest.report"]) rpt.report(HTML_TPL, output_file=outfile, record_list=record_list)
def report(self, template_name=HTML_TPL, output_file=None, record_list=None): """ Generate the report page, you can add custom data and overload it if needed :param template_name: default is HTML_TPL :param output_file: The file name or full path of the output file, default HTML_FILE :param record_list: List of screen recording files :return: """ if not self.script_name: path, self.script_name = script_dir_name(self.script_root) if self.export_dir: self.script_root, self.log_root = self._make_export_dir() # output_file可传入文件名,或绝对路径 output_file = output_file if output_file and os.path.isabs(output_file) \ else os.path.join(self.script_root, output_file or HTML_FILE) if not self.static_root.startswith("http"): self.static_root = "static/" log_all_file = os.path.join(self.log_root, 'log.txt') self.split_outfile(log_all_file) # 这里分割了 log 文件 if not record_list: record_list = [ f for f in os.listdir(self.log_root) if f.endswith(".mp4") ] all_case_data = [] # 这里按 test_name 分别生成 data 信息 for test_name in self.test_name: data = self.report_data(output_file=output_file, record_list=record_list, logfile=self.logfile[test_name], test_name=test_name, script_name=f"{test_name}.py") all_case_data.append(data) data_all = self.report_data(output_file=output_file, record_list=record_list, logfile=log_all_file, test_name="all") return self._render(template_name, output_file, all_case_data=all_case_data, **data_all)
def _airtest_report(htmlName): """ 自定义airtest报告输出,需要传入报告html文件名称 :param htmlName: airtest报告输出的html文件 :return: None """ htmlName = os.path.join(tempDir, htmlName) path, name = script_dir_name(caseFileName) rpt = LogToHtml( path, reportLogDir, static_root="static/", logfile=LOGFILE, script_name=name, ) rpt.report(HTML_TPL, output_file=htmlName)
def runTest(self): scriptpath, pyfilename = script_dir_name(self.args.script) pyfilepath = os.path.join(scriptpath, pyfilename) pyfilepath = os.path.abspath(pyfilepath) self.scope["__file__"] = pyfilepath with open(pyfilepath, 'r', encoding="utf8") as f: code = f.read() pyfilepath = pyfilepath.encode(sys.getfilesystemencoding()) print('<gql> runner>runTest>pyfilepath: ', pyfilepath) #print('<gql> runner>runTest>code: ',code) #print('<gql> runner>runTest>self.scope: ',self.scope) # log('<gql> runner>runTest>pyfilepath: ',pyfilepath) # log('<gql> runner>runTest>code: ',code) # log('<gql> runner>runTest>self.scope: ',self.scope) try: exec(compile(code.encode("utf-8"), pyfilepath, 'exec'), self.scope) except Exception as err: tb = traceback.format_exc() log("Final Error", tb) six.reraise(*sys.exc_info())
def makeData(self): self._load() steps = self._analysis() path, self.script_name = script_dir_name(self.script_root) script_path = os.path.join(self.script_root, self.script_name) info = json.loads(get_script_info(script_path)) #mpr repord record_list = [ f for f in os.listdir(self.log_root) if f.endswith(".mp4") ] records = [ os.path.join(LOGDIR, f) if self.export_dir else os.path.abspath( os.path.join(self.log_root, f)) for f in record_list ] scriptname = self.script_name if self.needResetPath: scriptname = self.splitPath(self.script_root, self.caseName) info['path'] = self.splitPath(info['path'], self.caseName) data = {} data['steps'] = steps data['name'] = scriptname data['test_result'] = self.test_result data['field_num'] = self.FieldNum data['run_end'] = self.run_end data['run_start'] = self.run_start #data['static_root'] = self.static_root data['records'] = records data['info'] = info # data['log'] = self.get_relative_log(output_file) # data['console'] = self.get_console(output_file) # 如果带有<>符号,容易被highlight.js认为是特殊语法,有可能导致页面显示异常,尝试替换成不常用的{} info = json.dumps(data).replace("<", "{").replace(">", "}") # data['data'] = info return data
def simple_report(filepath, logpath=True, logfile=LOGFILE, output=HTML_FILE): path, name = script_dir_name(filepath) if logpath is True: logpath = os.path.join(path, LOGDIR) rpt = LogToHtml(path, logpath, logfile=logfile, script_name=name) rpt.report(HTML_TPL, output_file=output)
def get_log_path(filepath, logpath): path, name = script_dir_name(filepath) print(path + ' ' + name) logpath = os.path.join(path, logpath) return logpath