如何通过在 GET URL of API testing using karate 中将 ID 用作参数来一次获取一个动态 ID 列表
How to fetch the list of dynamic ID one at a time by applying ID as a parameter in GET URL of API testing using karate
我试图通过应用程序 ID 作为 GET 中的参数一次获取一个动态 ID 列表 URL
示例:下面的响应是针对 POST 调用的
{“汽车”:1,
内容:[{
“类型”:“A”
“编号”:“1”
},
{
“类型”:“B”
“编号”:“2”
}
]}
现在对于上面的 POST 响应,我正在尝试使用动态 ID 作为 GET URL 中的参数来获取数据
例如:
- def ID = karate.jsonPath(响应, '$.content[*].id')
给定 URL 'https://localhost:8080'
和路径 '/'+ID+'/id
当方法 GET
然后状态200
在 GET 响应中,我在 URL 中获取 ID 列表而不是单个 ID,如下所示
这是输出:http://localhost:8080/1,2/id
由于 ID 是动态生成的,所以我不想手动调用一个一个的 ID,而是想使用参数进行调用
谁能建议我如何使用 GET URL 一次获取一个 ID?
试试这个例子,观察输出,然后试着理解它是如何工作的:
* def response = {"Car": 1, content:[{ "type" : "A", "Id" : "1" }, { "type" : "B", "Id" : "2" } ]}
* def ids = $response.content[*].Id
* match ids == ['1', '2']
* def data = karate.mapWithKey(ids, 'id')
* call read('called.feature') data
而 called.feature
看起来像这样:
@ignore
Feature:
Scenario:
* url 'https://httpbin.org/anything'
* param id = id
* method get
请尝试阅读文档,这是值得的:https://github.com/karatelabs/karate#json-transforms
我试图通过应用程序 ID 作为 GET 中的参数一次获取一个动态 ID 列表 URL
示例:下面的响应是针对 POST 调用的 {“汽车”:1, 内容:[{ “类型”:“A” “编号”:“1” }, { “类型”:“B” “编号”:“2” } ]}
现在对于上面的 POST 响应,我正在尝试使用动态 ID 作为 GET URL 中的参数来获取数据 例如:
- def ID = karate.jsonPath(响应, '$.content[*].id') 给定 URL 'https://localhost:8080' 和路径 '/'+ID+'/id 当方法 GET 然后状态200
在 GET 响应中,我在 URL 中获取 ID 列表而不是单个 ID,如下所示 这是输出:http://localhost:8080/1,2/id
由于 ID 是动态生成的,所以我不想手动调用一个一个的 ID,而是想使用参数进行调用
谁能建议我如何使用 GET URL 一次获取一个 ID?
试试这个例子,观察输出,然后试着理解它是如何工作的:
* def response = {"Car": 1, content:[{ "type" : "A", "Id" : "1" }, { "type" : "B", "Id" : "2" } ]}
* def ids = $response.content[*].Id
* match ids == ['1', '2']
* def data = karate.mapWithKey(ids, 'id')
* call read('called.feature') data
而 called.feature
看起来像这样:
@ignore
Feature:
Scenario:
* url 'https://httpbin.org/anything'
* param id = id
* method get
请尝试阅读文档,这是值得的:https://github.com/karatelabs/karate#json-transforms