def http_request(self, interface_url, headerdata, interface_parm, request_type): """ :param interface_url: 接口地址 :param headerdata: 请求头文件 :param interface_parm: 接口请求参数 :param request_type: 请求类型 :return: 字典形式结果 """ try: if request_type == 'get' or request_type == 'GET': result = self.__http_get(interface_url, headerdata, interface_parm) MyLog.debug('用例发送消息URL:%s\n' '用例发送消息头:%s\n' '用例发送消息体:%s\n' % (interface_parm, headerdata, interface_parm)) elif request_type == 'post' or request_type == 'POST': result = self.__http_post(interface_url, headerdata, interface_parm) MyLog.debug('用例发送消息URL:%s\n' '用例发送消息头:%s\n' '用例发送消息体:%s\n' % (interface_parm, headerdata, interface_parm)) else: result = {'code': '1000', 'message': '请求类型错误', 'data': []} except Exception as e: # traceback.print_exc() MyLog.error(e) return result
def setvar(self, result_interface): """ :param result_interface: 接口返回参数 :return: """ if self.extra_key_list_str and \ self.extra_key_list_str.startswith("[") and \ self.extra_key_list_str.endswith("]"): try: extra_key_list = eval(self.extra_key_list_str) except Exception as e: MyLog.error(e) if result_interface and result_interface.startswith( '{') and isinstance(result_interface, str): temp_result_interface = json.loads( result_interface) # 将字符串类型转换为字典类型 for extra_key in extra_key_list: value_list = get_target_value(extra_key, temp_result_interface, tmp_list=[]) if len(value_list) == 1: for value in value_list: if isinstance(value, str): # 判断value是否是字符串 self.cache.set(extra_key, '\'' + value + '\'') elif isinstance(value, int): # 判断value是不是int类型 self.cache.set(extra_key, value) elif isinstance(value, dict): # 判断value是不是字典类型 self.cache.set(extra_key, value) else: MyLog.error('未处理的数据类型', value) MyLog.debug('接口返回值入参成功%s %s' % (extra_key, value)) elif len(value_list) == 0: MyLog.error('缓存数据设置错误,未找到对应返回值%s' % extra_key) elif len(value_list) > 1: # MyLog.error('value_list',value_list) MyLog.error('缓存数据设置错误,存在多个值') else: MyLog.error('接口返回值类型错误,无法将参数存入缓存') else: MyLog.debug('接口无缓存参数')
def getvar(self): """ 获取参数 :return:{key1:value1,key2:value2,...} """ sign = self.match_var() cache_value = {} if sign: for key in sign: value = self.cache.get(key) if value is not None: MyLog.debug('获取其他接口入参参数成功,参数名称为%s,参数值为%s' % (sign, value)) cache_value[key] = value # return self.cache.get(sign) else: MyLog.error('获取参数异常,缓存中没有%s的值' % sign) return cache_value else: MyLog.debug('该接口无${}标识') return None
def replace(self): """ :return: 返回新赋值的字典 """ replace_dict = {} functions_tuple_list = self.match_function() if self.match_var() is None and functions_tuple_list is None: MyLog.debug('没有要替换的变量或函数返回值') return self.params_interface if self.match_var(): # 进行变量赋值替换 params_interface_json = json.dumps(self.params_interface, ensure_ascii=False) _temp_json = params_interface_json _new_params_dict = self.getvar() for _k, _v in _new_params_dict.items(): _temp_json = _temp_json.replace('$' + _k, _v) self.params_interface = json.loads(_temp_json) MyLog.debug('已进行变量赋值替换:%s' % self.params_interface) functions_tuple_list = self.match_function() print('functions_tuple_list', functions_tuple_list) if functions_tuple_list: function_results_dict = self.get_function_return( functions_tuple_list) params_interface_json = json.dumps(self.params_interface, ensure_ascii=False) print(function_results_dict) _temp_json = params_interface_json for need_to_replace_str in function_results_dict: _temp_json = _temp_json.replace( '${' + need_to_replace_str['function_name'] + '(' + need_to_replace_str['args'] + ')' + '}', str(need_to_replace_str['result'])) self.params_interface = json.loads(_temp_json) MyLog.debug('已进行函数值赋值替换:%s' % self.params_interface) return self.params_interface
def execute_test_case(self, *arg): """ 执行测试用例 :param arg: :return: """ test_interface = RequestInterface() sql = "SELECT * FROM `case_interface` WHERE case_status = 1" result = self.db.select_all(sql) if result.get('code') == '0000' and result.get('data'): MyLog.debug('获取执行接口成功') datas = result.get('data') for temp_case_interface in datas: obj = extraDB(temp_case_interface, self.cache) params_interface = obj.replace() # 执行测试前重构测试数据 url_interface = params_interface.get('url_interface') id_case = temp_case_interface.get('id') name_interface = temp_case_interface.get('name_interface') name_case = temp_case_interface.get('name_case') headdata = ast.literal_eval(params_interface.get('header_interface')) type_interface = params_interface.get('exe_mode') if url_interface != '' and headdata != '' and type_interface != '': temp_level_check = temp_case_interface.get('check_level') # 检查级别 result_http_respones = test_interface.http_request(url_interface, headdata, params_interface.get('params_interface'), type_interface) print(result_http_respones) MyLog.debug("用例返回消息:{result}".format(result=result_http_respones.get('data'))) obj.setvar(result_http_respones.get('data')) self.db.op_sql("UPDATE case_interface SET result_interface = '%s' where id = %s " % (result_http_respones.get('data'), id_case)) # 将返回包数据写入用例表 if result_http_respones['code'] == '0000' and len(result_http_respones['data']) != 0: base_compare = CompareParam(temp_case_interface) if '0' in list(temp_level_check): # 执行关键值参数检查 result_compare_code = base_compare.compare_code(result_http_respones.get('data')) MyLog.debug('用例编号:%s|检查级别:关键字参数值|接口名称:%s|用例名称:%s|提示信息:%s \n' % (id_case, name_interface, name_case, result_compare_code['message'])) if '1' in list(temp_level_check): # 执行参数完整性检查 result_compare_params_complete = base_compare.compare_params_complete( result_http_respones.get('data')) MyLog.debug('用例编号:%s|检查级别:参数完整性|接口名称:%s|用例名称:%s|提示信息:%s \n' % (id_case, name_interface, name_case, result_compare_params_complete['message'])) elif len(result_http_respones['data']) == 0: MyLog.debug('接口名称: %s|信息错误:获取用例数据为空,请检查用例\n' % name_interface) else: MyLog.debug('接口名称: %s|信息错误:获取用例数据失败' % name_interface)
class WindowsConnect(Connect): def __init__( self, windows_name: str, hwnd: int = 0, ): self.windows_name = windows_name if hwnd == 0: self.hwnd = win32gui.FindWindow(0, self.windows_name) else: self.hwnd = hwnd self.logger = MyLog('windows').get_logger() def connect(self): if self.hwnd == 0: self.logger.error('连接失败!请检查~ name:{}'.format(self.windows_name)) return None else: self.logger.info('连接成功!hwnd:{} name:{}'.format( self.hwnd, self.windows_name)) return self.hwnd @log def activate_window(self): user32 = ctypes.WinDLL('user32.dll') user32.SwitchToThisWindow(self.hwnd, True) def execute_command(self, com): ex = subprocess.Popen(com, stdout=subprocess.PIPE, shell=True) self.logger.debug('执行命令: {}'.format(com)) out, err = ex.communicate() status = ex.wait() self.logger.debug('命令结果: {}'.format(out.decode().strip())) self.logger.debug('状态: {}'.format(status)) return out.decode(), err, status def click_bg(self, pos, pos_end=None): """ 后台模拟点击 :param pos: 要点击的坐标 :param pos_end: 若不为空,则点击的是pos~pos_end之间的随机位置 :return: 无 """ pos_rand = proc_rand_pos(pos, pos_end) win32gui.SendMessage(self.hwnd, win32con.WM_MOUSEMOVE, 0, win32api.MAKELONG(*pos_rand)) win32gui.SendMessage(self.hwnd, win32con.WM_LBUTTONDOWN, 0, win32api.MAKELONG(*pos_rand)) time.sleep(random.randint(20, 80) / 1000) win32gui.SendMessage(self.hwnd, win32con.WM_LBUTTONUP, 0, win32api.MAKELONG(*pos_rand)) @log def drag_bg(self, pos1, pos2, cost_time=None): """ 后台拖拽 :param pos1: (x,y) 起点坐标 :param pos2: (x,y) 终点坐标 :param cost_time: 消耗时间 """ move_x = np.linspace(pos1[0], pos2[0], num=20, endpoint=True) move_y = np.linspace(pos1[1], pos2[1], num=20, endpoint=True) win32gui.SendMessage(self.hwnd, win32con.WM_LBUTTONDOWN, 0, win32api.MAKELONG(*pos1)) for i in range(20): x = int(round(move_x[i])) y = int(round(move_y[i])) win32gui.SendMessage(self.hwnd, win32con.WM_MOUSEMOVE, 0, win32api.MAKELONG(x, y)) t = 0.01 * random.random() time.sleep(t) cost_time -= t * 1000 if cost_time > 0: time.sleep(cost_time / 1000) win32gui.SendMessage(self.hwnd, win32con.WM_LBUTTONUP, 0, win32api.MAKELONG(*pos2))