Demo entry 6328782



Submitted by anonymous on Nov 29, 2016 at 06:27
Language: RobotFramework. Code size: 1.9 kB.

*** Settings ***
Library           RequestsLibrary
Library           requests
Library           Collections

*** Test Cases ***
    Create Session    api  # 创建一个session,接口域名地址
    ${addr}    Get Request    api    /mobile/get?phone=13728881664&dtype=json&key=d414f72419ff7ec8472156de0e580f58  # 发送请求并将其赋值给${addr}
    Should be Equal as strings    ${addr.status_code}    200  # 判断请求返回状态
    ${responsedata}    To Json    ${addr.content} # 转换为json格式
    ${keys}    Get Dictionary keys    ${responsedata} # 提取返回内容中的Key,可用于遍历后取值.eg:error_code,reason
    ${items}    Get Dictionary items    ${responsedata}
    ${values}    Get Dictionary values    ${responsedata}
    ${str}    Get From Dictionary    ${responsedata}    error_code  # Get From Dictionary从字典中提取指定key并将其值付给${str}
    Should Be Equal As Strings    ${str}    0
    ${str}    Get From Dictionary    ${responsedata}    reason
    Should be Equal as strings    ${str}    Return Successd!
    Delete All Sessions

Post_Test_Status_KeyErro  # 测试keyerror的情况
    ${dict}    Create Dictionary    Content-Type=application/x-www-from-urlencoded  # post请求一般请求头
    Create Session    api    ${dict} # 创建session
    ${data}    Create Dictionary    qq=364026456    key=bcfe4b5c9f24176250f8a8202c6c68b8  # 构造参数并赋值给${data},这样做可以方便后面调用,特别是参数过多的时候
    ${addr}    Post Request    api    POST    data=${data}  # 拼接请求内容并发送post请求
    Log    ${addr.content}  # Post Request取得的值为字典类型,所以可以直接以${addr.content}的方式提取内容
    Log    ${addr.json()} 
    ${responsedata}    To Json    ${addr.content} # 转换为json格式
    ${str_resultcode}    Get From Dictionary    ${responsedata}    resultcode # 提取想要的key对应的值
    ${str_reason}    Get From Dictionary    ${responsedata}    reason
    Should Be Equal As Strings    ${str_resultcode}    101  # 断言
    Delete All Sessions # 清除所有session

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).