robotframework框架 - 利用RequestsLibrary关键字轻松实现接口自动化-Josn格式
TestCaese.robot
*** 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} ${header_name} ${header_value} 成功
业务参数.robot
*** Variables ***
${iphone} 18334002627
${header_name} Content-Type
${header_value} application/json
业务逻辑.tobot
*** Settings ***
Library RequestsLibrary
Library requests
Library Collections
Library String
Library OperatingSystem
Library Selenium2Library
Library DateTime
Library BuiltIn
*** keywords ***
接口请求
[Arguments] ${Case_name} ${iphone} ${header_name} ${header_value} ${Last_results}
设置请求头 ${header_name} ${header_value}
设置body ${Case_name} ${iphone}
创建会话
提交Post请求 ${body}
解析Josn并记录数据 ${Case_name} ${iphone} ${Last_results}
设置请求头
[Arguments] ${header_name} ${header_value}
${headers} Create Dictionary ${header_name}=${header_value}
log **${headers}***
set suite variable ${headers}
设置body
[Arguments] ${Case_name} ${iphone}
${body} Run keyword If '办理活动需求' in '${Case_name}' set variable {"Request": {"BusiParams": {"BillId": "18484167602","OfferId": "310100924056"},"BusiCode": "OI_GetUserGuaranteeFee"},"PubInfo": {"InterfaceId": "6000","InterfaceType": "1","OpId": "95550115"}}
... ELSE IF '输入手机号为空进行办理' in '${Case_name}' set variable {"Request": {"BusiParams": {"BillId": " ","OfferId": "310100924056"},"BusiCode": "OI_GetUserGuaranteeFee"},"PubInfo": {"InterfaceId": "6000","InterfaceType": "1","OpId": "95550115"}}
... ELSE IF '输入手机号大于11位进行办理' in '${Case_name}' set variable {"Request": {"BusiParams": {"BillId": "18484167602830","OfferId": "310100924056"},"BusiCode": "OI_GetUserGuaranteeFee"},"PubInfo": {"InterfaceId": "6000","InterfaceType": "1","OpId": "95550115"}}
... ELSE IF '输入手机号小于11位进行办理' in '${Case_name}' set variable {"Request": {"BusiParams": {"BillId": "1848416","OfferId": "310100924056"},"BusiCode": "OI_GetUserGuaranteeFee"},"PubInfo": {"InterfaceId": "6000","InterfaceType": "1","OpId": "95550115"}}
... ELSE IF 'OfferId输入为空办理' in '${Case_name}' set variable {"Request": {"BusiParams": {"BillId": "18484167602","OfferId": " "},"BusiCode": "OI_GetUserGuaranteeFee"},"PubInfo": {"InterfaceId": "6000","InterfaceType": "1","OpId": "95550115"}}
... ELSE IF 'BusiCode输入为空办理' in '${Case_name}' set variable {"Request": {"BusiParams": {"BillId": "18484167602","OfferId": "310100924056"},"BusiCode": " "},"PubInfo": {"InterfaceId": "6000","InterfaceType": "1","OpId": "95550115"}}
... ELSE IF 'InterfaceId输入为空办理' in '${Case_name}' set variable {"Request": {"BusiParams": {"BillId": "18484167602","OfferId": "310100924056"},"BusiCode": "OI_GetUserGuaranteeFee"},"PubInfo": {"InterfaceId": " ","InterfaceType": "1","OpId": "95550115"}}
... ELSE IF 'InterfaceType输入为空办理' in '${Case_name}' set variable {"Request": {"BusiParams": {"BillId": "18484167602","OfferId": "310100924056"},"BusiCode": "OI_GetUserGuaranteeFee"},"PubInfo": {"InterfaceId": "6000","InterfaceType": " ","OpId": "95550115"}}
... ELSE IF 'OpId输入为空办理' in '${Case_name}' set variable {"Request": {"BusiParams": {"BillId": "18484167602","OfferId": "310100924056"},"BusiCode": "OI_GetUserGuaranteeFee"},"PubInfo": {"InterfaceId": "6000","InterfaceType": "1","OpId": " "}}
set suite variable ${body}
创建会话
Create Session api http://135.10.145.165:18100 \ \ \ timeout=60
run keyword if 'REQUEST_TIMEOUT' == 'TRUE' log 连接超时
提交Post请求
[Arguments] ${body}
${results_body} POST On Session api /CRMService data=${body} headers=${headers}
set suite variable ${results_body}
解析Josn并记录数据
[Arguments] ${Case_name} ${iphone} ${Last_results}
#第一种转换方式(最新)
log ${results_body.json()}
log ++++++++${results_body.json()}++++++++
#第二转换种方式(即将被淘汰)
#${body_conten} to json ${results_body.content}
#log ${body_conten}
#第一种获取方式
log -----++++-----${results_body.json()["Response"]["ErrorInfo"]["Message"]}------++++-------
${message_3} set variable ${results_body.json()["Response"]["ErrorInfo"]["Message"]}
#第二种获取方式
${Response} Get From Dictionary ${results_body.json()} Response
${ErrorInfo} Get From Dictionary ${Response} ErrorInfo
${Message} Get From Dictionary ${ErrorInfo} Message
log -----${Message}-----
接口断言 ${Case_name} ${iphone} ${Message} ${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}