RobotFrameWork接口自动化配置代理
By:Tangke024
From:平安科技公共平台开发部测试组
背景:
今天发现公司测试环境如http://test-api.pingan.com.cn,是通过代理访问的,在进行浏览器访问时,某些域名是通过公司配置的代理脚本进行连接访问;在运用自动化工具时,需要添加代理服务才能正常访问。
Jmeter的代理服务配置比较简单,在http sampler中可直接配置代理服务器。
RF的代理请求资料较少,在这里说明下通过RF自动化测试工具配置请求代理,连接服务。
work:
首先,这里需要用到RequestsLibrary库, robotframework-requests-0.4.7.tar \(60 KB, 下载次数: 0\) ,下载解压后进入目录运行python setup.py install安装;
打开RF,建立Project——Suite,在Suite引入Library——RequestsLibrary;
建立case:
1、创建代理值,此处需要用字典形式,如下;
&{proxies}= Create Dictionary http=http://10.36.232.125:8080
2、创建服务session,并引入代理,如下;
Create Session api http://test-api.pingan.com.cn proxies=${proxies}
3、建立业务请求。
至此一个包含代理的接口自动化请求做好了
请求结果:
Starting test: ESG-dev-test.dev-outerApp test.test
20170824 16:41:20.526 : INFO : &{proxies} = { http=10.36.232.125:8080 }
20170824 16:41:20.527 : INFO : Creating Session using : alias=api, url=http://test-api.pingan.com.cn, headers={}, cookies=None, auth=None, timeout=None, proxies={u'http': u'10.36.232.125:8080'}, verify=False, debug=0
20170824 16:41:20.529 : INFO : &{data} = { client_id=P_PJF_PA18_EMALL | grant_type=client_credentials | client_secret=7YMVDb83 }
20170824 16:41:20.542 : INFO : Starting new HTTP connection (1): 10.36.232.125
20170824 16:41:20.657 : INFO : Get Request using : alias=api, uri=/oauth/oauth2/access_token, headers=None json=None
20170824 16:41:20.657 : INFO : ${res} = <Response [200]>
20170824 16:41:20.659 : INFO : {"ret":"0","data":{"expires_in":"973","openid":"P_PJF_PA18_EMALL00","access_token":"BAABAAAE748448CA9621803528BB520D"},"msg":""}
20170824 16:41:20.660 : INFO : "请求的url:"http://test-api.pingan.com.cn/oauth/oauth2/access_token?client_secret=7YMVDb83&grant_type=client_credentials&client_id=P_PJF_PA18_EMALL
Ending test: ESG-dev-test.dev-outerApp test.test
可以看出代理设置接收成功,并且成功启用代理,请求数据正常返回。
要点:
proxies代理必须以字典的形式传入,不能直接的写成proxies=http://host:port,这是非法的形式,关键字help有明确说明“proxies Dictionary that contains proxy urls for HTTP and HTTPS communication”;
value中可以加协议,也可以不加协议,关联在key值。