def _get_html_resultspath(self): """Get the results path for the html results file""" filename = file_Utils.getNameOnly(os.path.basename(self.junit_file)) filename = filename.split("_junit")[0] html_filename = filename + ".html" results_dir = os.path.dirname(self.junit_file) html_results_path = results_dir + os.sep + html_filename return html_results_path
def get_path(self): """ get the html results path """ filename = file_Utils.getNameOnly(os.path.basename(self.junit_file)) filename = filename.split("_junit")[0] html_filename = filename + ".html" if hasattr(self, 'givenPath'): html_results_path = self.givenPath + os.sep + html_filename else: results_dir = os.path.dirname(self.junit_file) html_results_path = results_dir + os.sep + html_filename return html_results_path
def get_execution_files(filepath, execution_dir, extn): """Get the execution files like resultfile, logfile etc""" filename = file_Utils.getFileName(filepath) nameonly = file_Utils.getNameOnly(filename) if extn.lower() == "res": fullpath = execution_dir + os.sep + nameonly + "_results" + "." + extn else: fullpath = execution_dir + os.sep + nameonly + '.' + extn if file_Utils.fileExists(fullpath): fullpath = file_Utils.addTimeDate(fullpath) return fullpath
def get_default_xml_datafile(filepath): """Get the default datafile for a testcase/testsuite file :Arguments: 1. filepath = full path of the input xml file """ inpdir = os.path.split(filepath)[0] filename = file_Utils.getFileName(filepath) nameonly = file_Utils.getNameOnly(filename) data_dir = os.sep.join(inpdir.split(os.sep)[:-1]) + os.sep + 'Data' def_datafile_path = data_dir + os.sep + nameonly + '_Data.xml' return def_datafile_path
def add_keyword_result(self, tc_timestamp, step_num, kw_name, status, kw_timestamp, duration, resultfile, impact, onerror, desc="", info="", tc_name="", tc_resultsdir=""): """form a keyword status dict with kw info and call function to build keyword elem""" if str(status).lower() == "true": status = "PASS" elif str(status).lower() == "false": status = "FAIL" keyword_items = { "type": "keyword", 'display': 'True', "step": step_num, "name": kw_name, "status": status, "timestamp": kw_timestamp, "time": duration, "resultfile": resultfile, "impact": impact, "onerror": onerror, "description": desc, "info": info } # if a failing status if encountered add a defects atribute to the keyword tag # and its value is the path to the defects file. failing_status = ['FAIL', 'EXCEPTION', 'ERROR'] if str(status).upper() in failing_status: defects_dir = os.path.dirname(tc_resultsdir) + os.sep + 'Defects' kw_resultfile_nameonly = file_Utils.getNameOnly( os.path.basename(resultfile)) defects_file = tc_name + "_" + kw_resultfile_nameonly + ".json" defects_filepath = defects_dir + os.sep + defects_file keyword_items['defects'] = defects_filepath self.add_property(name=kw_name, value="KEYWORD_DISCARD", elem_type="kw", timestamp=tc_timestamp, keyword_items=keyword_items)
def __init__(self, filepath, filetype, res_startdir=None, logs_startdir=None): """Constructor """ self.filepath = filepath self.filetype = filetype self.res_startdir = res_startdir self.logs_startdir = logs_startdir self.ws_execution = None #calculate filename and nameonly from file path self.filename = os.path.basename(filepath) self.nameonly = file_Utils.getNameOnly(self.filename) #resultsdir, resultfile, results_location self.resultfile, self.resultsdir, self.results_execdir = self.get_result_files() #logsdir, logfile, logs_location self.logfile, self.logsdir, self.logs_execdir = self.get_log_files()
def construct_mail_body(exec_type, abs_filepath, logs_dir, results_dir): """ construct e-mail body with Project, Logs/Results directory & Execution summary :Arguments: 1. exec_type - type of test(case/suite/project) 2. abs_filepath - full path of case/suite/project 3. logs_dir - full path of logs directory 4. results_dir - full path of results directory :Returns: 1. body - return mail body """ junit_result_file = os.path.join( results_dir, file_Utils.getNameOnly( file_Utils.getFileName(abs_filepath))) + "_junit.xml" junit_object = ExecutionSummary(junit_result_file) project_sum = junit_object.project_summary(junit_result_file) suite_tc_sum = junit_object.suite_summary(junit_result_file) suite_tc, body = "", "" body_arg = ('<html><body><p><b>{0}</b>{1}</p>' '<p><b>Logs directory:</b>{2}</p>' '<p><b>Results directory:</b>{3}</p>' '<p><b>Execution Summary:</b></p>' '<table cellspacing="10" cellpadding="0"><tr><td><b>Type</b>' '</td><td><b>Name</b></td><td><b>Status</b></td>' '<td><b>Path</b></td></tr>').format(exec_type, abs_filepath, logs_dir, results_dir) # complete html body that will be sent through mail if exec_type == 'Project: ': project = "" for proj in project_sum: project = project + ( '<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td></tr>\n'. format(proj[0], proj[1], proj[2], proj[3])) for value in suite_tc_sum: suite_tc = suite_tc + ( '<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td></tr>\n'. format(value[0], value[1], value[2], value[3])) body = body_arg + project + suite_tc + "</table></body></html>" elif exec_type == 'Test Suite: ' or exec_type == 'Test Case: ': for value in suite_tc_sum: suite_tc = suite_tc + ( '<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td></tr>\n'. format(value[0], value[1], value[2], value[3])) body = body_arg + suite_tc + "</table></body></html>" return body
def compose_send_email(exec_type, abs_filepath, logs_dir, results_dir, result, mail_on="per_execution"): """ compose and sends email from smtp server using input arguments as: :Arguments: 1. exec_type - type of test(case/suite/project) 2. abs_filepath - full path of case/suite/project 3. logs_dir - full path of logs directory 4. results_dir - full path of results directory 5. result - execution result 6. mail_on(optional) - it is to specify when to send an email Supported options below: (1) per_execution(default) (2) first_failure (3) every_failure """ resultconverted = { "True": "Pass", "False": "Fail", "ERROR": "Error", "EXCEPTION": "Exception" }.get(str(result)) subject = str(resultconverted) + ": " + file_Utils.getFileName( abs_filepath) body = [ exec_type + abs_filepath, "Logs directory: " + logs_dir, "Results directory: " + results_dir ] report_attachment = results_dir + os.sep + \ file_Utils.getNameOnly(file_Utils.getFileName(abs_filepath)) + ".html" # Temporary fix - HTML file can not be attached since it will be generated # only after the completion of the warrior execution. Creating html result # file at runtime will solve this. # KH. 2017-07-27 if mail_on in ["per_execution", "first_failure", "every_failure"]: files = {report_attachment} else: files = {} set_params_send_email(subject, body, files, mail_on)
def _link_to_defects(self, junit_node, filetype, **kwargs): """Link to defects of keywords""" rel_filepath = False status = junit_node.get("status") failing_status = ["fail", "error"] if filetype == "Keyword" and str(status).lower() in failing_status: tc_details = kwargs.get("tc_details", {}) tc_resultsdir = tc_details.get("tc_resultsdir", None) tc_name = tc_details.get("tc_name", None) defects_dir = tc_resultsdir + os.sep + "Defects" # step_num = junit_node.get("step") # kw_name = junit_node.get("name") kw_resultfile = junit_node.get("resultfile") kw_resultfile_nameonly = file_Utils.getNameOnly( os.path.basename(kw_resultfile)) defects_file = tc_name + "_" + kw_resultfile_nameonly + ".json" defects_filepath = defects_dir + os.sep + defects_file rel_filepath = os.path.relpath(defects_filepath, self.html_results_dir) elif filetype == "Testcase" and str(status).lower() != "pass": tc_resultsdir = junit_node.get("resultsdir") defects_dir = str(tc_resultsdir) + os.sep + "Defects" rel_filepath = os.path.relpath(defects_dir, self.html_results_dir) if rel_filepath: href_link = xml_Utils.create_element( "span", style="padding-left:8px; padding-right: 8px;") #href_link.text = "|" href = xml_Utils.create_element("a", href=rel_filepath) logs_icon = self._get_icon("defects") logs_icon.text = " " href.append(logs_icon) href_link.append(href) else: href_link = rel_filepath return href_link
def compose_send_email(exec_type, abs_filepath, logs_dir, results_dir, result, mail_on="per_execution"): """ compose and sends email from smtp server using input arguments as: :Arguments: 1. exec_type - type of test(case/suite/project) 2. abs_filepath - full path of case/suite/project 3. logs_dir - full path of logs directory 4. results_dir - full path of results directory 5. result - execution result 6. mail_on(optional) - it is to specify when to send an email Supported options below: (1) per_execution(default) (2) first_failure (3) every_failure """ resultconverted = { "True": "Pass", "False": "Fail", "ERROR": "Error", "EXCEPTION": "Exception", "RAN": "Ran" }.get(str(result)) subject = str(resultconverted) + ": " + file_Utils.getFileName( abs_filepath) body = construct_mail_body(exec_type, abs_filepath, logs_dir, results_dir) report_attachment = results_dir + os.sep + \ file_Utils.getNameOnly(file_Utils.getFileName(abs_filepath)) + ".html" if mail_on in ["per_execution", "first_failure", "every_failure"]: files = [report_attachment] else: files = [] set_params_send_email(subject, body, files, mail_on)
def create_failing_kw_json(self): """Create a json file each failing keyword """ status = False result_filename = file_Utils.getNameOnly(os.path.basename( self.xmlfile)) tc_name = result_filename.split("_results")[0] #tc_name = file_Utils.getNameOnly(os.path.basename(self.xmlfile)) #tree = xml_Utils.get_tree_from_file(self.xmlfile) tree = xml_Utils.getRoot(self.xmlfile) data_filepath = tree.find("Datafile").text failed = 0 #get_tc_details_frm_resultxml keyword_list = tree.findall("Keyword") if len(keyword_list) == 0: print_error( 'No Keywords found in resultfile of testcase {0}'.format( tc_name)) status = False else: for keyword in tree.findall("Keyword"): fail_list = [] if keyword.find("KeywordStatus").text.upper() in [ 'FAIL', 'EXCEPTION', 'ERROR' ]: step_num = keyword.find("Step").get('step_num') kw_resultfile = keyword.find("Resultfile").text kw_resultfile_nameonly = file_Utils.getNameOnly( os.path.basename(kw_resultfile)) failed += 1 #addd testcase details to json of failed keyowrds fail_list.append({'tc_name': tc_name}) fail_list.append({'step_num': str(step_num)}) fail_list.append( {'testcase_filepath': str(self.testcase_filepath)}) fail_list.append({'data_filepath': str(data_filepath)}) fail_list.append({'defectsdir': str(self.defectsdir)}) fail_list.append({'logsdir': str(self.logsdir)}) fail_list.append({'resultfile': str(self.resultfile)}) for node in keyword.iter(): text = self._get_text_forjson(node) if text: fail_list.append({node.tag: text}) else: continue if len(fail_list) > 0: json_file_name = "{0}{1}{2}_{3}.json".format( self.defectsdir, os.sep, tc_name, kw_resultfile_nameonly) # json_file_name = self.defectsdir + os.sep + tc_name +\ # '_step-'+str(step_num)+'_' + keyword.text +'.json' j_file = open(json_file_name, 'w') j_file.write(json.dumps(fail_list, indent=4)) j_file.close() status = True if failed == 0: print_warning( "There are no failed keywords in the testcase result xml file" ) status = False return status