class DependentData: def __init__(self, case_id): self.case_id = case_id self.oper_excel = OperationExcel() self.method = RunMethod() self.data = GetData() # 通过case_id去获取依赖case_id的整行数据 def get_case_line_data(self): rows_data = self.oper_excel.get_rows_data(self.case_id) return rows_data #执行依赖测试,获取结果 def run_dependent(self): row_num = self.oper_excel.get_row_num(self.case_id) request_data = self.data.get_data_value(row_num) header = self.data.get_request_header(row_num) method = self.data.get_request_method(row_num) url = self.data.get_request_url(row_num) if (header == 'write_Cookies'): header = { 'Content-Type': 'application/json', "X-Lemonban-Media-Type": "lemonban.v2" } data = json.dumps(request_data) res = self.method.run_main(method, url, data, header, params=data) else: res = self.method.run_main(method, url, request_data, header, params=request_data) return res #获取依赖字段的响应数据:通过执行依赖测试case来获取响应数据,响应中某个字段数据作为依赖key的value def get_value_for_key(self, row): newdepend = '' #获取依赖的返回数据key depend_data = self.data.get_depend_key(row) #执行依赖case返回结果 response_data = self.run_dependent() if ',' in depend_data: newdepend_data = depend_data.split(',') if len(newdepend_data) > 1: for index, item in enumerate(newdepend_data): newdepend += str([ match.value for match in parse(item).find(response_data) ][0]) + "," return newdepend else: return [ match.value for match in parse(depend_data).find(response_data) ][0]
class DependentData: def __init__(self,case_id): self.case_id = case_id self.opera_excel = OperationExcel() self.data =GetData() # 根据依赖caseid去获取该caseid整行数据 def get_case_line_data(self): rows_data = self.opera_excel.get_rows_data(self.case_id) return rows_data # 执行依赖测试,获取结果 def run_dependent(self): run_method = RunMethod() # 拿到行号,获取get_data等需要行号获取 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) # if header == 'yes': method = self.data.get_request_method(row_num) url = self.data.get_request_url(row_num) res = run_method.run_main(method,url,request_data,header) # return res return json.loads(res) # 根据依赖的key去获取执行依赖测试case的响应,然后返回 def get_data_for_key(self,row): depend_data = self.data.get_depend_key(row) response_data = self.run_dependent() # print("------------->>>>>>") # print(depend_data) # print(response_data) # 响应值为字符串,需要处理。根据层级关系去拿 json_exe = parse(depend_data) madle = json_exe.find(response_data) # for i in madle: i是字段类型 # i.value # course:[0]:out_trade_no return [math.value for math in madle][0]
class DependdentData: def __init__(self, case_id): self.case_id = case_id self.opera_excel = OperationExcel() self.data = GetData() #通过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): run_method = RunMethod() 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 = run_method.run_main(method,url,request_data) res = None if header == "yes": op_json = OperetionJson('./dataconfig/cookie.json') cookie = op_json.get_data('apsid') res = run_method.run_main(method, url, request_data, cookie) else: res = run_method.run_main(method, url, request_data) return json.loads(res) #根据依赖的key去获取执行依赖测试case的响应,然后返回 def get_data_for_key(self, row): 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]
class DependentData: def __init__(self, case_id): self.case_id = case_id self.opera_excel = OperationExcel() self.run_method = RunMethod() self.data = GetData() #通过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): row_num = self.opera_excel.get_row_num(self.case_id) request_data = self.data.get_data_for_json(row_num) header = self.data.get_header_for_json(row_num) url = self.data.get_url(row_num) method = self.data.get_requst_method(row_num) # print(row_num, request_data, header, url, method) res = self.run_method.run_main(method, url, request_data, header[0]) return json.loads(res) #根据依赖的key去获取依赖测试case的请求值且返回 def get_data_for_key(self, row): depend_data = self.data.get_depend_key(row) #打印正则 # print(depend_data) response_data = self.run_dependent() json_exe = parse(depend_data) madle = json_exe.find(response_data) #打印通过正则而得到的结果 # print(madle) return [math.value for math in madle][0]
class DependentData: def __init__(self, case_id): self.case_id = case_id self.opera_excel = OperationExcel() xiao_iron = 'E:/xt/xironbardepend/xironbackend/' self.data = GetData(xiao_iron + 'dataconfig/interfacebar1.xlsx', 2) self.read_ini = ReadIni() # 通过 caseid 去获取该 caseID 的整行数据 def get_case_line_data(self): rows_data = self.opera_excel.get_rows_data(self.case_id) return rows_data # 执行依赖测试,获取结果 def run_dependent(self): run_method = RunMethod() row_num = self.opera_excel.get_row_num(self.case_id) request_data = self.data.get_data_for_json(row_num) header = self.data.get_header(row_num) method = self.data.get_request_method(row_num) url = self.read_ini.get_value() + self.data.get_url(row_num) res = run_method.run_main(method, url, request_data, header) return res # 根据依赖的key去获取执行依赖测试case的响应,然后返回 def get_data_for_key(self, row): depend_data = self.data.get_depend_key(row) is_pre_start = depend_data.startswith('pre:') if is_pre_start: depend_key = depend_data.split(':')[1] return self.get_pre_data(depend_key) else: response_data = self.run_dependent() res = self.dict_get(response_data, depend_data, None) return res def get_pre_data(self, depend_key): row_num = self.opera_excel.get_row_num(self.case_id) pre_data = json.loads(self.data.get_pre_data( row_num)) # pre_data 是直接从 excel中读取,不用通过 json的 key获取 res = pre_data[depend_key] return res # 获取字典中的objkey对应的值,适用于字典嵌套 # dict:字典 objkey:目标key default:找不到时返回的默认值 def dict_get(self, _dict, key, default): tmp = _dict for k, v in tmp.items(): if k == key: return v else: if isinstance(v, list): for item in v: ret = self.dict_get(item, key, default) if ret is not default: return ret elif isinstance(v, dict): ret = self.dict_get(v, key, default) if ret is not default: return ret return default