我正在打api - > http://127.0.0.1:5000/api/library/?book_id=2,5,13
api的反应是:
{
"data": {
"2":{
"rack": 219,
"price": 360,
"title": "book1"
},
"5":{
"rack": 309,
"price": 230,
"title": "book2"
},
"13":{
"rack": 112,
"price": 200,
"title": "book3"
}
},
"status_code": 200
}
键2,5,13是可变的,具体取决于api param。
我写了以下代码=>
Feature: Verify Library API
Scenario: Verify book prices
* def id1 = 2
* def id2 = 5
* def id3 = 13
Given url 'http://127.0.0.1:5000/api/library/?book_id='+ id1 +',' + id2 + ',' + id3
When method get
Then status 200
* def id1_price = $.data.#(id1).price
* assert id1_price == 360
我无法获得变量中book1价格的值,导致断言失败。如何使用变量" id1"?
访问价格答案 0 :(得分:1)
如果有动态JsonPath表达式,请使用karate.jsonPath()
API。试试这个:
* def id1_price = karate.jsonPath(response, "$.data." + id1 + ".price")