json - 如何从 Get Response Body - Robot 框架中获取值
问题描述
响应正文的输出
{"data":[{"id”:122,"name”:”Test 1“,”description”:”TEST 1 Test 2 …..}]},{"id”:123,"name”:”DYNAMO”……}]},{"id”:126,”name”:”T DYNAMO”……
*** Keywords ***
Capture The Data Ids
@{ids}= Create List 122 123 126 167 190
${header} Create Dictionary Authoriztion...
${resp} Get Response httpsbin /data
${t_ids}= Get Json Value ${resp.content} /data/0/id
问题
我在测试用例中创建了上述 id 的列表,我需要将创建的数据与响应正文中返回的 id 进行比较。t_ids 返回 122,当 0 被 1 替换时,返回 123
是否可以将它们放入 for 循环中,而不是捕获单个 id?
:FOR ${i} IN ${ids}
\ ${the_id= Get Json Value ${resp.content} /data/${i}/id ?
我试过这个但失败了。
将响应数据中的 id 与创建的列表进行比较的可能解决方案是什么?
谢谢你。
解决方案
你想要什么都是可能的,但是知道你的变量包含什么样的数据结构总是好的。在下面的示例中,加载 json 文件会替换${resp.content}
. 据我所知,这是一个字符串,它也是Get File
返回的。
该示例分为 json 文件和机器人文件。
so_json.json
{
"data":[
{
"id":122,
"name": "Test 1",
"description": "TEST 1 Test 2"
},
{
"id": 123,
"name": "DYNAMO"
},
{
"id": 126,
"name": "T DYNAMO"
}
]
}
so_robot.robot
*** Settings ***
Library HttpLibrary.HTTP
Library OperatingSystem
Library Collections
*** Test Cases ***
TC
${json_string} Get File so_json.json
${json_object} Parse Json ${json_string}
:FOR ${item} IN @{json_object['data']}
\ Log To Console ${item['id']}
这反过来又给出了以下结果:
==============================================================================
Robot - Example
==============================================================================
Robot - Example.SO JSON
==============================================================================
TC 122
123
126
| PASS |
------------------------------------------------------------------------------
Robot - Example.SO JSON | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Robot - Example | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
推荐阅读
- python - 如果发生异常,如何重新启动尝试循环
- c# - 添加附加条件时 LINQ 查询失败
- python-3.x - 如何缩放包含 NaN 值的矩阵?
- mysql - 带有 CONCAT 的多个左连接返回 null
- mysql - SQL IN 和 OR 运算符的区别
- c++ - 具有默认分配器初始化为空的 std::map 是否分配内存?
- mysql - 在插入新数据之前检查表中是否存在记录
- authentication - 赛普拉斯不适用于外部登录
- github - 尝试在 Github .md 文件上渲染图表时出现美人鱼语法错误
- jquery - jQuery Smart Wizard v4 Step Validaton 使用 jquery 验证插件