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、源码地址:https://github.com/Duanxh2018/automated_testing.git
6、执行结果(两者打印执行结果日志如下:可分别通过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问题现在还没找到解决方法,如果有哪位老鸟路过,还请告知下!