def runfile(self, args): conffile = args['conffile'].strip() key = args['key'] projectdir = get_projectdirfromkey(key) conffile = conffile.replace('${ROBOT_DIR}', projectdir) conffile = conffile.replace('${PROJECT_DIR}', projectdir) conffile = conffile.replace('%{ROBOT_DIR}', projectdir) conffile = conffile.replace('%{PROJECT_DIR}', projectdir) if not conffile.strip().startswith('/'): conffile = os.path.join(key, conffile) if not os.path.isfile(conffile): return {"status": "fail", "msg": "无法找到配置文件:{}".format(conffile)} case_name = os.path.basename(key) if not is_run(self.app, case_name): p = multiprocessing.Process(target=robot_run, args=(self.app, key, ' -A ' + conffile)) p.start() self.app.config["AUTO_ROBOT"].append({ "name": "%s" % case_name, "process": p }) else: return {"status": "fail", "msg": "请等待前面的任务运行完成."} return {"status": "success", "msg": "开始运行:{}".format(conffile)}
def get_resource_data(app, path): projectdir = get_projectdirfromkey(path) os.environ["ROBOT_DIR"] = projectdir os.environ["PROJECT_DIR"] = projectdir suite = TestSuiteBuilder().build(path) children = [] if suite: # add library , make it can be open if it is a file. for i in suite.resource.imports: rsfile = i.name if rsfile.find("%{ROBOT_DIR}") != -1: rsfile = rsfile.replace("%{ROBOT_DIR}", projectdir) if rsfile.find("%{PROJECT_DIR}") != -1: rsfile = rsfile.replace("%{PROJECT_DIR}", projectdir) # do not show System Library or rs file cannot be found. if not os.path.exists(rsfile): continue if os.path.isfile(rsfile): fname = os.path.basename(rsfile) children.append({ "text": fname, "iconCls": "icon-library", "state": "open", "attributes": { "name": fname, "category": "resource", "key": rsfile, } }) for v in suite.resource.variables: children.append({ "text": v.name, "iconCls": "icon-variable", "state": "open", "attributes": { "name": v.name, "category": "variable", "key": path, } }) for v in suite.resource.keywords: children.append({ "text": v.name, "iconCls": "icon-user-keyword", "state": "open", "attributes": { "name": v.name, "category": "user_keyword", "key": path, } }) return children
def get_robotcase_res(casefile): project = get_projectnamefromkey(casefile) projectdir = get_projectdirfromkey(casefile) os.environ["ROBOT_DIR"] = projectdir os.environ["PROJECT_DIR"] = projectdir cwd = os.getcwd() + "/keyword/" + project if not os.path.exists(cwd): mk_dirs(cwd) try: suite = TestSuiteBuilder().build(casefile) except SyntaxError as e: log.error("Exception:TestSuiteBuilder().build:{} {}".format( casefile, e)) return [] except Exception as e: log.error("Exception:TestSuiteBuilder().build:{} {}".format( casefile, e)) return [] resources = [] for i in suite.resource.imports: rsfile = i.name # Full path of file if rsfile.find("%{ROBOT_DIR}") != -1: rsfile = rsfile.replace("%{ROBOT_DIR}", projectdir) if rsfile.find("%{PROJECT_DIR}") != -1: rsfile = rsfile.replace("%{PROJECT_DIR}", projectdir) if rsfile.startswith('.'): dir = os.path.dirname(casefile) rsfile = os.path.join(dir, rsfile) basename = os.path.basename(rsfile) # Lib name without path fpre = basename.split('.')[ 0] # BuildIn or UserRes without '.resource' or '.robot' xmlfile = cwd + "/%s.xml" % fpre if not os.path.exists(rsfile) and (rsfile.find('/') != -1 or rsfile.find('.robot') != -1 or rsfile.find('.resource') != -1): log.error("找不到资源文件:{} !".format(rsfile)) continue if os.path.exists(rsfile): res = generate_resource_xml( rsfile, xmlfile) if not os.path.exists(xmlfile) else None resources.append(fpre) if os.path.exists(xmlfile) else log.error( "生成资源文件失败 XML:{},INFO:{}".format(xmlfile, res)) else: res = generate_resource_xml( fpre, xmlfile) if not os.path.exists(xmlfile) else None resources.append(fpre) if os.path.exists(xmlfile) else log.error( "生成资源文件失败 XML:{},INFO:{}".format(xmlfile, res)) if rsfile.endswith('.robot') or rsfile.endswith('.resource'): resources += get_robotress_res(rsfile) return resources
def bzt_debugrun(app, yamlfile): projectdir = get_projectdirfromkey(yamlfile) os.environ["ROBOT_DIR"] = projectdir os.environ["PROJECT_DIR"] = projectdir cmd = 'bzt ' + yamlfile cp = subRun(cmd, shell=True, stdout=PIPE, stderr=STDOUT, text=True, timeout=180) # timeout: sec app.config['DB'].insert_loginfo(session['username'], 'case', 'debug', yamlfile, 'OK') return cp.stdout
def py_debugrun(app, pyfile): projectdir = get_projectdirfromkey(pyfile) os.environ["ROBOT_DIR"] = projectdir os.environ["PROJECT_DIR"] = projectdir cmd = 'python ' + pyfile cp = subRun(cmd, shell=True, stdout=PIPE, stderr=STDOUT, text=True, timeout=120) # timeout: sec app.config['DB'].insert_loginfo(session['username'], 'lib', 'debug', pyfile, 'OK') return cp.stdout
def export_casezip(key, exp_filedir=''): dir = exp_filedir if dir == '': dir = get_projectdirfromkey(key) + '/runtime' zip_name = os.path.basename(key) + '.zip' zip_path = os.path.join(dir, zip_name) try: z = zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) for dirpath, dirnames, filenames in os.walk(key): fpath = dirpath.replace(key, '') fpath = fpath and fpath + os.sep or '' for filename in filenames: z.write(os.path.join(dirpath, filename), fpath + filename) z.close() except Exception as e: log.error("下载zip用例异常:{}".format(e)) return (False, "{}".format(e)) return (True, zip_path)
def export_casexlsx(key, db, exp_filedir=''): export_dir = key if not os.path.isdir(export_dir): log.error("不支持导出一个文件中的用例:" + export_dir) return (False, "不支持导出一个文件中的用例:" + export_dir) basename = os.path.basename(export_dir) dir = exp_filedir if dir == '': dir = get_projectdirfromkey(export_dir) + '/runtime' os.mkdir(dir) if not os.path.exists(dir) else None export_file = os.path.join(dir, basename + '.xlsx') db.refresh_caseinfo(export_dir, "Force") cases = [] sql = "SELECT info_key,info_name,info_doc,info_tags FROM testcase WHERE info_key like '{}%' ;".format( key) res = db.runsql(sql) for i in res: (info_key, info_name, info_doc, info_tag) = i cases.append([info_key, info_name, info_doc, info_tag]) wb = Workbook() ws = wb.active ws.append(["导出&导入用例:"]) ws.append(["'_'用在文件名前后,表示用例文件:如 '_用例文件名_' 表示'用例文件名.robot'"]) ws.append(["'-'用来连接目录,没有此符号表示没有子目录:如 '目录1-目录11' 表示 '目录1/目录11'"]) ws.append(["每个sheet的第一列,是后面用例所在的用例文件名(.robot)"]) ws.append(["... ..."]) ws.append(["注意:通过xlsx文件导入用例,如果是自动化用例,且用例已经存在,则只更新doc和tag,不更新用例内容"]) ws.append(["... ..."]) ws.append(["此'sheet'页面,不会被导入"]) ws.append(["... ..."]) ws.append(["Export&Import Cases:"]) ws.append([ "'_'after the file name,Means a suite:'_SuiteName_' means 'SuiteName.robot'" ]) ws.append([ "'-'concat the dirs,no this sign no subdir:'dir1-dir11' means 'dir1/dir11'" ]) ws.append([ "First Column of each sheet,is the suite name of the case in this line(.robot)" ]) ws.append(["... ..."]) ws.append([ "Caution:Import cases from xlsx file,if it is Auto-case and it exists,then update doc and tag Only,Do not update Case content." ]) ws.append(["... ..."]) ws.append(["This 'sheet' ,Wont be imported."]) for c in cases: if not os.path.exists(c[0]): continue casecontent = getCaseContent(c[0], c[1]) suitename = os.path.basename(c[0]) tags = c[3].split(',') tags.remove('${EMPTY}') if '${EMPTY}' in tags else None category = "Auto" if "HAND" in tags or "Hand" in tags or 'hand' in tags: category = "Hand" casecontent = casecontent.replace(' ' * 4 + '#', '') sheetname = _get_ws(export_dir, c[0]) #print("Get sheete name :"+sheetname) # print(suitename,c[1],c[2],casecontent,c[3],category) if not sheetname in wb.sheetnames: ws = wb.create_sheet(sheetname) #ws = wb.active ws.append([ "Suite_Name", "Case_Name", "Case_Doc", "Case_Content", "Case_Tag", "Case_Type" ]) else: ws = wb[sheetname] #ws = wb.active ws.append([suitename, c[1], c[2], casecontent, c[3], category]) os.remove(export_file) if os.path.exists(export_file) else None wb.save(export_file) log.info("生成测试用例文件 {} 到目录 {}".format(export_dir, export_file)) return (True, export_file)
def get_case_data(app, path): projectdir = get_projectdirfromkey(path) os.environ["ROBOT_DIR"] = projectdir os.environ["PROJECT_DIR"] = projectdir suite = TestSuiteBuilder().build(path) children = [] if suite: # add library , make it can be open if it is a file. for i in suite.resource.imports: rsfile = i.name if rsfile.find("%{ROBOT_DIR}") != -1: rsfile = rsfile.replace("%{ROBOT_DIR}", projectdir) if rsfile.find("%{PROJECT_DIR}") != -1: rsfile = rsfile.replace("%{PROJECT_DIR}", projectdir) # do not show System Library or rs file cannot be found. if not os.path.exists(rsfile): continue if os.path.isfile(rsfile): fname = os.path.basename(rsfile) children.append({ "text": fname, "iconCls": "icon-library", "state": "open", "attributes": { "name": fname, "category": "case", "key": rsfile, } }) for t in suite.tests: status = app.config['DB'].get_casestatus(path, t.name) icons = 'icon-step' if status == 'FAIL': icons = 'icon-step_fail' if status == 'PASS': icons = 'icon-step_pass' children.append({ "text": t.name, "iconCls": icons, "state": "open", "attributes": { "name": t.name, "category": "step", "key": path, }, "children": [] }) ''' for v in suite.resource.variables: children.append({ "text": v.name, "iconCls": "icon-variable", "state": "open", "attributes": { "name": v.name, "category": "variable", "key": path, } }) for t in suite.tests: keys = [] for k in t.keywords: keys.append({ "text": k.name, "iconCls": "icon-keyword", "state": "open", "attributes": { "name": k.name, "category": "keyword", "key": path, } }) children.append({ "text": t.name, "iconCls": "icon-step", "state": "closed", "attributes": { "name": t.name, "category": "step", "key": path, }, "children": keys }) for v in suite.resource.keywords: children.append({ "text": v.name, "iconCls": "icon-user-keyword", "state": "open", "attributes": { "name": v.name, "category": "user_keyword", "key": path, } }) ''' return children