1. 安装robotframework-requests
pip install robotframework-requests
2. 发起请求
a. Create Session:创建一个与服务器的会话
参数:
alias:别名,作为当前会话的标识
url:服务器地址,基本地址,不是接口地址
b. Post Request:发起post请求
alias:选择一个会话的别名,来自于Create Session
url:接口地址,不包含服务器地址
data:请求提参数
返回值:response 对象
注意:post 请求一定要添加请求头,否则发送请求会失败
*** Settings ***
Library RequestsLibrary
*** Test Cases ***
测试用例1-发送post请求:
#需要创建session,在通session发起请求
Create Session qcdApi http://api.lemonban.com/futureloan
#测试数据
&{testdata} Create Dictionary mobile_phone=13122907522 pwd=12345678
&{headers} Create Dictionary X-Lemonban-Media-Type=lemonban.v2 User-Agent=Mozilla/5.0 LookSky
#发起post请求,一定需要传headers
${resp} Post Request qcdApi /member/register json=${testdata} headers=${headers}
Log ${resp.status_code}
Log ${resp.text}
c. Get Requests:发送一个get请求,并返回一个响应对象
alias:选择一个会话别名
url:接口地址,不包含服务器地址
params:请求参数
返回值:response对象
*** Settings ***
Library RequestsLibrary
*** Test Cases ***
测试用例2-发起get请求:
Create Session weatherApi http://www.sojson.com
&{testdata} Create Dictionary city=北京
${resp} Get Request weatherApi /open/api/weather/json.shtml params=${testdata}
Log ${resp.status_code}
Log ${resp.text}