python+excel接口自動化獲取token并作為請求參數(shù)進行傳參操作
1、登錄接口登錄后返回對應token封裝:
import jsonimport requestsfrom util.operation_json import OperationJsonfrom base.runmethod import RunMethodclass OperationHeader: def __init__(self, response): self.response = json.loads(response) def get_response_token(self): ’’’ 獲取登錄返回的token ’’’ token = {'data':{'token':self.response[’data’][’token’]}} return token def write_token(self): op_json = OperationJson() op_json.write_data(self.get_response_token())if __name__ == ’__main__’: url = 'http://xxxx.com' data = { 'username': '1111', 'password': '123456' } res=requests.post(url,data).json op = OperationHeader(res) op.write_token()
2、將登錄接口返回的token存儲在一個json文件中:
3、根據(jù)excel中對應的header字段(為了更容易看可以自行再封裝及excel增加一列token字段)為yes時,讀取保存token的文件數(shù)據(jù),并通過將請求數(shù)據(jù)和token數(shù)據(jù)合并為一個請求數(shù)據(jù)后再作為一個請求數(shù)據(jù)發(fā)起請求:
request_data=dict(request_data,**token) # 把請求數(shù)據(jù)與登錄token合并,并作為請求數(shù)據(jù)
res = self.run_method.run_main(method, url, request_data) # 再將合并的數(shù)據(jù)作為一個請求數(shù)據(jù)發(fā)起請求
補充知識:python讀取Excel接口用例進行接口自動化并寫入結果
前提準備:需要導入對應的庫和準備Excel接口測試用例
續(xù)上:
源文件數(shù)據(jù)展示
執(zhí)行后結果展示
以上這篇python+excel接口自動化獲取token并作為請求參數(shù)進行傳參操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。
相關文章:
1. Warning: require(): open_basedir restriction in effect,目錄配置open_basedir報錯問題分析2. ASP中常用的22個FSO文件操作函數(shù)整理3. Vue+elementUI下拉框自定義顏色選擇器方式4. React+umi+typeScript創(chuàng)建項目的過程5. SharePoint Server 2019新特性介紹6. php測試程序運行速度和頁面執(zhí)行速度的代碼7. php網(wǎng)絡安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究8. ASP的Global.asa文件技巧用法9. ASP中if語句、select 、while循環(huán)的使用方法10. html清除浮動的6種方法示例
