Serenity-BDD jsonPath 验证

Serenity-BDD jsonPath validation

我正在做一个平静测试,想在 json 上做一些验证。 我得到的 json 看起来像

{
    "devices": [
        {
            "deviceId": "0",
            "deviceName": "Device-0",
            "deviceStatus": "free",
            "claimer": "-",
            "claimedUntil": null
        },
        {
            "deviceId": "1",
            "deviceName": "Device-1",
            "deviceStatus": "free",
            "claimer": "-",
            "claimedUntil": null
        },
        ...
    ]
}

这个json中的设备顺序在我做了一些操作后发生了变化。我想通过 Id 获取设备并验证“claimer”和“deviceStatus”。

我的代码如下:

@Then("see if device with id {string} is status {string}")
public void checkIfDeviceIsClaimed(String deviceId, String status) {
    theActorInTheSpotlight().should(seeThatResponse("Get valid",
        res -> res.statusCode(200)
            .body("$.devices.?(@.deviceId == " + deviceId + ").claimer", equalTo(theActorInTheSpotlight().getName()))
            .body("$.devices.?(@.deviceId == " + deviceId + ").deviceStatus", equalTo(status))));
  }

我的错误看起来像

Step failed
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 1: Unexpected input: 'restAssuredJsonRootObject.$.devices.[' @ line 1, column 37.
   restAssuredJsonRootObject.$.devices.[?(@.deviceId == 0)].claimer
                                       ^

是否有可能更改 JsonPath 库或实现正确的行为?

非常感谢

您可以通过使用正确的语法得到结果。

.body("devices.find {it.deviceId == '" + deviceId + "'}.claimer", equalTo(theActorInTheSpotlight().getName()))
.body("devices.find {it.deviceId == '" + deviceId + "'}.deviceStatus", equalTo(status))));