def execute_test_case(test_step_sheet_name, test_datas_dict): wb = ExcelUtil(test_data_file_path) print("要执行的sheet name:", test_step_sheet_name) if not test_step_sheet_name in wb.get_sheet_names(): # 判断了一下测试步骤sheet名字在不在 print("设定的测试用例sheet名称不存在!", test_step_sheet_name) return None try: test_result = [] test_steps_info = get_test_info(test_data_file_path, test_step_sheet_name) # print("所有的测试步骤:",test_steps_info) wb.set_sheet_by_name(test_result_sheet) wb.write_a_line_in_sheet(test_steps_info[0], fgcolor="CD9B9B") for test_data_dict in test_datas_dict: # 把列表中的每个子字典做遍历: if test_data_dict["是否执行"] != "y": continue test_steps_info = get_test_info(test_data_file_path, test_step_sheet_name) flag = True for test_step in test_steps_info[1:]: test_step_description = test_step[test_case_description_col_no] # 以下三行:将测试步骤的三部分取出来,赋值给三个变量 # 第一个是关键字、第二个是定位表达式,第三个表达式是操作值 keyword = test_step[keyword_col_no] locator = test_step[locator_col_no] value = test_step[value_col_no] print("***********:", test_data_dict, type(value), value) if value is not None and re.search(r"\$\{.*?\}", str(value)): key = re.search(r"\$\{(.*?)\}", value).group(1) value = test_data_dict[key] test_step[value_col_no] = value print("@@@@@@@@@@@@替换${%s}后的value是%s" % (key, value)) if locator is None and value is None: command = keyword + "()" elif locator is not None and value is None: command = "%s('%s')" % (keyword, locator) elif locator is None and value is not None: command = "%s('%s')" % (keyword, value) else: command = "%s('%s','%s')" % (keyword, locator, value) print("------------:", command) test_step[test_step_time_col_no] = TimeUtil( ).get_chinesedatetime() try: temp = eval(command) if "open_browser" in command: driver = temp test_step[test_step_result_col_no] = "成功" except AssertionError as e: print("断言失败") flag = False test_step[test_step_result_col_no] = "断言失败" take_pic(driver) wb.write_a_line_in_sheet(test_step, font_color="red") break except Exception as eak: print("突发异常") flag = False test_step[test_step_result_col_no] = "异常失败" take_pic(driver) # Action文件中的drive变量是"" wb.write_a_line_in_sheet(test_step, font_color="red") break wb.write_a_line_in_sheet(test_step, font_color="green") wb.set_sheet_by_name(test_result_sheet) wb.write_a_line_in_sheet(["", "", ""]) wb.save() test_time = TimeUtil().get_chinesedatetime() if flag: result = "成功" else: result = "失败" test_result.append({"测试时间": test_time, "测试结果": result}) except Exception as e: traceback.print_exc() return test_result
def execute_test_case(): # print(get_test_info(test_data_file_path,test_case_info_sheet)) wb = ExcelUtil(test_data_file_path) test_cases = get_test_info(test_data_file_path, test_case_info_sheet) for testcase in test_cases[1:]: test_step_sheet_name = testcase[test_steps_sheet_name_col_no] if "y" in testcase[is_test_executed_flag_col_no].lower(): # print("要执行的sheet name:",test_step_sheet_name) test_steps_info = get_test_info(test_data_file_path, test_step_sheet_name) # print("所有的测试步骤:",test_steps_info) wb.set_sheet_by_name(test_result_sheet) wb.write_a_line_in_sheet(test_steps_info[0], fgcolor="CD9B9B") #写测试结果表头 for test_step in test_steps_info[1:]: flag = True test_step_description = test_step[test_case_description_col_no] keyword = test_step[keyword_col_no] locator = test_step[locator_col_no] value = test_step[value_col_no] if "y" in test_step[is_test_step_executed_flag_col_no].lower(): if locator is None and value is None: command = keyword + "()" elif locator is not None and value is None: command = "%s('%s')" % (keyword, locator) elif locator is None and value is not None: command = "%s('%s')" % (keyword, value) else: command = "%s('%s','%s')" % (keyword, locator, value) print("------------:", command) test_step[test_step_time_col_no] = TimeUtil( ).get_chinesedatetime() try: temp = eval(command) if "open_browser" in command: driver = temp test_step[test_step_result_col_no] = "成功" except AssertionError as e: print("断言失败") flag = False test_step[test_step_result_col_no] = "断言失败" take_pic(driver) wb.write_a_line_in_sheet(test_step, font_color="red") break except Exception as eak: print("突发异常") flag = False test_step[test_step_result_col_no] = "异常失败" take_pic(driver) #Action文件中的drive变量是"" wb.write_a_line_in_sheet(test_step, font_color="red") break wb.write_a_line_in_sheet(test_step, font_color="green") wb.set_sheet_by_name(test_result_sheet) testcase[test_time_col_no] = TimeUtil().get_chinesedatetime() if flag is True: print("所有测试步骤执行成功") testcase[test_result_col_no] = "成功" wb.write_a_line_in_sheet(test_cases[0], fgcolor="CD9B9B") wb.write_a_line_in_sheet(testcase, font_color="green") else: print("测试步骤出现执行失败") testcase[test_result_col_no] = "失败" wb.write_a_line_in_sheet(test_cases[0], fgcolor="CD9B9B") wb.write_a_line_in_sheet(testcase, font_color="red") wb.save() else: print("不执行的sheet name:", test_step_sheet_name)
def execute_test_case_by_keyword(test_step_sheet_name): wb = ExcelUtil(test_data_file_path) print("要执行的sheet name:", test_step_sheet_name) if not test_step_sheet_name in wb.get_sheet_names(): # 判断了一下测试步骤sheet名字在不在 print("设定的测试用例sheet名称不存在!", test_step_sheet_name) return None try: test_result = {} test_steps_info = get_test_info(test_data_file_path, test_step_sheet_name) # print("所有的测试步骤:",test_steps_info) wb.set_sheet_by_name(test_result_sheet) wb.write_a_line_in_sheet(test_steps_info[0], fgcolor="CD9B9B") flag = True for test_step in test_steps_info[1:]: # 以下三行:将测试步骤的三部分取出来,赋值给三个变量 # 第一个是关键字、第二个是定位表达式,第三个表达式是操作值 keyword = test_step[keyword_col_no] locator = test_step[locator_col_no] value = test_step[value_col_no] if locator is None and value is None: command = keyword + "()" elif locator is not None and value is None: command = "%s('%s')" % (keyword, locator) elif locator is None and value is not None: command = "%s('%s')" % (keyword, value) else: command = "%s('%s','%s')" % (keyword, locator, value) print("------------:", command) test_step[test_step_time_col_no] = TimeUtil().get_chinesedatetime() try: temp = eval(command) if "open_browser" in command: driver = temp test_step[test_step_result_col_no] = "成功" except AssertionError as e: print("断言失败") flag = False test_step[test_step_result_col_no] = "断言失败" take_pic(driver) wb.write_a_line_in_sheet(test_step, font_color="red") break except Exception as eak: print("突发异常") flag = False test_step[test_step_result_col_no] = "异常失败" take_pic(driver) # Action文件中的drive变量是"" wb.write_a_line_in_sheet(test_step, font_color="red") break wb.write_a_line_in_sheet(test_step, font_color="green") wb.write_a_line_in_sheet(["","",""]) wb.save() test_time = TimeUtil().get_chinesedatetime() if flag: result ="成功" else: result = "失败" test_result = {"测试时间":test_time,"测试结果":result} except Exception as e: traceback.print_exc() return test_result
excel.set_sheet_by_name("测试结果") excel.write_a_line_in_sheet(contact_data[0],fgcolor="FF9D6F") # 从联系人的sheet中读取数据行 for contact_row in contact_data[1:]: contact_data_flag = True if contact_row[contact_data_valid_col_no].lower() == "y": name = contact_row[contact_name_col_no] email = contact_row[contact_email_col_no] mobile = contact_row[contact_mobile_col_no] info = contact_row[contact_info_col_no] assert_word = contact_row[assert_word_col_no] try: addContact(name, email, mobile, info, assert_word) except Exception as e: traceback.print_exc() pic_file_path = take_pic(driver) contact_row[pic_path_col_no] = pic_file_path contact_data_flag = False test_data_flag = False contact_row[test_end_time_col_no] = get_chinese_datetime() if contact_row[contact_data_valid_col_no].lower() != "y": contact_row[test_result_col_no] = "未执行" elif contact_data_flag: contact_row[test_result_col_no] = "成功" else: contact_row[test_result_col_no] = "失败" if contact_row[test_result_col_no] == "失败": excel.write_a_line_in_sheet(contact_row,font_color="red") else: excel.write_a_line_in_sheet(contact_row)
def run_test_case(): user_info = get_test_data(test_user_info_sheet) info("________________" * 5 + "\n" + "测试开始了!\n") for line in user_info[1:]: if line[execute_flag_col_no] is None: continue if "y" in line[execute_flag_col_no]: info("此用户数据要被执行:%s,%s,%s" % (line[username_col_no], line[password_col_no], line[test_data_sheet_name_col_no])) #print("此数据要被执行",line[username_col_no],line[password_col_no],line[test_data_sheet_name_col_no]) username = line[username_col_no] password = line[password_col_no] test_data_sheet = line[test_data_sheet_name_col_no] add_contacts_data = get_test_data(test_data_sheet) print("---:", add_contacts_data) driver = login(username, password) #调用一下登录逻辑 wb.set_sheet_by_name(test_result_sheet) wb.write_a_line_in_sheet(add_contacts_data[0], fgcolor="CD9B9B") #在测试结果页写了一个表头 flag = True for i in add_contacts_data[1:]: #遍历所有新建联系人的数据,在登录状态下进行新建操作 if i[test_data_execute_flag_col_no] is None: continue i[test_data_time_col_no] = TimeUtil().get_chinesedatetime() if "y" in i[test_data_execute_flag_col_no]: contact_name = i[contact_name_col_no] contact_email = i[contact_email_col_no] contact_star = i[contact_star_col_no] contact_mobile = i[contact_mobile_col_no] contact_comment = i[contact_comment_col_no] assert_word = i[assert_word_col_no] info("当前联系人数据行:%s,%s,%s,%s,%s," % (contact_name, contact_email, contact_star, contact_mobile, contact_comment)) try: add_person_info(driver, contact_name, contact_email, contact_star, contact_mobile, contact_comment) assert assert_word in driver.page_source i[test_data_result_no] = "成功" #设定一下测试结果列中的状态 info("当前测试数据执行成功了") except AssertionError: info(traceback.format_exc()) i[test_data_result_no] = "断言失败" # 设定一下测试结果列中的状态 flag = False info("当前测试数据执行断言失败了") take_pic(driver) except Exception as e: info(traceback.format_exc()) i[test_data_result_no] = "失败" #设定一下测试结果列中的状态 flag = False info("当前测试数据执行失败了") take_pic(driver) if i[test_data_result_no] == "成功": wb.write_a_line_in_sheet(i, font_color="green") else: wb.write_a_line_in_sheet(i, font_color="red") #wb.write_a_line_in_sheet(i) #把每一行测试数据的测试结果写入到文件中 if flag == True: line[test_user_info_result_col_no] = '成功' else: line[test_user_info_result_col_no] = '失败' line[test_time_col_no] = TimeUtil().get_chinesedatetime() wb.save() wb.write_a_line_in_sheet(user_info[0], "CD9B9B") if line[test_user_info_result_col_no] == '成功': info("当前测试用户 %s 的所有测试数据均执行成功了" % line[username_col_no]) wb.write_a_line_in_sheet(line, font_color="green") else: wb.write_a_line_in_sheet(line, font_color="red") info("当前测试用户 %s 的所有测试数据中有失败的测试用例!" % line[username_col_no]) wb.write_a_line_in_sheet([]) wb.write_a_line_in_sheet([]) wb.save() driver.quit() info("测试结束了!\n")