class DependentData: def __init__(self, case_id): self.case_id = case_id self.opera_excel = OperationExcel() self.data = GerData() #通过case_id去获取该case_id的整行数据 def get_case_line_data(self): rows_data = self.opera_excel.get_rows_data(self.case_id) return rows_data #执行依赖测试,获取结果 def run_dependent(self): httpclient = HttpClient() row_num = self.opera_excel.get_row_num(self.case_id) request_data = self.data.get_data_for_json(row_num) #header = self.data.is_header(row_num) method = self.data.get_request_method(row_num) url = self.data.get_request_url(row_num) result = httpclient.request(method, url, request_data) return json.loads(result) #根据依赖的key去获取执行依赖测试case的响应,然后返回 def get_data_for_key(self, row): depend_data = self.data.get_depend_key(row) response_data = self.run_dependent() print(type(depend_data), "depend_data:", depend_data) print(type(response_data), "response_data:", response_data) json_exe = parse(depend_data) madle = json_exe.find(response_data) return [math.value for math in madle][0]
class DependentData: def __init__(self,case_id): self.case_id=case_id self.opera_excel=OperationExcel() self.datas=GetData() self.gettime=GetTime() def get_case_line_data(self): rows_data=self.opera_excel.get_rows_data(self.case_id) #根据caseid获取excel整行数据 def run_dependent(self): row_num=self.opera_excel.get_rows_num(self.case_id) url = self.datas.get_request_url(row_num) # print(url) method = self.datas.get_request_method(row_num) # print(method) headers = self.datas.get_header_for_json(row_num) # print(headers) # print(type(headers)) data = self.datas.get_data_for_json(row_num) # print(data) # 获取预期结果值 #excepts = self.datas.get_except_val(row_num) # 延迟发送 times = self.datas.get_delaytime_mm(row_num) self.gettime.delaytime(times) # 获取cookies cookies = self.datas.get_is_cookie(row_num) if cookies != None: if cookies != "write": # 发送请求 # print(headers) res = RunQuest(url, method, data, headers, cookies).res return res res = RunQuest(url, method, data, headers).res #res=json.loads(res) return res #只根据依赖请求 def get_is_req_sucess(self): res=self.run_dependent() time.sleep(1) print(self.case_id+"依赖请求:"+str(res)) #根据依赖的key 获取依赖的case的响应中的值 def get_data_for_key(self,row): depend_data=self.datas.get_depend_key(row) #获取响应依赖的数据 depend_data=depend_data.split(":") res_data=self.run_dependent() #获取响应的值 if isinstance(res_data, dict): # 判断是否请求成功 # json_exe=parse(depend_data) # madle=json_exe.find(res_data) # return [math.value for math in madle][0] for_key_value=self.get_for_key(res_data,depend_data) if for_key_value !=None: return for_key_value else: return None else: return None def get_for_key(self,res_data, *keys): res_datas = res_data for ks in keys: for k in ks: #print(k) res_datas = self.get_for_key_value(res_datas, k) # print(res_datas) # print(res_datas) return res_datas #判断是否存在 def get_for_key_value(self,res_data, depend_data): try: json_exe = parse(depend_data) # print(json_exe) madle = json_exe.find(res_data) # print(madle) # print(type(madle)) datas = [math.value for math in madle][0] #print(datas) # print(type(datas)) return datas except: return None
class DependentData(object): def __init__(self, case_id): self.case_id = case_id self.opera_excel = OperationExcel() self.data = GetData() self.run_method = RunMethod() '''通过case_id去获取该case_id的整行数据''' def get_case_line_data(self): try: rows_data = self.opera_excel.get_rows_data(self.case_id) return rows_data except Exception as e: logging.info("错误为:", e) '''执行依赖测试,获取结果''' def run_dependent(self): try: row_num = self.opera_excel.get_row_num(self.case_id) request_data = self.data.get_data_for_json(row_num) header = self.data.is_header(row_num) method = self.data.get_request_method(row_num) url = self.data.get_request_url(row_num) res = self.run_method.run_main(method, url, request_data, header) # return res return json.loads(res) except Exception as e: logging.info("错误为:", e) '''根据依赖的key去获取执行依赖测试case的响应, 然后返回,都要为字典格式,所以将上面的run_dependent函数的结果以字典的格式返回''' def get_data_for_key(self, row): try: # rows.[0].productID # depend_data是依赖的返回数据 rows.[0].productID depend_data = self.data.get_depend_key(row) response_data = self.run_dependent() json_exe = parse(depend_data) madle = json_exe.find(response_data) return [math.value for math in madle][0] except Exception as e: logging.info("错误为:", e) dic = {} # 把依赖数据保存到文件里 def save_depend_value(self, res, depend_data, dict_key): res = json.loads(res) depend_data = list(depend_data.split(",")) dict_key = list(dict_key.split(",")) for i in range(len(dict_key)): try: json_exe = parse(depend_data[i]) madle = json_exe.find(res) value = [math.value for math in madle][0] with open(depend_data_path, 'r', encoding='utf-8') as f: depend_value = f.read() if depend_value == "": depend_value = "{}" depend_value = json.loads(depend_value) key = dict_key[i] depend_value[key] = value except IndexError as e: logging.info("错误为:", e) finally: try: with open(depend_data_path, 'w', encoding='utf-8') as f: depend_value = json.dump(depend_value, f) f.flush() except Exception as e: logging.info("错误为:", e)