RobotFramework自動化框架RequestsLibrary之Post Request


RF環境安裝引用了https://blog.csdn.net/qq_15158911/article/details/80592526,所以也跟着統一了一下環境(其實現在python3各種庫也都兼容了,所以也可以用python3,但各安裝包都安裝與之對應的版本)

 

1、首先根據提供的別名查找會話並在會話中發送POST請求

alias, uri, data={}, headers=None, files={}, allow_redirects=None(其中alias為接口別名,uri為url后半段:注:可以為空(有些Url地址直接為http://ip:port的形式),data為接口傳遞的參數,headers為接口請求頭信息)

 

2、接口導入的庫:requestsLibrary,Collections(可以通過python自帶工具pip install +庫名安裝),新建robot套件,然后點擊右側“庫”,在彈出窗口添加如下兩個庫名

 

3、右擊套件新建testcase,點擊打開后在新建case的Eidit界面做接口的參數化設置(此處涉及data兩種方式,一種是直接將post上傳的json直接設置好用例需要設置的值,另一種是將json內各參數直接拎出來做參數化如下圖)

直接將data值參數化:

 

 

將傳遞的method和params下的各個參數都做參數化(此好處就是在做條件組合時每個參數可以單獨設置):

 

4、說明:

(1)headers請求頭信息參數${headers},各個請求可能請求頭不一樣,按實際情況設置(創建字典);

(2)創建session時,設置session別名(alias),請求的Url,以及headers參數(創建session);

(3)設置data參數${data}(創建字典);

(4)設置post請求方式以及請求參數:Post request(session別名)、uri、data=${data}、headers=${headers}(請求方式及請求參數設置)

(5)打印上傳的參數和值(Log:日志打印);

(6)設置斷言(Should Be equal As Strings);斷言的作用不懂得可以問百度0.0.

(7)執行完成后,刪除所有session(Delete All Sessions);

5、執行結果(兩者打印執行結果日志如下:可分別通過GetAccount.test_GetAccount、GetAccount.test_getAccountData下面日志查看執行結果):

 

Starting test: BlockChain.Rfcode.GetAccount.test_GetAccount
20190525 10:12:35.913 : INFO : ${headers} = {'Content-Type': u'application/json'}
20190525 10:12:35.914 : INFO : Creating Session using : alias=AccountJsonget, url=http://192.168.1.13:8089, headers={'Content-Type': u'application/json'}, cookies=None, auth=None, timeout=None, proxies=None, verify=False, debug=0
20190525 10:12:35.914 : DEBUG : Creating session: AccountJsonget
20190525 10:12:35.915 : INFO :
Argument types are:
<type 'bool'>
20190525 10:12:35.917 : INFO : ${method} = GetAccount
20190525 10:12:35.918 : INFO : ${chainId} = 2
20190525 10:12:35.920 : INFO : ${address} = 0x2c7536e3605d9c16a7a3d7b1898e529396a65c23
20190525 10:12:35.920 : INFO : ${params} = {'chainId': '2', 'address': '0x2c7536e3605d9c16a7a3d7b1898e529396a65c23'}
20190525 10:12:35.920 : INFO : {'chainId': '2', 'address': '0x2c7536e3605d9c16a7a3d7b1898e529396a65c23'}
20190525 10:12:35.922 : INFO : ${data} = {'params': {'chainId': '2', 'address': '0x2c7536e3605d9c16a7a3d7b1898e529396a65c23'}, 'method': 'GetAccount'}
20190525 10:12:35.929 : DEBUG : Starting new HTTP connection (1): 192.168.1.13:8089
20190525 10:12:35.932 : DEBUG : http://192.168.1.13:8089 "POST / HTTP/1.1" 200 129
20190525 10:12:35.933 : DEBUG : post response: {"address":"0x2c7536e3605d9c16a7a3d7b1898e529396a65c23","balance":9.999999998e+26,"codeHash":null,"nonce":175,"storageRoot":null}
20190525 10:12:35.933 : INFO : Post Request using : alias=AccountJsonget, uri=, data={"params": {"chainId": "2", "address": "0x2c7536e3605d9c16a7a3d7b1898e529396a65c23"}, "method": "GetAccount"}, headers={'Content-Type': u'application/json'}, files=None, allow_redirects=True
20190525 10:12:35.934 : INFO : ${response} = <Response [200]>
20190525 10:12:35.935 : INFO : {"address":"0x2c7536e3605d9c16a7a3d7b1898e529396a65c23","balance":9.999999998e+26,"codeHash":null,"nonce":175,"storageRoot":null}
20190525 10:12:35.936 : INFO :
Argument types are:
<type 'int'>
<type 'unicode'>
20190525 10:12:35.937 : INFO : To JSON using : content={"address":"0x2c7536e3605d9c16a7a3d7b1898e529396a65c23","balance":9.999999998e+26,"codeHash":null,"nonce":175,"storageRoot":null}
20190525 10:12:35.937 : INFO : To JSON using : pretty_print=
20190525 10:12:35.937 : INFO : ${respdata} = {u'nonce': 175, u'storageRoot': None, u'balance': 9.999999998e+26, u'codeHash': None, u'address': u'0x2c7536e3605d9c16a7a3d7b1898e529396a65c23'}
20190525 10:12:35.938 : INFO : {u'nonce': 175, u'storageRoot': None, u'balance': 9.999999998e+26, u'codeHash': None, u'address': u'0x2c7536e3605d9c16a7a3d7b1898e529396a65c23'}
20190525 10:12:35.939 : INFO : ${address} = 0x2c7536e3605d9c16a7a3d7b1898e529396a65c23
20190525 10:12:35.940 : INFO : ${balance} = 9.999999998e+26
20190525 10:12:35.941 : INFO : ${codeHash} = None
20190525 10:12:35.942 : INFO : ${nonce} = 175
20190525 10:12:35.943 : INFO : ${storageRoot} = None
20190525 10:12:35.943 : INFO : Delete All Sessions
Ending test: BlockChain.Rfcode.GetAccount.test_GetAccount

Starting test: BlockChain.Rfcode.GetAccount.test_getAccountData
20190525 10:12:35.949 : INFO : ${headers} = {'Content-Type': u'application/json'}
20190525 10:12:35.950 : INFO : Creating Session using : alias=AccountJson, url=http://192.168.1.13:8089, headers={'Content-Type': u'application/json'}, cookies=None, auth=None, timeout=None, proxies=None, verify=False, debug=0
20190525 10:12:35.950 : DEBUG : Creating session: AccountJson
20190525 10:12:35.950 : INFO :
Argument types are:
<type 'bool'>
20190525 10:12:35.951 : INFO : ${data} = { "method": "GetAccount","params": {"chainId":"2", "address":"0x2c7536e3605d9c16a7a3d7b1898e529396a65c23"}}
20190525 10:12:35.955 : DEBUG : Starting new HTTP connection (1): 192.168.1.13:8089
20190525 10:12:35.958 : DEBUG : http://192.168.1.13:8089 "POST / HTTP/1.1" 200 129
20190525 10:12:35.958 : DEBUG : post response: {"address":"0x2c7536e3605d9c16a7a3d7b1898e529396a65c23","balance":9.999999998e+26,"codeHash":null,"nonce":175,"storageRoot":null}
20190525 10:12:35.958 : INFO : Post Request using : alias=AccountJson, uri=, data={ "method": "GetAccount","params": {"chainId":"2", "address":"0x2c7536e3605d9c16a7a3d7b1898e529396a65c23"}}, headers={'Content-Type': u'application/json'}, files=None, allow_redirects=True
20190525 10:12:35.959 : INFO : ${response} = <Response [200]>
20190525 10:12:35.960 : INFO : {"address":"0x2c7536e3605d9c16a7a3d7b1898e529396a65c23","balance":9.999999998e+26,"codeHash":null,"nonce":175,"storageRoot":null}
20190525 10:12:35.961 : INFO :
Argument types are:
<type 'int'>
<type 'unicode'>
20190525 10:12:35.963 : INFO : To JSON using : content={"address":"0x2c7536e3605d9c16a7a3d7b1898e529396a65c23","balance":9.999999998e+26,"codeHash":null,"nonce":175,"storageRoot":null}
20190525 10:12:35.963 : INFO : To JSON using : pretty_print=
20190525 10:12:35.963 : INFO : ${respdata} = {u'nonce': 175, u'storageRoot': None, u'balance': 9.999999998e+26, u'codeHash': None, u'address': u'0x2c7536e3605d9c16a7a3d7b1898e529396a65c23'}
20190525 10:12:35.964 : INFO : {u'nonce': 175, u'storageRoot': None, u'balance': 9.999999998e+26, u'codeHash': None, u'address': u'0x2c7536e3605d9c16a7a3d7b1898e529396a65c23'}
20190525 10:12:35.965 : INFO : ${address} = 0x2c7536e3605d9c16a7a3d7b1898e529396a65c23
20190525 10:12:35.966 : INFO : ${balance} = 9.999999998e+26
20190525 10:12:35.968 : INFO : ${codeHash} = None
20190525 10:12:35.969 : INFO : ${nonce} = 175
20190525 10:12:35.970 : INFO : ${storageRoot} = None
20190525 10:12:35.971 : INFO : Delete All Sessions
Ending test: BlockChain.Rfcode.GetAccount.test_getAccountData

 

由於剛開始學習,對於日志顯示的type問題現在還沒找到解決方法,如果有哪位老鳥路過,還請告知下!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM