def login_request(self, **kwargs): """ 获取登录后返回授权 :return: """ dict_parm = get_value('login') for i in kwargs: for j in dict_parm['payload']: if i == j: dict_parm['payload'][j] = kwargs[i] headers = { 'authorization': "Basic YmljYWk6QmljYWkzNjU=", 'origin': "http://manager.bicai365.com", 'user-agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36", 'content-type': "application/x-www-form-urlencoded", 'accept': "*/*", 'cache-control': "no-cache", 'postman-token': "6db8f158-30c1-6a85-093c-7c7ffc88284b" } response = requests.request("POST", url=self.get_evn_url + dict_parm['url'], data=dict_parm['payload'], headers=headers) # return response response_dicts = json.loads(response.text) authorization = response_dicts['token_type'].capitalize( ) + ' ' + response_dicts['access_token'] return authorization
def get_request(self, key_name, key): """ delete请求 :param url: :param header: :return: """ dict_parm = get_value(key_name) try: with allure.step("请求内容"): with allure.step("method:" + str(dict_parm['method'])): pass with allure.step("url:" + self.get_evn_url + dict_parm['url'] + "/" + str(key)): pass with allure.step("headers:" + json.dumps(self.get_web_header())): pass with allure.step("payload为空"): response = requests.request("GET", url=self.get_evn_url + dict_parm['url'] + "/" + str(key), headers=self.get_web_header()) except requests.RequestException as e: print('%s%s' % ('RequestException url: ', self.get_evn_url + dict_parm['url'] + "/" + key)) print(e) return () except Exception as e: print('%s%s' % ('Exception url: ', self.get_evn_url + dict_parm['url'] + "/" + key)) print(e) return () time_consuming = response.elapsed.microseconds / 1000 time_total = response.elapsed.total_seconds() Common.Consts.STRESS_LIST.append(time_consuming) response_dicts = dict() response_dicts['code'] = response.status_code try: response_dicts['body'] = response.json() except Exception as e: print(e) response_dicts['body'] = '' response_dicts['text'] = response.text response_dicts['time_consuming'] = time_consuming response_dicts['time_total'] = time_total with allure.step("响应报文:"): with allure.step("act_results:" + str(response_dicts['text'])): pass return json.loads(response_dicts['text'])
def api_request(self, key_name, data=None, **kwargs): self.data = get_value(key_name) self.url = self.get_evn_url + self.data['url'] headers = { 'content-type': "application/json", 'accept': "*/*", } if kwargs is not None: for i in kwargs: for j in self.data['payload']['head']: if i == j: self.data['payload']['head'][j] = kwargs[i] for i in kwargs: for j in self.data['payload']['param']: if i == j: self.data['payload']['param'][j] = kwargs[i] if data is not None: self.data['payload'] = data response_dicts = requests.post(url=self.url, data=json.dumps(self.data['payload']), headers=headers) time_consuming = response_dicts.elapsed.microseconds / 1000 # 接口耗时单位毫秒 time_total = response_dicts.elapsed.total_seconds() # 接口耗时单位秒 with allure.step("time:\r" + str(time_total) + "s"): pass with allure.step("url:\r" + self.url): pass with allure.step("method:\r" + self.data['method']): pass with allure.step("headers:\r" + json.dumps(headers)): pass with allure.step("payload:\r" + json.dumps(self.data['payload'])): pass with allure.step("response:\r" + json.dumps(response_dicts.json())): pass with allure.step("response:\r" + str(response_dicts.json())): pass return response_dicts.json()
def start_request(self, key_name, payload=None, **kwargs): """ 根据value值匹配yaml文件内name获取字典数据源参数 :param value: :param payload: :param kwargs: :return:response_dicts """ dict_parm = get_value(key_name) if kwargs is not None and 'payload' in dict_parm: if type(dict_parm['payload']) == dict: for i in kwargs: for j in dict_parm['payload']: if i == j: dict_parm['payload'][j] = kwargs[i] # if 'payload' in dict_parm: if type(dict_parm['payload']) == str: dict_parm['payload'] = json.loads(dict_parm['payload']) if kwargs is not None and 'payload' in dict_parm: for i in kwargs: for j in dict_parm['payload']: if i == j: dict_parm['payload'][j] = kwargs[i] dict_parm['payload'] = json.dumps(dict_parm['payload']) if payload is not None: dict_parm['payload'] = payload try: if dict_parm['method'] == 'POST': with allure.step("请求内容"): with allure.step("method:" + dict_parm['method']): pass with allure.step("url:" + self.get_evn_url + str(dict_parm['url'])): pass with allure.step("headers:" + json.dumps(self.get_web_header())): pass if 'payload' in dict_parm: with allure.step( "payload:" + str(json.loads(dict_parm['payload']))): response = requests.request( "POST", url=self.get_evn_url + dict_parm['url'], data=dict_parm['payload'], headers=self.get_web_header()) else: with allure.step("payload为空"): response = requests.post( url=self.get_evn_url + dict_parm['url'], headers=self.get_web_header()) elif dict_parm['method'] == 'PUT': with allure.step("请求内容"): with allure.step("method:" + dict_parm['method']): pass with allure.step("url:" + self.get_evn_url + str(dict_parm['url'])): pass with allure.step("headers:" + str(self.get_web_header())): pass with allure.step("payload:" + str(json.loads(dict_parm['payload']))): response = requests.request( "PUT", url=self.get_evn_url + dict_parm['url'], data=dict_parm['payload'], headers=self.get_web_header()) elif dict_parm['method'] == 'GET': with allure.step("请求内容"): with allure.step("method:" + dict_parm['method']): pass with allure.step("url:" + self.get_evn_url + str(dict_parm['url'])): pass with allure.step("headers:" + str(self.get_web_header())): pass if 'payload' in dict_parm: with allure.step("payload:" + str(dict_parm['payload'])): response = requests.get( url=self.get_evn_url + dict_parm['url'], params=dict_parm['payload'], headers=self.get_web_header()) else: with allure.step("payload为空"): response = requests.get( url=self.get_evn_url + dict_parm['url'], headers=self.get_web_header()) except requests.RequestException as e: print('%s%s' % ('RequestException url: ', self.get_evn_url + dict_parm['url'])) print(e) return () except Exception as e: print('%s%s' % ('Exception url: ', self.get_evn_url + dict_parm['url'])) print(e) return () time_consuming = response.elapsed.microseconds / 1000 time_total = response.elapsed.total_seconds() Common.Consts.STRESS_LIST.append(time_consuming) response_dicts = dict() response_dicts['code'] = response.status_code try: response_dicts['body'] = response.json() except Exception as e: print(e) response_dicts['body'] = '' response_dicts['text'] = response.text response_dicts['time_consuming'] = time_consuming response_dicts['time_total'] = time_total with allure.step("响应报文:"): with allure.step("act_results:" + str(response_dicts['text'])): pass return json.loads(response_dicts['text'])