class OpValue(): def __init__(self): self.getcfg = GetConf() self.comtool = CommonUtil() # 传递含有变量的信息,进行替换 变量值 def replace_value(self, data): # data = {"sourceType": "0", "userName": "******", "password": "******"} # data = {"sourceType": "0", "userName": "******", "password": "******"} nn = self.comtool.res_find(str(data)) ##找到所有变量 data_ret = data for key in nn: try: getv = self.getcfg.read_conf_value_toexcel(key) # 获取key的值 data_ret = self.comtool.res_value_replace( key, getv, str(data_ret)) # 替换data的赋值给data_ret except Exception as e: print("替换excel的变量失败。 确认%s变量存在。跳过替换。" % key, e) pass return str(data_ret).replace("'", '"') def save_value(self): pass
def __init__(self,logs_file=None,logs_name=None,logs_level=None): get_conf = GetConf() log_path = get_conf.read_value('common', 'logpath') ## 日志文件名称 = logs目录 + 当前时间+扩展名 current_time = datetime.datetime.now().strftime("%Y-%m-%d") logfile_loc = os.path.join(log_path, current_time + ".log") loglevel_loc = get_conf.read_value('common', 'loglevel') self.log_file=logfile_loc self.log_level=loglevel_loc self.log_name=__file__ if logs_file: self.log_file = logs_file #扩展名 配置文件 if logs_name: self.log_name = logs_name #参数 if logs_level: self.log_level = logs_level # 配置文件 # 设置logger名称 self.logger = logging.getLogger(self.log_name) # 设置log级别 self.logger.setLevel(log_l[str(self.log_level).lower()]) #logging.INFO #判断handlers是否存在 if not self.logger.handlers: # 输出控制台 fh_stream = logging.StreamHandler() fh_stream.setLevel(log_l[self.log_level]) #日志格式 formatter = logging.Formatter('%(asctime)s %(name)s line:%(lineno)s [%(levelname)s]:%(message)s ') fh_stream.setFormatter(formatter) # 写入文件 utf-8 防止中文乱码 fh_file = logging.FileHandler(self.log_file,encoding="utf-8") fh_file.setLevel(log_l[self.log_level]) fh_file.setFormatter(formatter) # 添加handler self.logger.addHandler(fh_stream) self.logger.addHandler(fh_file)
class SaveBodyValue(): KEY_ERROR_RET = "NOF_FOUND_KEY" ''' 从响应体中,保存需要被保存的变量到conf.ini文件里 ''' def __init__(self): self.getconf = GetConf() self.comtool = CommonUtil() def save_value_to_conf(self, get_body_value, current_result): ''' 从json文件中,提取值,最终返回字典 :param save_body_value: 传递一个字典(“user":"******" :param current_result: 需要从该内容中 匹配提取 :return: dictvalue "user":"******" ''' current_result = current_result #所有内容 dictvalue = {} if not isinstance(current_result, dict): try: current_result = json.loads(current_result) except Exception as e: traceback.print_exc() return None if not isinstance(get_body_value, dict): try: get_body_value = json.loads(get_body_value) except Exception as e: traceback.print_exc() return None for keyname in dict(get_body_value): save_key = keyname get_save_value_jmespath = get_body_value[save_key] save_value = self.comtool.json_search(get_save_value_jmespath, current_result) dictvalue[save_key] = save_value print("保存的k,v:", save_key, save_value) if not self.getconf.write_conf_value(save_key, save_value): return False return True
def __init__(self): self.getconf = GetConf() self.comtool = CommonUtil()
def __init__(self): self.getconf = GetConf()
def __init__(self, filename=None, sheet_id=None): self.opera_excel = OperationExcel(filename, sheet_id) self.com_util = CommonUtil() self.op_excel_value = op_excel_value.OpValue() self.get_conf = GetConf()
class GetData(): def __init__(self, filename=None, sheet_id=None): self.opera_excel = OperationExcel(filename, sheet_id) self.com_util = CommonUtil() self.op_excel_value = op_excel_value.OpValue() self.get_conf = GetConf() def trans_value(self, v): return self.com_util.value_trans(v) # 获取当前 sheet name def get_current_sheet_name(self): return self.trans_value(self.opera_excel.get_sheet_name()) # 获取excel行数 def get_case_lines(self): return self.trans_value(self.opera_excel.get_sheet_rows_num()) # 通过caseidname 获取行号 def get_case_row_by_idname(self, idname): col_num = 1 for i in self.opera_excel.get_col_value(1): if idname == i: return col_num col_num += 1 return False # 获取case id def get_case_id_name(self, row): col = data_config.get_id_col() case_id = self.opera_excel.get_cell_value(row, col) if case_id: return self.trans_value(case_id) else: return "" #获取模块名称 def get_mod_name(self, row): col = data_config.get_modname_col() modname = self.opera_excel.get_cell_value(row, col) return self.trans_value(modname) #获取api接口名称 def get_apiname(self, row): col = data_config.get_apiname_col() apiname = self.opera_excel.get_cell_value(row, col) return self.trans_value(apiname) #获取url 请求地址 def get_url(self, row): col = data_config.get_url_col() url = self.opera_excel.get_cell_value(row, col) if url: return self.trans_value(url) else: return "" def get_url_final(self, row): url = self.get_url(row) if url: url = str( self.get_conf.read_conf_value_toexcel("urlprefix")).replace( '"', '') + str(url) return url # 是个否运行 def get_is_run(self, row): flag = None col = data_config.get_run_col() is_run = self.opera_excel.get_cell_value(row, col) if str(is_run).lower() == "yes" or str(is_run).lower() == "y": flag = True else: flag = False return self.trans_value(flag) # 获取请求的方式 def get_request_method(self, row): col = data_config.get_request_method_col() method = self.opera_excel.get_cell_value(row, col) return self.trans_value(method) #获取 cookie值 def get_is_cookie(self, row): col = data_config.get_cookie_col() is_cookie = self.opera_excel.get_cell_value(row, col) return self.trans_value(is_cookie) # # 是否携带 header # def get_is_header(self, row): # col = data_config.get_header_col() # is_header = self.opera_excel.get_cell_value(row, col) # if str(is_header).lower() == "yes": # return self.trans_value(data_config.get_header_info()) # else: # return None ##获取header值 def get_header_info(self, row): col = data_config.get_header_col() headerinfo = self.opera_excel.get_cell_value(row, col) if str(headerinfo) == "" or headerinfo == None: return {} else: return json.loads(headerinfo) # 获取 dependent caseid name def get_dependent_caseid(self, row): col = data_config.get_dependent_caseid_col() caseid = self.opera_excel.get_cell_value(row, col) if caseid == "": return None else: return caseid # 获取dependent data 内容 def get_dependent_data(self, row): col = data_config.get_dependent_data_col() dependent_data = self.opera_excel.get_cell_value(row, col) return dependent_data # 获取depent filed字段内容 def get_dependent_filed(self, row): col = data_config.get_dependent_filed_col() filed_data = self.opera_excel.get_cell_value(row, col) return filed_data # 获取请求数据 def _get_request_data(self, row): col = data_config.get_request_data_col() data = self.opera_excel.get_cell_value(row, col) if data == "": return None return self.trans_value(data) # 通过关键字拿到 请求体,request_data数据 def get_request_data_final(self, row): request_data = self._get_request_data(row) #请求数据,关键字 if str(request_data).startswith("json_"): #如果json_开头则进行json获取 opera_json = OperationJson() request_data = opera_json.get_value(request_data) return self.trans_value(request_data) elif str(request_data) != "": request_data = self.op_excel_value.replace_value( request_data) # 更新替换excel请求中带的变量 return self.trans_value( request_data) #如果不为空,不是json_开头,那么就替换变量再返回数据 else: return None #没有请求数据,则返回None #保存响应中的指定值(json获取方式) def get_save_value(self, row): col = data_config.get_save_value_col() savevalue = self.opera_excel.get_cell_value(row, col) if str(savevalue) == "" or savevalue == None: return None else: return str(savevalue) # 获取预期响应结果 def _get_expect_result(self, row): col = data_config.get_expect_result_col() expect_value = self.opera_excel.get_cell_value(row, col) if expect_value == "": return None return self.trans_value(expect_value) def get_expect_result_final(self, row): ''' 获取预期结果,返回数组类型 :param row: :return: type:list ''' expect_result = self._get_expect_result(row) if str(expect_result).startswith("json_"): # 如果json_开头则进行json获取 opera_json = OperationJson() expect_data = opera_json.get_value(expect_result) print('通过json配置的预期结果断言###值为:', expect_data) return list(str(self.trans_value(expect_data)).split(',')) elif str(expect_result) != "": expect_data = self.op_excel_value.replace_value( expect_result) # 更新替换excel请求中带的变量 return list(str(self.trans_value(expect_data)).split( ',')) # 如果不为空,不是json_开头,那么就替换变量再返回数据 elif str(expect_result) == "" or expect_result == None: return [] # 没有请求数据,则返回None else: return ["ERROR_FOUND_EXPECT_RESULT"] #获取预期响应code def get_expect_code(self, row): col = data_config.get_except_code_col() expect_code = self.opera_excel.get_cell_value(row, col) if expect_code == "": return 200 return self.trans_value(expect_code) #获取数据库校验语句 def get_dbcheck_sql(self, row): col = data_config.get_dbcheck_col() dbchecksql = self.opera_excel.get_cell_value(row, col) if str(dbchecksql) != "" or str(dbchecksql) != None: return str(dbchecksql) else: return None #获取备注信息 def get_comment_info(self, row): col = data_config.get_comment_col() commentinfo = self.opera_excel.get_cell_value(row, col) if str(commentinfo) != "" or str(commentinfo) != None: return str(commentinfo) else: return "" # 判断是否需要 依赖case def get_is_dependent(self, row): data = self.get_dependent_caseid(row) if data: return True else: return False #写 当前结果到excel def write_current_result(self, row, value): col = data_config.get_current_result_col() writevalue = self.opera_excel.write_cell_value(row, col, value) if writevalue: return True else: return False #写 当前响应code到excel def write_current_code(self, row, value): col = data_config.get_current_code_col() writevalue = self.opera_excel.write_cell_value(row, col, value) if writevalue: return True else: return False #写 测试结果到excel def write_test_result(self, row, value): col = data_config.get_test_result_col() ## row,col,value,result(通过value来判断pass,fail,else 用于写不同的颜色) writevalue = self.opera_excel.write_cell_value(row, col, value, result=value) if writevalue: return True else: return False