[英]Getting the value from the response element using GPath and Rest Assured
我想從我的回復中獲取status
值。 這樣我就可以斷言了。 我正在使用 rest 保證 java & serenity BDD。
回復
{
"locationType": "STORE",
"locationId": "0003",
"events": {
"66e326db-fbfb-4f6e-9d2b-9425e7test5": {
"status": "BOOKING_OPEN"
}
}
}
因此,這里的event id (66e326db-fbfb-4f6e-9d2b-9425e7test5)
是動態的,這意味着每次運行此 UUID 都會發生變化。
代碼
Response response = SerenityRest.lastResponse();
final ValidatableResponse validatableResponse = response.then();
validatableResponse.assertThat().body("events.*.status", containsString(expectedResponse));
當我運行它時,我從 serenity BDD 得到無法識別的異常。 我認為,在 JSON 中遍歷存在一些問題。
有人可以幫我在這里獲得地位的價值嗎? 所以在這種情況下,我正在尋找BOOKING_OPEN
我認為您應該將 UUID 存儲為變量,並根據您的響應更改定位器。
response.getBody().jsonPath().get("events."+yourUUID+".status");
Groovy JsonSlurper 不支持*
breadthFirst()
或**
depthFirst()
標記。
您可以使用下面的一個來獲取字符串結果:
response.getBody().jsonPath().get("events.collect{it.value.status}.find()");
// would return "BOOKING_OPEN"
或低於一個以獲得列表結果:
response.getBody().jsonPath().get("events.collect{it.value.status}");
//would return ["BOOKING_OPEN"]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.