我是新手,为了在我们的测试框架中实现它,我现在正在高兴。
我面临的问题是从REST响应中提取Json数组中的对象。
我正在使用的json示例:
{
"MRData": {
"xmlns": "http://ergast.com/mrd/1.4",
"series": "f1",
"url": "http://ergast.com/api/f1/2016/drivers.json",
"limit": "30",
"offset": "0",
"total": "24",
"DriverTable": {
"season": "2016",
"Drivers": [
{
"driverId": "alonso",
"permanentNumber": "14",
"code": "ALO",
"url": "http://en.wikipedia.org/wiki/Fernando_Alonso",
"givenName": "Fernando",
"familyName": "Alonso",
"dateOfBirth": "1981-07-29",
"nationality": "Spanish"
},
{
"driverId": "bottas",
"permanentNumber": "77",
"code": "BOT",
"url": "http://en.wikipedia.org/wiki/Valtteri_Bottas",
"givenName": "Valtteri",
"familyName": "Bottas",
"dateOfBirth": "1989-08-28",
"nationality": "Finnish"
}
]
}
}
}
到目前为止我尝试过的事情:
这个断言正在运作
RestAssured.rootPath = "MRData.DriverTable.Drivers";
given()
.when()
.get("http://ergast.com/api/f1/2016/drivers.json")
.then()
.assertThat()
.body("find { find { d -> d.driverId == 'alonso' }.code }.code", equalTo("ALO"));
但我正在尝试实际获取特定数组项的Json
RestAssured.rootPath = "MRData.DriverTable.Drivers";
given()
.when()
.get("http://ergast.com/api/f1/2016/drivers.json")
.then()
.extract()
//.jsonPath().param("driverId", "alonso").get("find { d -> d.driverId == driverId }");
.path("find { d -> d.driverId == 'alonso' }");
尝试两种方式(一种被注释掉)。但是我收到了一个错误:
"The parameter "driverId" was used but not defined. Define parameters using the JsonPath.params(...)"
答案 0 :(得分:0)
RestAssured.rootPath = "MRData.DriverTable.Drivers";
仅适用于身体期望。对于提取,您必须使用完整路径参数,例如MRData.DriverTable.Drivers.find { it.@driverId == 'alonso' }