class Method: def __init__(self): self.excel=OperationExcel() self.operationJson=OperationJson() def post(self,row): """参数data不存在变量""" try: r=requests.post(url=self.excel.get_Url(row=row), data=self.operationJson.get_json_data(row=row), headers=getHeadersValue()) return r except Exception as e: raise RuntimeError('接口请求发生异常') pass def post1(self,row,data): """参数data中存在变量""" try: r=requests.post(url=self.excel.get_Url(row=row), data=data, headers=getHeadersValue()) return r except Exception as e: raise RuntimeError('接口请求发生异常')
class Method: def __init__(self): self.excel = OperationExcel() self.operationJson = OperationJson() def post(self, row): """参数中不存在data中的变量参数时使用""" try: r = requests.post(url=self.excel.get_Url(row=row), json=self.operationJson.get_json_data(row=row), headers=getHeadersValue()) return r except Exception as e: raise RuntimeError('接口请求发生异常') pass def post1(self, row, data): """参数中存在data中的变量参数时使用""" try: r = requests.post(url=self.excel.get_Url(row=row), json=data, headers=getHeadersValue()) return r except Exception as e: raise RuntimeError('接口请求发生异常') def post2(self, data1): """获取accesstoken使用""" try: r = requests.post(url='https://api.heplat.com/iface2/getToken', json=data1, headers=getHeadersValue()) return r except Exception as e: raise RuntimeError('接口请求发生异常') pass def post3(self, data2): """获取accesstoken使用""" try: r = requests.post(url='https://api.heplat.com/iface2/getSmsCode', json=data2, headers=getHeadersValue()) return r except Exception as e: raise RuntimeError('接口请求发生异常') pass
class Login(unittest.TestCase): def setUp(self): self.methon = WebMethon() self.config = Config() self.excel = OperationExcel() self.pm = PublicMethod() self.operationJson = OperationJson() self.iscontent = IsContent() def tearDown(self): print('测试结束') def test_Login_001(self): ''' 登陆DDOU:/api/login :return: ''' r = requests.post(url=self.config.getUrl() + self.excel.get_Url(1), json=self.operationJson.getRequestsData(1)) print(r.json()) self.assertTrue(r.json()['msg'], "成功") self.assertTrue(self.iscontent.isContent(1, r.text))
class WebMethon(PublicMethod): def __init__(self): self.config = Config() self.excel = OperationExcel() self.public = PublicMethod() self.json = OperationJson() #=================封装请求头,s=0,未登录的请求头。s=1,已登陆的请求头============================== def getHeadersInfo( self, row, ): '''已登录的请求头''' headers = { 'Host': self.config.getHost(), #'Content-Type': self.excel.get_ParamsType(row=row), 'Accept-Encoding': 'gzip, deflate', 'Cookie': 'Hm_lvt_950d894867df0a55fbb3239fba8837ea=1588758701,1588774347,1588833146,1588856383', 'Accept': '*/*', 'User-Agent': 'DDOU/1.0.3 (iPhone; iOS 13.3.1; Scale/3.00)', 'Authorization': self.public.getFile('data', 'login', 'token'), #'Referer': self.config.getUrl()[0]+self.excel.get_Referer(row=row) } return headers def post(self, s, row, **kwargs): ''' 请求参数是json格式的post,这里读取的是excel+json文件中的数据 s=1:json格式的参数 s=2:data格式的参数 ''' if s == 1: try: r = requests.post(url=self.config.getUrl()[0] + self.excel.get_Url(row=row), json=self.json.getRequestsData(row=row), headers=self.getHeadersInfo(row=row), timeout=5) return r except Exception as e: raise RuntimeError('接口请求法生未知的错误') elif s == 2: '''请求方法是data格式的post,这里读取得是excel文件中的数据''' try: r = requests.post(url=self.excel.get_Url(row=row), data=self.excel.get_Params(row=row), headers=self.getHeadersInfo(row=row), timeout=5) return r except Exception as e: raise RuntimeError('接口请求法生未知的错误') else: print('Eorr:请检查请求参数!') def get(self, url, params=None): '''对get请求进行二次封装''' r = requests.get(url=url, params=params, headers=self.getHeadersInfo(), timeout=5) return r