*** Settings ***
Library RequestsLibrary
Library Collections
Library String
Library HttpLibrary.HTTP
*** Keywords ***
Request_Post
[Documentation] 通用post请求
[Arguments] ${host} ${path} ${datas} ${params} ${headers}=None ${cookies}=None ${timeout}=30
# 处理请求header
${header_dict} Create Dictionary Content-Type=application/json
Log ${header_dict}
Run Keyword If ${headers}==${None} Log 没有添加自定义header
... ELSE Run Keyword add_header ${headers} ${header_dict}
# 处理cookies
${cookies_dict} Create Dictionary
Run Keyword If ${cookies}==${None} Log 没有添加cookies信息
... ELSE Run Keyword add_cookies ${cookies} ${cookies_dict}
# 创建session
Create Session TZ_robotframework ${host} timeout=${timeout} cookies=${cookies_dict} verify=False
# 发起post请求
${resp} Post Request TZ_robotframework ${path} data=${datas} headers=${header_dict} params=${params}
[Return] ${resp}
Request_Get
[Documentation] 通用get请求
[Arguments] ${host} ${path} ${datas} ${params} ${headers} ${cookies}=None ${timeout}=30
# 处理请求header
${header_dict} Create Dictionary Content-Type=application/json
Run Keyword If ${headers}==${None} Log 没有添加自定义header
... ELSE Run Keyword add_header ${headers} ${header_dict}
# 处理cookies
${cookies_dict} Create Dictionary
Run Keyword If ${cookies}==${None} Log 没有添加cookies信息
... ELSE Run Keyword add_cookies ${cookies} ${cookies_dict}
# 创建session
Create Session TZ_robotframework ${host} timeout=${timeout} cookies=${cookies_dict} verify=False
# 发起get请求
${resp} Get Request TZ_robotframework ${path} headers=${header_dict} params=${params}
${resp_code} Set Variable ${resp.status_code}
Should Be True ${resp_code}==200
[Return] ${resp}
这里我们把get请求和post请求封装成通用关键字并增加了添加header和cookies的处理,首先我们定义的传入参数,
${host}为请求域名,
${path}为请求连接路径,
${data}和${params}分别为请求体,
${headers}为请求头,
${cookies}为cookies,
请求头和cookies默认设置为空,
通过create session创建一个session实例,
再通过Post Request或者Get Request发起请求,
唯一要注意有区分的是,post请求参数data和get请求参数params的不同,但都是通过字典类型传入。
下面是具体实例:
POST请求:
*** Settings ***
Resource ../http通用请求/http_requests.robot
Library Collections
Library HttpLibrary.HTTP
*** Keywords ***
sso_login_token
[Arguments] ${account} ${password} ${fromType} ${error_info}
${host} Set Variable http://testpassport.ra*****.com
${path} Set Variable /api/s**/login
${datas} Create Dictionary
Set To Dictionary ${datas} account ${account}
Set To Dictionary ${datas} fromType ${fromType}
Set To Dictionary ${datas} password ${password}
${params} Set Variable
${res} requests_post ${host} ${path} ${datas} ${params}
# log ${content.content}
# Log ${content.status_code}
${cookies} Set Variable ${res.cookies}
Log Json ${res.content}
#提取json数据方式一:
${info} Get Json Value ${res.content} /msg #填写数据所在层级路径提取json信息
# 返回token
${res_headers} Set Variable ${res.headers}
${res_token} get_cookies ${res_headers}
[Return] ${res_token}
#提取返回数据方式二:
${data} Parse Json ${res.content}
# ${datas} Get From Dictionary ${data} data
# ${code} Get From Dictionary ${data} code
${msg} Get From Dictionary ${data} msg
# # 断言验证
Should Be Equal As Strings ${msg} ${error_info}
上面是一个登录接口的例子,
先定义了必传参数${account}、${password}、${fromtype}和断言判断变量${error_info},
设置了接口域名${host}、接口请求路径${path},创建字典${datas}设置传入参数,
设置完毕通过二次封装的requests_post关键字发起请求,实际同关键字 :Post Request请求一致,只是这里我们多做了一层封装;
关于提取返回数据,关键字get json vaule是在不转换返回数据类型的情况下提示json数据,或者可以通过parse json关键字解析json,再通过get from Dictionary或者get from list获取我们预期数据,
最后通过关键字should be equal as strings断言验证接口
GET请求:
*** Settings ***
Resource ../../../../Resources/Business/http通用请求/http_requests.robot
Library Collections
Library HttpLibrary.HTTP
*** Variables ***
${host} https://testpassport.rab*****.com
${path} /api/sso/getUserI*****DisposableCode
*** Test Cases ***
getUserInfoByDisposableCode_Get
${disposableCode} get_code 158******* 111111 pc
${res} 通过临时code获取用户信息 ${disposableCode}
Log Json ${res.content}
${msg} Get Json Value ${res.content} /msg
${msg} Parse Json ${msg}
Should Be Equal ${msg} ok
getUserInfoByDisposableCode_Exception
${res} 通过临时code获取用户信息 ${EMPTY}
Log Json ${res.content}
${msg} Get Json Value ${res.content} /msg
Should Contain ${msg} 一次性code没有找到或已取出用户数据
getUserInfoByDisposableCode_DataVerify
${disposableCode} get_code 158****** 111111 pc
${res} 通过临时code获取用户信息 ${disposableCode}
Log Json ${res.content}
${msg} Get Json Value ${res.content} /msg
${uId} Get Json Value ${res.content} /data/uId
${account} Get Json Value ${res.content} /data/account
${mobile} Get Json Value ${res.content} /data/mobile
#断言
Should Not Be Empty ${msg}
Should Not Be Empty ${uId}
Should Not Be Empty ${account}
Should Not Be Empty ${mobile}
*** Keywords ***
通过临时code获取用户信息
[Arguments] ${disposableCode}
${params} Create Dictionary
${datas} Create Dictionary
Set To Dictionary ${params} disposableCode=${disposableCode}
${res} requests_get ${host} ${path} ${datas} ${params}
[Return] ${res}
首先通过封装的用户关键字:get_code,获取请求接口需要参数${disposableCode},
同样我们把get请求接口的公共部分抽离,分装成用户关键字:通过临时code获取用户信息,
这里get请求接口的入参${params}同样是通过字典的形式传入,
最后获取返回数据,提取预期字段断言验证如同上文POST接口所示