18
2018
04

robot Framework(03)接口测试脚本开发

一、Robot Framework接口测试介绍

接口测试,无非就是通过http请求需要测试的接口,不同接口的数据准备包括:url,domain,header,cookie,param/data等等。

返回值,一般是json/text/xml,我们要做的就是多了些数据转换操作,不同接口处理起来大同小异。


二、接口测试脚本示例

引用Library

requests
json
RequestsLibrary
Collections
SeleniumLibrary   #要使用cookie的话需要打开浏览器

1、先创建resource,稍微封装一下

查询、更新CPS订单接口示例.txt

*** Settings ***
Library           requests
Library           json
Library           RequestsLibrary
Library           Collections
Library           SeleniumLibrary

*** Keywords ***
接口查询CPS订单
    [Arguments]    ${pageIndex}    ${pageSize}
    #API说明    http://localhost:777/HelpPage/Help/Api/GET-v2-Order-GetCPSOrderList
    ${apiurl}    set variable    /v2/Order/GetCPSOrderList
    ${params}    set variable    pageIndex=${pageIndex}&pageSize=${pageSize}
    ${fullurl}    set variable    ${apiurl}?${params}
    #comment    访问GET接口地址:${fullurl}
    log    ................................................................................
    log    访问GET接口地址:${fullurl}
    log    ................................................................................
    ${response}    api接口请求    "get"    ${apiurl}    getparams=${params}
    ${jsonData}    to json    ${response.content}
    #${dict}    get dictionary items    ${jsonData}
    ${dict}    set variable    ${jsonData}
    log    ................................................................................
    log    返回结果Json:${dict}
    log    ................................................................................
    log    data = ${dict["data"]}
    ${data}    Get From Dictionary    ${dict}    data
    ${totalCount}    get from dictionary    ${data}    totalCount
    ${cpsOrderList}    get from dictionary    ${data}    cpsOrderList
    log    ................................................................................
    log    返回json解析数据:totalCount=${totalCount} cpsOrderList=${cpsOrderList}
    log    ................................................................................
    Delete All Sessions
    [Return]    ${totalCount}    ${cpsOrderList}    # 返回总数量和订单列表

接口更新CPS订单
    [Arguments]    ${OrderId}    ${CPSFee}    ${CpsOrderStatus}
    #API说明    http://localhost:777/HelpPage/Help/Api/POST-v2-Order-UpdateCPSOrderStatus
    ${apiurl}    set variable    /v2/Order/UpdateCPSOrderStatus
    ${dictParams}    Create Dictionary    OrderId=${OrderId}    CPSFee=${CPSFee}    CpsOrderStatus=${CpsOrderStatus}
    ${listParams}    Create List    ${dictParams}
    log    ................................................................................
    log    参数:${listParams}
    log    ................................................................................
    ${response}    api接口请求    "post"    ${apiurl}    postdata=${listParams}
    ${content}    to json    ${response.content}
    #${Success}    set variable    ${true}
    ${Success}    Get From Dictionary    ${content}    success
    ${message}    Get From Dictionary    ${content}    message
    log    ................................................................................
    log    返回结果:success=${Success} message=${message}
    log    ................................................................................
    Delete All Sessions
    Should Be True    ${Success}==${true}
    [Return]    ${Success}    # 是否成功

api接口请求
    [Arguments]    ${method}    ${url}    ${postdata}=${None}    ${getparams}=${None}
    [Documentation]    api接口请求
    comment    接口域名地址,不含http://
    ${domain}    set variable    api.test.com
    comment    模拟ApigeeHeader安全验证串
    ${token}    set variable    uTnm9QSxFpcAd=
    comment    组建header信息
    ${headers}    Create Dictionary    token=${token}    host=${domain}    Content-Type=application/json
    comment    从浏览器取cookie,需要打开浏览器
    #${cookies}    get cookies
    #${cookies}    set variable
    comment    下面两种方式可以连接字符串
    #${httpDomain}    set variable    http://${domain}
    #${httpDomain}    catenate    SEPARATOR=    http://    ${domain}
    comment    创建本次api请求的session会话信息
    create session    api    http://${domain}    ${headers}
    comment    最简单的get请求方式,也可以实现get请求,就是没地方加header参数
    #${response}    requests.get    ${fullurl}
    comment    请求接口并返回响应结果,判断get就调用 Get Request请求接口,否则调用Post Request请求接口
    ${response}=    run keyword if    ${method} == "get"    Get Request    api    ${url}    headers=${headers}
    ...    params=${getparams}
    ...    ELSE    Post Request    api    ${url}    headers=${headers}    data=${postdata}
    [Return]    ${response}    # 接口返回内容








