python+excel接口自動(dòng)化獲取token并作為請求參數(shù)進(jìn)行傳參操作
1、登錄接口登錄后返回對應(yīng)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存儲(chǔ)在一個(gè)json文件中:
3、根據(jù)excel中對應(yīng)的header字段(為了更容易看可以自行再封裝及excel增加一列token字段)為yes時(shí),讀取保存token的文件數(shù)據(jù),并通過將請求數(shù)據(jù)和token數(shù)據(jù)合并為一個(gè)請求數(shù)據(jù)后再作為一個(gè)請求數(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ù)作為一個(gè)請求數(shù)據(jù)發(fā)起請求
補(bǔ)充知識:python讀取Excel接口用例進(jìn)行接口自動(dòng)化并寫入結(jié)果
前提準(zhǔn)備:需要導(dǎo)入對應(yīng)的庫和準(zhǔn)備Excel接口測試用例
續(xù)上:
源文件數(shù)據(jù)展示
執(zhí)行后結(jié)果展示
以上這篇python+excel接口自動(dòng)化獲取token并作為請求參數(shù)進(jìn)行傳參操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. js select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼2. 如何在PHP中讀寫文件3. java加載屬性配置properties文件的方法4. PHP正則表達(dá)式函數(shù)preg_replace用法實(shí)例分析5. 什么是Python變量作用域6. 《Java程序員修煉之道》作者Ben Evans:保守的設(shè)計(jì)思想是Java的最大優(yōu)勢7. CSS3中Transition屬性詳解以及示例分享8. php redis setnx分布式鎖簡單原理解析9. bootstrap select2 動(dòng)態(tài)從后臺(tái)Ajax動(dòng)態(tài)獲取數(shù)據(jù)的代碼10. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式
