class GetData: def __init__(self): self.operation_excel = OperationExcel() self.get_headers_cookies = GetHeadersCookies() self.get_config = ReadConfig() def get_sheet_names(self): """获取当前工作簿中所有工作表的名称""" return self.operation_excel.get_sheet_names() def get_sheet_data(self, sheet_name): """通过sheet页名称读取对应sheet页的数据""" return self.operation_excel.get_sheet_data(sheet_name) def get_sheet_lines(self): """获取当前sheet页的总行数""" return self.operation_excel.get_sheet_lines() def get_json_file_name(self, sheet_name): """获取json文件名称""" sheet_name = "../test_data/json_data/" + sheet_name + ".json" return sheet_name def get_save_result_path(self): '''获取结果文件路径''' return self.operation_excel.save_result_path def get_is_run(self, x): """获取case是否运行""" flag = None y = data_conf.get_run() run_value = self.operation_excel.get_cell_value(x, y) if run_value.lower() == 'yes': flag = True else: flag = False return flag def get_is_header(self, x): """是否携带headers""" y = data_conf.get_request_header() header = self.operation_excel.get_cell_value(x, y) if header == 'yes': return self.get_headers_cookies.get_headers_value() else: return None def get_is_cookie(self, x): """是否携带cookies""" y = data_conf.get_request_cookie() cookie = self.operation_excel.get_cell_value(x, y) if cookie == 'yes': return self.get_headers_cookies.get_cookies_value() return None def get_request_method(self, x): """获取请求方法""" y = data_conf.get_request_method() request_method = self.operation_excel.get_cell_value(x, y) return request_method def get_request_url(self, x): """获取请求地址""" y = data_conf.get_url() request_url = self.operation_excel.get_cell_value(x, y) base_url = self.get_config.get_http_config_var('baseurl') request_url = base_url + request_url return request_url def get_request_data(self, sheet_name, x): """获取请求数据""" y = data_conf.get_request_data() request_data_key = self.operation_excel.get_cell_value(x, y) if request_data_key == '': return None else: request_data = self.__get_data_for_json(sheet_name, request_data_key) return request_data def __get_data_for_json(self, sheet_name, request_data_key): """通过excel中关键字去获取json数据""" sheetName = self.get_json_file_name(sheet_name) op_json = OperationJson(file_path=sheetName) data = op_json.get_key_words(request_data_key) return data def get_expect_data(self, x): """获取预期结果数据""" y = data_conf.get_expect_result() expect_data = self.operation_excel.get_cell_value(x, y) if expect_data == '': return None else: return string_to_eval(x, y, expect_data) def get_module_name(self, x): """获取模块名称""" y = data_conf.get_name() module_name = self.operation_excel.get_cell_value(x, y) return module_name def write_real_result(self, sheet_name, row, value): """写测试结果到excel""" y = data_conf.get_real_results() self.operation_excel.write_value(sheet_name, row, y, value) def get_is_case_depend(self, x): """判断是否有case依赖""" y = data_conf.get_case_depend() case_depend = self.operation_excel.get_cell_value(x, y) if case_depend == "": return None else: return string_to_eval(x, y, case_depend) def get_depend_data(self, x): """获取响应结果中的依赖字段""" y = data_conf.get_data_depend() data_depend = self.operation_excel.get_cell_value(x, y) if data_depend == "": return None else: return string_to_eval(x, y, data_depend) def get_depend_filed(self, x): """获取数据依赖字段""" y = data_conf.get_filed_depend() filed_depend = self.operation_excel.get_cell_value(x, y) if filed_depend == "": return None else: return string_to_eval(x, y, filed_depend)
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
class GetData(object): def __init__(self): self.opera_excel = OperationExcel() self.sheet = self.opera_excel.get_data() '''获取excel的行数,就是我们的case个数''' def get_case_lines(self): try: return self.opera_excel.get_lines() except Exception as e: logging.info("错误为:", e) '''获取是否执行''' def get_is_run(self, row): try: flag = None col = get_run() run_model = self.opera_excel.get_cell_value(row, col) if run_model == 'y': flag = True else: flag = False return flag except Exception as e: logging.info("错误为:", e) '''获取接口描述''' def get_api_msg(self, row): try: col = get_request_name() api_msg = self.opera_excel.get_cell_value(row, col) return api_msg except Exception as e: logging.info("错误为:", e) '''获取接口名称''' def get_api_name(self, row): try: col = get_api_name() api_name = self.opera_excel.get_cell_value(row, col) return api_name except Exception as e: logging.info("错误为:", e) '''是否携带header''' def get_request_headers(self, row): try: col = get_header() header = self.opera_excel.get_cell_value(row, col) if header == '': return None return header except Exception as e: logging.info("错误为:", e) '''是否有header''' def is_header(self, row): try: col = get_header() header = self.opera_excel.get_cell_value(row, col) if header != 'n': header = json.loads(header) return header # return self.opera_excel.get_cell_value(row,col) elif header == 'w': return 'w' else: return None except Exception as e: logging.info("错误为:", e) '''获取请求方式''' def get_request_method(self, row): try: col = get_run_way() request_method = self.opera_excel.get_cell_value(row, col) return request_method except Exception as e: logging.info("错误为:", e) '''获取url''' def get_request_url(self, row): try: col = get_url() url = self.opera_excel.get_cell_value(row, col) return url except Exception as e: logging.info("错误为:", e) '''请求数据''' def get_request_data(self, row): try: col = get_data() data = self.opera_excel.get_cell_value(row, col) if data == '': return None return data except Exception as e: logging.info("错误为:", e) '''通过获取关键字拿到data数据''' def get_data_for_json(self, row): try: opera_json = OperationJson() request_data = opera_json.get_data(self.get_request_data(row)) return request_data except Exception as e: logging.info("错误为:", e) '''获取要提取的字段的值''' def get_depend_value(self, row): try: col = get_depend_value() depend_value = self.opera_excel.get_cell_value(row, col) if depend_value == '': return None return depend_value except Exception as e: logging.info("错误为:", e) '''获取预期结果''' def get_expect_data(self, row): try: col = get_expect() expect = self.opera_excel.get_cell_value(row, col) if expect == '': return None return expect except Exception as e: logging.info("错误为:", e) '''获取http状态码''' def get_http_code_data(self, row): try: col = get_http_code() http_code = self.opera_excel.get_cell_value(row, col) return http_code except Exception as e: logging.info("错误为:", e) '''获取待替换的值''' def get_waiting_replace_key(self, row): try: col = get_waiting_replace_key() wait_rep_key = self.opera_excel.get_cell_value(row, col) if wait_rep_key == '': return None return wait_rep_key except Exception as e: logging.info('错误为:', e) '''获取实际替换的值''' def get_actual_replace_key(self, row): try: col = get_actual_replace_key() act_rep_key = self.opera_excel.get_cell_value(row, col) if act_rep_key == '': return None return act_rep_key except Exception as e: logging.info('错误为:', e) '''写入数据''' def write_result(self, row, value): try: col = get_result() self.opera_excel.write_value(sheet=self.sheet, content=value, rowNo=row, colsNo=col) except Exception as e: logging.info("错误为:", e) '''获取依赖返回数据的key''' def get_depend_key(self, row): try: col = get_data_depend() depent_key = self.opera_excel.get_cell_value(row, col) if depent_key == "": return None else: return depent_key except Exception as e: logging.info("错误为:", e) def is_depend(self, row): # 是否有依赖 try: col = get_case_depend() depend_case_id = self.opera_excel.get_cell_value(row, col) if depend_case_id == "": return None else: return depend_case_id except Exception as e: logging.info("错误为:", e) '''获取数据依赖字段''' def get_depend_field(self, row): try: col = get_field_depend() data = self.opera_excel.get_cell_value(row, col) if data == "": return None else: return data except Exception as e: logging.info("错误为:", e) '''获取请求方式''' def get_set_key(self, row): try: col = get_set_key() request_method = self.opera_excel.get_cell_value(row, col) return request_method except Exception as e: logging.info("错误为:", e)
class GetData(object): def __init__(self, filename=None, sheet_id=None): self.opera_excel = OperationExcel(filename, sheet_id) def get_case_lines(self): return self.opera_excel.get_lines() def get_is_run(self, row): flag = None col = data_config.global_val.get_run() run_model = self.opera_excel.get_cell_value(row, int(col)) if run_model == 'yes': flag = True else: flag = False return flag def is_cookie(self, row): col = data_config.global_val.get_cookie() cookie = self.opera_excel.get_cell_value(row, int(col)) return cookie def is_header(self, row): col = data_config.global_val.get_header() header = self.opera_excel.get_cell_value(row, int(col)) if header == 'yes': return data_config.global_val.get_header_value() else: return None def get_request_method(self, row): col = data_config.global_val.get_request_way() request_method = self.opera_excel.get_cell_value(row, int(col)) return request_method def get_url(self, row): col = data_config.global_val.get_url() url = self.opera_excel.get_cell_value(row, int(col)) return url def get_request_data(self, row): col = data_config.global_val.get_data() data = self.opera_excel.get_cell_value(row, int(col)) if data == '': return None else: return data def get_data_for_json(self, row): self.opera_json = OperationJson() data_json = self.opera_json.get_data(self.get_request_data(row)) return data_json def get_expect(self, row): col = data_config.global_val.get_expect() expect = self.opera_excel.get_cell_value(row, int(col)) if expect == '': return None else: return expect def write_result(self, row, value): col = data_config.global_val.get_result() self.opera_excel.write_value(row, int(col), value) def get_data_depend(self, row): col = data_config.global_val.get_data_depend() data_depend = self.opera_excel.get_cell_value(row, int(col)) if data_depend == "": return None else: return data_depend def is_depend(self, row): col = data_config.global_val.get_case_depend() case_depend = self.opera_excel.get_cell_value(row, int(col)) if case_depend is not None: return case_depend else: return None def get_field_depend(self, row): col = data_config.global_val.get_field_depend() field_depend = self.opera_excel.get_cell_value(row, int(col)) if field_depend is not None: return field_depend else: return None