其实,最重要的就是3行:

    ${response}=    run keyword if    ${method} == "get"    Get Request    api    ${url}    headers=${headers}
    ...    params=${getparams}
    ...    ELSE    Post Request    api    ${url}    headers=${headers}    data=${postdata}


2、创建 Suite,创建测试用例

*** Settings ***
Resource          ../../../Resource/接口/webapi/查询、更新CPS订单接口示例.txt
Resource          ../../../Resource/公用/DB.txt
Resource          ../../../Resource/MAG后台/公用.txt

*** Test Cases ***

查询CPS订单内容、更新CPS订单
    [Documentation]    1、执行数据库更新SQL
    ...    2、查询数据库并判断
    ...    3、访问CPS订单接口
    ...    4、开始通过接口循环更新CPS订单
    ...    4.1 单个CPS订单更新结果判断
    ...    5、循环更新CPS订单结束
    comment    1、执行数据库更新SQL
    执行SQLSERVER    ${None}    update orders set CpsOrderStatus=-2 where orderid in(2706802,2706801);
    comment    2、查询数据库并判断
    ${queryresult}    查SQLSERVER    ${None}    select OrderId,CpsOrderStatus from orders where orderid in(2706802,2706801);
    ${resultcount}=    Get Length    ${queryresult}
    : FOR    ${index}    IN RANGE    0    ${resultcount}
    \    ${data}    Get From List    ${queryresult}    ${index}
    \    ${orderid}    convert to string    ${data[0]}
    \    log    ${data}
    \    log    ${queryresult[${index}][0]}
    Should Be Equal As Integers    2    ${resultcount}
    comment    3、访问CPS订单接口
    ${totalCount}    ${cpsOrderList}    接口查询CPS订单    1    20
    ${OrderId}    set variable    ${None}
    ${CPSFee}    set variable    ${None}
    ${CpsOrderStatus}    set variable    ${None}
    ${list}    set variable    ${cpsOrderList}    #Convert To List
    log    CPS未处理订单数量:${totalCount}
    comment    4、开始通过接口循环更新CPS订单
    : FOR    ${index}    IN RANGE    0    ${totalCount}
    \    log    循环list索引:${index}
    \    ${data}    Get From List    ${list}    ${index}
    \    run keyword if    ${data}==${None}    exit for loop
    \    ${OrderId}    Get From Dictionary    ${data}    orderId
    \    ${CPSFee}    set variable    0.2
    \    ${CpsOrderStatus}    Get From Dictionary    ${data}    cpsOrderStatus
    \    ${Success}    接口更新CPS订单    ${OrderId}    ${CPSFee}    ${CpsOrderStatus}
    \    comment    4.1 单个CPS订单更新结果判断
    \    Should Be Equal As Strings    ${true}    ${Success}
    comment    5、循环更新CPS订单结束



3、测试用例执行结果

(点击弹出大图):




附注意事项:

Content-Type 区分大小写,而且有问题比较难查,因为post数据时,封装方法内部是判断Content-Type来把参数转换为对应的格式。


其他字段,最好严格按原始的大小写,不要随意更改




其他接口测试语法参考:https://www.cnblogs.com/zz27zz/p/7347273.html



版权声明:
作者:真爱无限 出处:http://www.pukuimin.top 本文为博主原创文章版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接.
« 上一篇下一篇 »

相关文章:

评论列表:

1.文章缺少注释  2018-4-27 18:53:09 回复该留言
麻烦对脚本注释清楚含义,以便通俗易懂
2.pukuimin  2018-4-27 19:28:55 回复该留言
这位看官有点挑剔了,在下甘拜下风,已经含泪补充了几句说明文档
3.无名  2018-4-28 9:41:13 回复该留言
这一补充,我更看不懂了,这是怎么回事?

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。