繁体   English   中英

使用 GPath 和 Rest Assured 从响应元素获取值

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM