def __get(self,requests_info): try: url = self.hosts+requests_info['请求地址'] variable_list = re.findall('\\${\w+}', requests_info['请求参数(get)']) for v in variable_list: requests_info['请求参数(get)'] = requests_info['请求参数(get)'].replace(v, '"%s"' % self.tmp_variables[v[2:-1]]) response = self.session.get(url = url, params = json.loads(requests_info['请求参数(get)']), headers = requests_info['请求头部信息']) response.encoding = response.apparent_encoding #保证不乱吗 if requests_info['取值方式'] == 'jsonpath取值': value = jsonpath.jsonpath(response.json(),requests_info['取值代码'])[0] self.tmp_variables[requests_info['取值变量']] = value elif requests_info['取值方式'] == '正则取值': value = re.findall(requests_info['取值代码'],response.text)[0] self.tmp_variables[requests_info['取值变量']] = value result = CheckUtils(response).run_check(requests_info['断言类型'],requests_info['期望结果']) logger.info(CheckUtils(response).run_check(requests_info['断言类型'], requests_info['期望结果'])) print(result) except ProxyError as e: result = {'code': 3, 'message': '调用接口[%s]时发生Proxy异常,异常原因为%s' % (requests_info['接口名称'], e.__str__()),'check_result': False} logger.error('调用接口[%s]时发生Proxy异常,异常原因为:%s' % (requests_info['接口名称'], e.__str__())) except ConnectionError as e: result = {'code': 3, 'message': '调用接口[%s]时发生Connection异常,异常原因为%s' % (requests_info['接口名称'], e.__str__()),'check_result': False} logger.error('调用接口[%s]时发生Connection异常,异常原因为:%s' % (requests_info['接口名称'], e.__str__())) except RequestException as e: result = {'code': 3, 'message': '调用接口[%s]时发生Request异常,异常原因为%s' % (requests_info['接口名称'], e.__str__()),'check_result': False} logger.error('调用接口[%s]时发生Request异常,异常原因为:%s' % (requests_info['接口名称'], e.__str__())) except Exception as e: result = {'code': 3, 'message': '调用接口[%s]时发异常,异常原因为%s' % (requests_info['接口名称'], e.__str__()), 'check_result': False} print(logger.info(result)) return result
def __post(self, requests_info): url = self.hosts + requests_info['请求地址'] get_variable_list = re.findall('\\${\w+}', requests_info['请求参数(get)']) for variable in get_variable_list: requests_info['请求参数(get)'] = requests_info['请求参数(get)'].replace( variable, '"%s"' % self.tmp_variables[variable[2:-1]]) post_variable_list = re.findall('\\${\w+}', requests_info['请求参数(post)']) for variable in post_variable_list: requests_info['请求参数(post)'] = requests_info['请求参数(post)'].replace( variable, '"%s"' % self.tmp_variables[variable[2:-1]]) response = self.session.post( url=url, headers=requests_info['请求头部信息'], params=json.loads(requests_info['请求参数(get)']), json=json.loads(requests_info['请求参数(post)'])) response.encoding = response.apparent_encoding if requests_info['取值方式'] == 'jsonpath取值': value = jsonpath.jsonpath(response.json(), requests_info['取值代码'])[0] self.tmp_variables[requests_info['取值变量']] = value elif requests_info['取值方式'] == '正则取值': value = re.findall(requests_info['取值代码'], response.text)[0] self.tmp_variables[requests_info['取值变量']] = value result = CheckUtils(response).run_check(requests_info['断言类型'], requests_info['期望结果']) return result
def __get(self,requests_info): try: url = self.hosts + requests_info['请求地址'] variable_list = re.findall('\\${\w+}',requests_info['请求参数(get)']) for variable in variable_list: requests_info['请求参数(get)'] = requests_info['请求参数(get)'].replace( variable,'"%s"'%self.tmp_variables[variable[2:-1]]) response = self.sessin.get(url = url, params = json.loads(requests_info['请求参数(get)']), headers = requests_info['请求头部信息']) response.encoding = response.apparent_encoding # 根据网页的内容分析网页的编码方式,防止乱码 if requests_info['取值方式'] == 'jsonpath取值': value = jsonpath.jsonpath(response.json(),requests_info['取值代码'])[0] self.tmp_variables[requests_info['取值变量']] = value elif requests_info['取值方式'] == '正则取值': value = re.findall(requests_info['取值代码'],response.text)[0] self.tmp_variables[requests_info['取值变量']] = value result = CheckUtils(response).run_check(requests_info['断言类型'],requests_info['期望结果']) except ProxyError as e: result = {'code':3,'message':'调用接口[%s]时发生代理异常,异常原因:%s'%(requests_info['接口名称'],e.__str__()), 'check_result':False} logger.error('调用接口[%s]时发生代理异常,异常原因:%s'%(requests_info['接口名称'],e.__str__())) except ConnectionError as e: result = {'code': 3, 'message': '调用接口[%s]时发生连接异常,异常原因:%s' % (requests_info['接口名称'], e.__str__()), 'check_result': False} logger.error( '调用接口[%s]时发生连接异常,异常原因:%s' % (requests_info['接口名称'], e.__str__())) except RequestException as e: result = {'code': 3, 'message': '调用接口[%s]时发生请求异常,异常原因:%s' % (requests_info['接口名称'], e.__str__()), 'check_result': False} logger.error('调用接口[%s]时发生请求异常,异常原因:%s' % (requests_info['接口名称'], e.__str__())) except Exception as e: result = {'code': 3, 'message': '调用接口[%s]时发生异常,异常原因:%s' % (requests_info['接口名称'], e.__str__()), 'check_result': False} logger.error('调用接口[%s]时发生异常,异常原因:%s' % (requests_info['接口名称'], e.__str__())) return result
def __post(self, case_info): url = self.conf.get_inidata('test', 'host') + case_info['请求地址'] get_variables_list = re.findall('\\${\w+}', case_info['请求参数(get)']) for variables in get_variables_list: case_info['请求参数(get)'] = case_info['请求参数(get)'].replace( variables, '"%s"' % self.temporary[variables[2:-1]]) post_variables_list = re.findall('\\${\w+}', case_info['请求参数(post)']) for variables in post_variables_list: case_info['请求参数(post)'] = case_info['请求参数(post)'].replace( variables, '"%s"' % self.temporary[variables[2:-1]]) header_variables_list = re.findall('\\${\w+}', case_info['请求头部信息']) for variables in header_variables_list: case_info['请求头部信息'] = case_info['请求头部信息'].replace( variables, "%s" % self.temporary[variables[2:-1]]) if not case_info['请求参数(get)']: get_params = None else: get_params = json.loads(case_info['请求参数(get)']) if not case_info['请求参数(post)']: post_params = None else: post_params = json.loads(case_info['请求参数(post)']) if not case_info['请求头部信息']: header_params = None else: header_params = json.loads(case_info['请求头部信息']) response = self.session.post(url=url, headers=header_params, params=get_params, json=post_params) response.encoding = response.apparent_encoding variable_list = case_info['取值变量'].split(',') code_list = case_info['取值代码'].split(',') for item in range(len(variable_list)): if case_info['取值方式'] == 'jsonpath': self.temporary[variable_list[item]] = jsonpath.jsonpath( response.json(), code_list[item])[0] elif case_info['取值方式'] == 're': self.temporary[variable_list[item]] = re.findall( code_list[item], response.text)[0] rseult = CheckUtils(response).run_check(case_info["断言类型"], case_info["预期结果"]) return rseult
def __post(self, requests_info): try: url = self.hosts + requests_info['请求地址'] get_variable_list = re.findall('\\${\w+}', requests_info['请求参数(get)']) for variable in get_variable_list: requests_info['请求参数(get)'] = requests_info[ '请求参数(get)'].replace( variable, '"%s"' % self.tmp_variables[variable[2:-1]]) post_variable_list = re.findall('\\${\w+}', requests_info['请求参数(post)']) for variable in post_variable_list: requests_info['请求参数(post)'] = requests_info[ '请求参数(post)'].replace( variable, '"%s"' % self.tmp_variables[variable[2:-1]]) response = self.session.post( url=url, headers=requests_info['请求头部信息'], params=json.loads(requests_info['请求参数(get)']), data=json.dumps(json.loads(requests_info['请求参数(post)']), ensure_ascii=False).encode('utf-8') # json = json.loads(requests_info['请求参数(post)']) ) response.encoding = response.apparent_encoding if requests_info['取值方式'] == 'jsonpath取值': value = jsonpath.jsonpath(response.json(), requests_info['取值代码'])[0] self.tmp_variables[requests_info['取值变量']] = value elif requests_info['取值方式'] == '正则取值': value = re.findall(requests_info['取值代码'], response.text)[0] self.tmp_variables[requests_info['取值变量']] = value result = CheckUtils(response).run_check(requests_info['断言类型'], requests_info['期望结果']) except ProxyError as e: result = { 'code': 3, 'message': '调用接口 [%s] 时发生代理异常,异常原因:%s' % (requests_info['接口名称'], e.__str__()), 'check_result': False } logger.error('调用接口 [%s] 时发生代理异常,异常原因:%s' % (requests_info['接口名称'], e.__str__())) except ConnectionError as e: result = { 'code': 3, 'message': '调用接口 [%s] 时发生连接异常,异常原因:%s' % (requests_info['接口名称'], e.__str__()), 'check_result': False } logger.error('调用接口 [%s] 时发生连接异常,异常原因:%s' % (requests_info['接口名称'], e.__str__())) except RequestException as e: result = { 'code': 3, 'message': '调用接口 [%s] 时发生Request异常,异常原因:%s' % (requests_info['接口名称'], e.__str__()), 'check_result': False } logger.error('调用接口 [%s] 时发生Request异常,异常原因:%s' % (requests_info['接口名称'], e.__str__())) except IndexError as e: result = { 'code': 3, 'message': '根据 %s 方式不能完成从响应正文截取,异常原因:%s' % (requests_info['取值方式'], e.__str__()), 'check_result': False } logger.error('根据 %s 方式不能完成从响应正文截取,异常原因:%s' % (requests_info['取值方式'], e.__str__())) except Exception as e: result = { 'code': 3, 'message': '调用接口 [%s] 时发生异常,异常原因:%s' % (requests_info['接口名称'], e.__str__()), 'check_result': False } logger.error('调用接口 [%s] 时发生异常,异常原因:%s' % (requests_info['接口名称'], e.__str__())) return result
def __get(self, requests_info): try: logger.info('%s 接口调用get请求 --开始执行' % requests_info['接口名称']) url = self.hosts + requests_info['请求地址'] # 处理接口间的关联 post_variable_list = re.findall('\\${\w+}', requests_info['请求参数(post)']) for variable in post_variable_list: logger.info('%s 接口开始将请求参数(post)里的变量替换成值--开始执行' % requests_info['接口名称']) requests_info['请求参数(post)'] = requests_info['请求参数(post)'].replace(variable, '"%s"' % self.tmp_variables[ variable[2:-1]]) logger.info('%s 接口开始将请求参数(post)里的变量替换成值--执行结束' % requests_info['接口名称']) headers_variable_list = re.findall('\\${\w+}', requests_info['请求头部信息']) for variable in headers_variable_list: # 将接口中的变量替换成值 replace为替换 logger.info('%s 接口开始将头部信息里的变量替换成值--开始执行' % requests_info['接口名称']) requests_info['请求头部信息'] = requests_info['请求头部信息'].replace(variable, '"%s"' % self.tmp_variables[variable[2:-1]]) logger.info('%s 接口开始将头部信息里的变量替换成值--执行结束' % requests_info['接口名称']) get_variable_list = re.findall('\\${\w+}', requests_info['请求参数(get)']) for variable in get_variable_list: logger.info('%s 接口开始将请求参数(get)里的变量替换成值--开始执行' % requests_info['接口名称']) requests_info['请求参数(get)'] = requests_info['请求参数(get)'].replace(variable, '"%s"' % self.tmp_variables[ variable[2:-1]]) logger.info('%s 接口开始将请求参数(get)里的变量替换成值--执行结束' % requests_info['接口名称']) if requests_info['请求头部信息'] != '': if requests_info['请求参数(post)'] != '': logger.info('%s 接口开始调用--带请求头--带post请求参数 ,(--get请求)' % requests_info['接口名称']) response = self.session.get(url=url, headers=eval(requests_info['请求头部信息']), params=json.loads(requests_info['请求参数(get)']), data=json.loads(requests_info['请求参数(post)'])) else: logger.info('%s 接口开始调用--带请求头,(--get请求)' % requests_info['接口名称']) response = self.session.get(url=url, headers=eval(requests_info['请求头部信息']), params=json.loads(requests_info['请求参数(get)'])) elif requests_info['请求参数(post)'] != '': logger.info('%s 接口开始调用--带post请求参数 ,(--get请求)' % requests_info['接口名称']) response = self.session.get(url=url, params=json.loads(requests_info['请求参数(get)']), data=json.loads(requests_info['请求参数(post)'])) else: logger.info('%s 接口开始调用 ,(--get请求)' % requests_info['接口名称']) response = self.session.get(url=url, params=json.loads(requests_info['请求参数(get)'])) response.encoding = response.apparent_encoding # 防止乱码 for data in range(len(requests_info['取值方式'].split(','))): if requests_info['取值方式'].split(',')[data] == 'jsonpath取值': logger.info('%s 接口开始使用jsonpath取值' % requests_info['接口名称']) value = jsonpath.jsonpath(response.json(), requests_info['取值代码'].split(',')[data])[0] self.tmp_variables[requests_info['取值变量'].split(',')[data]] = value logger.info('%s 接口使用jsonpath取值完毕,值为:%s' % (requests_info['接口名称'], value)) elif requests_info['取值方式'].split(',')[data] == '正则取值': logger.info('%s 接口开始使用正则取值' % requests_info['接口名称']) value = re.findall(requests_info['取值代码'].split(',')[data], response.text)[0] self.tmp_variables[requests_info['取值变量'].split(',')[data]] = value logger.info('%s 接口使用正则取值取值完毕,值为:%s' % (requests_info['接口名称'], value)) elif requests_info['取值方式'].split(',')[data] == '响应头取值': logger.info('%s 接口开始使用响应头取值' % requests_info['接口名称']) value = response.headers[requests_info['取值代码'].split(',')[data]] self.tmp_variables[requests_info['取值变量'].split(',')[data]] = value logger.info('%s 接口使用响应头取值完毕,值为:%s' % (requests_info['接口名称'], value)) result = CheckUtils(response).run_check(requests_info['断言类型'], requests_info['期望结果']) except ProxyError as e: result = {'code': 3, 'message': '调用接口 [%s] 时发生代理异常,异常原因:%s' % (requests_info['接口名称'], e.__str__()), 'check_result': False} logger.error('调用接口 [%s] 时发生代理异常,异常原因:%s' % (requests_info['接口名称'], e.__str__())) except ConnectionError as e: result = {'code': 3, 'message': '调用接口 [%s] 时发生链接异常,异常原因是:%s' % (requests_info['接口名称'], e.__str__()), 'check_result': False} logger.error('调用接口 [%s] 时发生链接异常,异常原因是:%s' % (requests_info['接口名称'], e.__str__())) except RequestException as e: result = {'code': 3, 'message': '调用接口 [%s] 时发生Request异常,异常原因是:%s' % (requests_info['接口名称'], e.__str__()), 'check_result': False} logger.error('调用接口 [%s] 时发生Request异常,异常原因是:%s' % (requests_info['接口名称'], e.__str__())) except Exception as e: result = {'code': 3, 'message': '调用接口 [%s] 时发生异常,异常原因:%s' % (requests_info['接口名称'], e.__str__()), 'check_result': False} logger.error('调用接口 [%s] 时发生异常,异常原因:%s' % (requests_info['接口名称'], e.__str__())) logger.info('%s 接口调用get请求 --执行结束' % requests_info['接口名称']) return result
def __post(self, requests_info): try: logger.info('%s 接口调用post请求 --开始执行' % requests_info['接口名称']) url = self.hosts + requests_info['请求地址'] # 处理接口间的关联 headers_variable_list = re.findall('\\${\w+}', requests_info['请求头部信息']) for variable in headers_variable_list: # 将头部信息里的变量替换成值 logger.info('%s 接口开始将头部信息里的变量替换成值--开始执行' % requests_info['接口名称']) requests_info['请求头部信息'] = requests_info['请求头部信息'].replace( variable, '%s' % self.tmp_variables[variable[2:-1]]) logger.info('%s 接口开始将头部信息里的变量替换成值--执行结束' % requests_info['接口名称']) get_variable_list = re.findall('\\${\w+}', requests_info['请求参数(get)']) for variable in get_variable_list: logger.info('%s 接口开始将请求参数(get)里的变量替换成值--开始执行' % requests_info['接口名称']) requests_info['请求参数(get)'] = requests_info[ '请求参数(get)'].replace( variable, '%s' % self.tmp_variables[variable[2:-1]]) logger.info('%s 接口开始将请求参数(get)里的变量替换成值--执行结束' % requests_info['接口名称']) post_variable_list = re.findall('\\${\w+}', requests_info['请求参数(post)']) for variable in post_variable_list: logger.info('%s 接口开始将请求参数(post)里的变量替换成值--开始执行' % requests_info['接口名称']) requests_info['请求参数(post)'] = requests_info[ '请求参数(post)'].replace( variable, '%s' % self.tmp_variables[variable[2:-1]]) logger.info('%s 接口开始将请求参数(post)里的变量替换成值--执行结束' % requests_info['接口名称']) if requests_info['请求头部信息'] != '': if requests_info['请求参数(get)'] != '': if requests_info['请求参数(post)'] != '': logger.info( '%s 接口开始调用--带求头--带get请求参数--带post请求参数 ,(--post请求)' % requests_info['接口名称']) response = self.session.post( url=url, headers=eval(requests_info['请求头部信息']), params=json.loads(requests_info['请求参数(get)']), data=json.loads(requests_info['请求参数(post)'])) else: logger.info('%s 接口开始调用--带求头--带get请求参数 ,(--post请求)' % requests_info['接口名称']) response = self.session.post( url=url, headers=eval(requests_info['请求头部信息']), params=json.loads(requests_info['请求参数(get)'])) else: if requests_info['请求参数(post)'] != '': logger.info('%s 接口开始调用--带请求头--带post请求参数,(--post请求)' % requests_info['接口名称']) response = self.session.post( url=url, headers=eval(requests_info['请求头部信息']), data=json.loads(requests_info['请求参数(post)'])) else: logger.info('%s 接口开始调用--带求头-- ,(--post请求)' % requests_info['接口名称']) response = self.session.post( url=url, headers=eval(requests_info['请求头部信息'])) else: if requests_info['请求参数(get)'] != '': if requests_info['请求参数(post)'] != '': logger.info( '%s 接口开始调用--带get请求参数--带post请求参数 ,(--post请求)' % requests_info['接口名称']) response = self.session.post( url=url, params=json.loads(requests_info['请求参数(get)']), data=json.loads(requests_info['请求参数(post)'])) else: logger.info('%s 接口开始调用--带get请求参数--,(--post请求)' % requests_info['接口名称']) response = self.session.post( url=url, params=json.loads(requests_info['请求参数(get)'])) else: if requests_info['请求参数(post)'] != '': logger.info('%s 接口开始调用--带post请求参数,(--post请求)' % requests_info['接口名称']) response = self.session.post( url=url, data=json.loads(requests_info['请求参数(post)'])) else: logger.info('%s 接口开始调用--无post请求参数 ,(--post请求)' % requests_info['接口名称']) response = self.session.post(url=url) response.encoding = response.apparent_encoding logger.info(response.text) for data in range(len(requests_info['取值方式'].split(','))): if requests_info['取值方式'].split(',')[data] == 'jsonpath取值': logger.info('%s 接口开始使用jsonpath取值' % requests_info['接口名称']) value = jsonpath.jsonpath( response.json(), requests_info['取值代码'].split(',')[data])[0] self.tmp_variables[requests_info['取值变量'].split(',') [data]] = value logger.info('%s 接口使用jsonpath取值完毕,值为:%s' % (requests_info['接口名称'], value)) elif requests_info['取值方式'].split(',')[data] == '正则取值': logger.info('%s 接口开始使用正则取值' % requests_info['接口名称']) value = re.findall(requests_info['取值代码'].split(',')[data], response.text)[0] self.tmp_variables[requests_info['取值变量'].split(',') [data]] = value logger.info('%s 接口使用正则取值完毕,值为:%s' % (requests_info['接口名称'], value)) elif requests_info['取值方式'].split(',')[data] == '响应头取值': logger.info('%s 接口开始使用响应头取值' % requests_info['接口名称']) value = response.headers[requests_info['取值代码'].split(',') [data]] self.tmp_variables[requests_info['取值变量'].split(',') [data]] = value logger.info('%s 接口使用响应头取值完毕,值为:%s' % (requests_info['接口名称'], value)) elif requests_info['取值方式'].split(',')[data] == 'sta_time': logger.info('%s 接口开始使用sta_time取值' % requests_info['接口名称']) value = (int(time.time()) + int( requests_info['取值代码'].split(',')[data])) * 1000 self.tmp_variables[requests_info['取值变量'].split(',') [data]] = value logger.info('%s 接口sta_time取值完毕,值为:%s' % (requests_info['接口名称'], value)) elif requests_info['取值方式'].split(',')[data] == 'datetime': logger.info('%s 接口开始使用datetime取值' % requests_info['接口名称']) now_time = datetime.datetime.now() value = (now_time + datetime.timedelta( hours=+int(requests_info['取值代码'].split(',')[data])) ).strftime('%Y-%m-%d %H:%M:%S') self.tmp_variables[requests_info['取值变量'].split(',') [data]] = value logger.info('%s 接口datetime取值完毕,值为:%s' % (requests_info['接口名称'], value)) result = CheckUtils(response).run_check(requests_info['断言类型'], requests_info['期望结果']) except ProxyError as e: result = { 'code': 3, 'message': '调用接口 [%s] 时发生代理异常,异常原因:%s' % (requests_info['接口名称'], e.__str__()), 'check_result': False } logger.error('调用接口 [%s] 时发生代理异常,异常原因:%s' % (requests_info['接口名称'], e.__str__())) except ConnectionError as e: result = { 'code': 3, 'message': '调用接口 [%s] 时发生链接异常,异常原因是:%s' % (requests_info['接口名称'], e.__str__()), 'check_result': False } logger.error('调用接口 [%s] 时发生链接异常,异常原因是:%s' % (requests_info['接口名称'], e.__str__())) except RequestException as e: result = { 'code': 3, 'message': '调用接口 [%s] 时发生Request异常,异常原因是:%s' % (requests_info['接口名称'], e.__str__()), 'check_result': False } logger.error('调用接口 [%s] 时发生Request异常,异常原因是:%s' % (requests_info['接口名称'], e.__str__())) except Exception as e: result = { 'code': 3, 'message': '调用接口 [%s] 时发生异常,异常原因:%s' % (requests_info['接口名称'], e.__str__()), 'check_result': False } logger.error(result) logger.error('调用接口 [%s] 时发生异常,异常原因:%s' % (requests_info['接口名称'], e.__str__())) logger.info('%s 接口调用post请求 --执行结束' % requests_info['接口名称']) return result