robotframework框架 - 利用RequestsLibrary关键字轻松实现接口自动化-XML格式
用例层:
*** Settings ***
Suite Setup 访问CRM个人网台系统
Suite Teardown 退出系统
Library String
Resource 接口参数.robot
Resource 接口逻辑.robot
Resource ../../../01_Yz_Resource/02_业务层/web公共逻辑yjh.txt
*** Test Cases ***
CRM网台-校验金额配置-余额大于于校验金额时
接口请求 CRM网台-校验金额配置-余额大于于校验金额时 ${iphone} ${xmlhead} 校验通过
接口参数层
*** Variables ***
${iphone} 15285701683
${xmlhead} <?xml version="1.0" encoding="UTF-8"?><InterBOSS><Version>0100</Version><TestFlag>0</TestFlag><BIPType><BIPCode>BIP3B505</BIPCode><ActivityCode>T3000505</ActivityCode> <ActionCode>0</ActionCode></BIPType><RoutingInfo><OrigDomain>CTRM</OrigDomain><RouteType>00</RouteType><Routing><HomeDomain>BOSS</HomeDomain><RouteValue>851</RouteValue></Routing></RoutingInfo><TransInfo><SessionID>280_20210228230320411_s9Ap</SessionID><TransIDO>MutoN2N20210325115504cMAd9k</TransIDO><TransIDOTime>20210325115504</TransIDOTime><TransIDH>7287737220</TransIDH><TransIDHTime>20210228230320</TransIDHTime></TransInfo><SNReserve><TransIDC>99808080-ctrm2-dpvmm20210228230320530335278</TransIDC><ConvID>e57268a9-1a69-41a3-b374-18409dd80acd</ConvID><CutOffDay>20210325</CutOffDay><OSNTime>20210228230320</OSNTime><OSNDUNS>9980</OSNDUNS><HSNDUNS>8510</HSNDUNS><MsgSender>0046</MsgSender><MsgReceiver>8511</MsgReceiver><Priority>3</Priority><ServiceLevel>1</ServiceLevel></SNReserve><SvcCont></SvcCont></InterBOSS>
接口逻辑层
*** Settings ***
Library RequestsLibrary
Library requests
Library XML
Library Collections
Library String
Library OperatingSystem
Library Selenium2Library
Library DateTime
Library BuiltIn
*** keywords ***
接口请求
[Arguments] ${Case_name} ${iphone} ${xmlhead} ${Last_results}
设置请求头 ${xmlhead}
设置body ${Case_name} ${iphone}
XML请求报文组装 ${xmlhead} ${xmlbody}
创建会话
提交Post请求 &{multiple_files}
解析xml并记录数据 ${Case_name} ${iphone} ${Last_results}
设置请求头
[Arguments] ${xmlhead}
set suite variable ${xmlhead}
设置body
[Arguments] ${Case_name} ${iphone}
${xmlbody} Run keyword If '余额大于于校验金额时' in '${Case_name}' set variable <?xml version="1.0" encoding="UTF-8"?><BizReq><OprNumb>270_20210338232434602_hKwJ</OprNumb> <ChannelID>270</ChannelID><NumType>1</NumType><MobileNo>${iphone}</MobileNo><CheckType>3</CheckType><GoodsID>2021999900003944</GoodsID><ProductList><ProductType>09101</ProductType><ProductID>2021999900003944</ProductID></ProductList></BizReq>
... ELSE IF '输入错误的手机号' in '${Case_name}' set variable <?xml version="1.0" encoding="UTF-8"?><BizReq><OprNumb>270_20210338232434602_hKwJ</OprNumb><ChannelID>270</ChannelID><NumType>1</NumType><MobileNo>${iphone}</MobileNo><CheckType>3</CheckType><GoodsID>2021999900003944</GoodsID><ProductList><ProductType>09101</ProductType><ProductID>2021999900003944</ProductID></ProductList></BizReq>
... ELSE IF '输入手机号为空' in '${Case_name}' set variable <?xml version="1.0" encoding="UTF-8"?><BizReq><OprNumb>270_20210338232434602_hKwJ</OprNumb><ChannelID>270</ChannelID><NumType>1</NumType><MobileNo>${iphone}</MobileNo><CheckType>3</CheckType><GoodsID>2021999900003944</GoodsID><ProductList><ProductType>09101</ProductType><ProductID>2021999900003944</ProductID></ProductList></BizReq>
... ELSE IF '输入手机号超过11位' in '${Case_name}' set variable <?xml version="1.0" encoding="UTF-8"?><BizReq><OprNumb>270_20210338232434602_hKwJ</OprNumb><ChannelID>270</ChannelID><NumType>1</NumType><MobileNo>${iphone}</MobileNo><CheckType>3</CheckType><GoodsID>2021999900003944</GoodsID><ProductList><ProductType>09101</ProductType><ProductID>2021999900003944</ProductID></ProductList></BizReq>
... ELSE IF '输入手机号号不足11位' in '${Case_name}' set variable <?xml version="1.0" encoding="UTF-8"?><BizReq><OprNumb>270_20210338232434602_hKwJ</OprNumb><ChannelID>270</ChannelID> <NumType>1</NumType><MobileNo>${iphone}</MobileNo><CheckType>3</CheckType><GoodsID>2021999900003944</GoodsID><ProductList><ProductType>09101</ProductType> <ProductID>2021999900003944</ProductID></ProductList></BizReq>
set suite variable ${xmlbody}
创建会话
#files
Create Session api http://135.10.145.100:18082 \ \ \ timeout=60
run keyword if 'REQUEST_TIMEOUT' == 'TRUE' log 连接超时
XML请求报文组装
[Arguments] ${xmlhead} ${xmlbody}
${xmlhead} evaluate (None,u'${xmlhead}')
${xmlbody} evaluate (None,u'${xmlbody}')
&{multiple_files} Create Dictionary xmlhead=${xmlhead} xmlbody=${xmlbody}
log *****&{multiple_files}*****
set suite variable &{multiple_files}
提交Post请求
[Arguments] &{multiple_files}
${body} Post Request api /cboss/home files=&{multiple_files}
sleep 2
${body_conten} set variable ${body.content}
set suite variable ${body_conten}
log ***${body_conten}***
解析xml并记录数据
[Arguments] ${Case_name} ${iphone} ${Last_results}
${SvcCont} get element text ${body_conten} SvcCont
log -----${SvcCont}-----
${Reserve} get element text ${SvcCont} Reserve
log ${Reserve}
接口断言 ${Case_name} ${iphone} ${Reserve} ${Last_results}
接口断言
[Arguments] ${Case_name} ${iphone} ${Reserve} ${Last_results}
log ${Reserve}
log ${Last_results}
${results} Run Keyword And return status should contain ${Reserve} ${Last_results}
log ${results}
Run keyword if ${results}==True 测试结果记录11 ${Case_name} 成功 ${Case_name}校验成功 ${Reserve}
... ${iphone}
... ELSE 测试结果记录11 ${Case_name} 失败 ${Case_name}校验失败 预期结果与实际结果不一致
... ${iphone}