1. *** Settings ***
    2. Library RequestsLibrary
    3. Library Collections
    4. Library String
    5. Library HttpLibrary.HTTP
    6. *** Keywords ***
    7. Request_Post
    8. [Documentation] 通用post请求
    9. [Arguments] ${host} ${path} ${datas} ${params} ${headers}=None ${cookies}=None ${timeout}=30
    10. # 处理请求header
    11. ${header_dict} Create Dictionary Content-Type=application/json
    12. Log ${header_dict}
    13. Run Keyword If ${headers}==${None} Log 没有添加自定义header
    14. ... ELSE Run Keyword add_header ${headers} ${header_dict}
    15. # 处理cookies
    16. ${cookies_dict} Create Dictionary
    17. Run Keyword If ${cookies}==${None} Log 没有添加cookies信息
    18. ... ELSE Run Keyword add_cookies ${cookies} ${cookies_dict}
    19. # 创建session
    20. Create Session TZ_robotframework ${host} timeout=${timeout} cookies=${cookies_dict} verify=False
    21. # 发起post请求
    22. ${resp} Post Request TZ_robotframework ${path} data=${datas} headers=${header_dict} params=${params}
    23. [Return] ${resp}
    24. Request_Get
    25. [Documentation] 通用get请求
    26. [Arguments] ${host} ${path} ${datas} ${params} ${headers} ${cookies}=None ${timeout}=30
    27. # 处理请求header
    28. ${header_dict} Create Dictionary Content-Type=application/json
    29. Run Keyword If ${headers}==${None} Log 没有添加自定义header
    30. ... ELSE Run Keyword add_header ${headers} ${header_dict}
    31. # 处理cookies
    32. ${cookies_dict} Create Dictionary
    33. Run Keyword If ${cookies}==${None} Log 没有添加cookies信息
    34. ... ELSE Run Keyword add_cookies ${cookies} ${cookies_dict}
    35. # 创建session
    36. Create Session TZ_robotframework ${host} timeout=${timeout} cookies=${cookies_dict} verify=False
    37. # 发起get请求
    38. ${resp} Get Request TZ_robotframework ${path} headers=${header_dict} params=${params}
    39. ${resp_code} Set Variable ${resp.status_code}
    40. Should Be True ${resp_code}==200
    41. [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请求:

    1. *** Settings ***
    2. Resource ../http通用请求/http_requests.robot
    3. Library Collections
    4. Library HttpLibrary.HTTP
    5. *** Keywords ***
    6. sso_login_token
    7. [Arguments] ${account} ${password} ${fromType} ${error_info}
    8. ${host} Set Variable http://testpassport.ra*****.com
    9. ${path} Set Variable /api/s**/login
    10. ${datas} Create Dictionary
    11. Set To Dictionary ${datas} account ${account}
    12. Set To Dictionary ${datas} fromType ${fromType}
    13. Set To Dictionary ${datas} password ${password}
    14. ${params} Set Variable
    15. ${res} requests_post ${host} ${path} ${datas} ${params}
    16. # log ${content.content}
    17. # Log ${content.status_code}
    18. ${cookies} Set Variable ${res.cookies}
    19. Log Json ${res.content}
    20. #提取json数据方式一:
    21. ${info} Get Json Value ${res.content} /msg #填写数据所在层级路径提取json信息
    22. # 返回token
    23. ${res_headers} Set Variable ${res.headers}
    24. ${res_token} get_cookies ${res_headers}
    25. [Return] ${res_token}
    26. #提取返回数据方式二:
    27. ${data} Parse Json ${res.content}
    28. # ${datas} Get From Dictionary ${data} data
    29. # ${code} Get From Dictionary ${data} code
    30. ${msg} Get From Dictionary ${data} msg
    31. # # 断言验证
    32. 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请求:

    1. *** Settings ***
    2. Resource ../../../../Resources/Business/http通用请求/http_requests.robot
    3. Library Collections
    4. Library HttpLibrary.HTTP
    5. *** Variables ***
    6. ${host} https://testpassport.rab*****.com
    7. ${path} /api/sso/getUserI*****DisposableCode
    8. *** Test Cases ***
    9. getUserInfoByDisposableCode_Get
    10. ${disposableCode} get_code 158******* 111111 pc
    11. ${res} 通过临时code获取用户信息 ${disposableCode}
    12. Log Json ${res.content}
    13. ${msg} Get Json Value ${res.content} /msg
    14. ${msg} Parse Json ${msg}
    15. Should Be Equal ${msg} ok
    16. getUserInfoByDisposableCode_Exception
    17. ${res} 通过临时code获取用户信息 ${EMPTY}
    18. Log Json ${res.content}
    19. ${msg} Get Json Value ${res.content} /msg
    20. Should Contain ${msg} 一次性code没有找到或已取出用户数据
    21. getUserInfoByDisposableCode_DataVerify
    22. ${disposableCode} get_code 158****** 111111 pc
    23. ${res} 通过临时code获取用户信息 ${disposableCode}
    24. Log Json ${res.content}
    25. ${msg} Get Json Value ${res.content} /msg
    26. ${uId} Get Json Value ${res.content} /data/uId
    27. ${account} Get Json Value ${res.content} /data/account
    28. ${mobile} Get Json Value ${res.content} /data/mobile
    29. #断言
    30. Should Not Be Empty ${msg}
    31. Should Not Be Empty ${uId}
    32. Should Not Be Empty ${account}
    33. Should Not Be Empty ${mobile}
    34. *** Keywords ***
    35. 通过临时code获取用户信息
    36. [Arguments] ${disposableCode}
    37. ${params} Create Dictionary
    38. ${datas} Create Dictionary
    39. Set To Dictionary ${params} disposableCode=${disposableCode}
    40. ${res} requests_get ${host} ${path} ${datas} ${params}
    41. [Return] ${res}

    首先通过封装的用户关键字:get_code,获取请求接口需要参数${disposableCode},
    同样我们把get请求接口的公共部分抽离,分装成用户关键字:通过临时code获取用户信息,
    这里get请求接口的入参${params}同样是通过字典的形式传入,
    最后获取返回数据,提取预期字段断言验证如同上文POST接口所示