如何使用 Rest Assured 在 Response API 中获取两个具有相同名称的不同字段?
How to get two different fields with the same name in a Response API using Rest Assured?
我正在尝试使用 Rest-Assured 和 Java.
为 GET API 创建测试自动化
此 API 具有以下响应正文:
{
"items": [
{
"id": "3185",
"customer_id": "299",
"region": "São Paulo",
"region_id": 1234,
"country_id": "BR",
"street": [
"Av Paulista"
],
"company": "Teste",
"telephone": "(19) 99999-9999",
"postcode": "",
"city": "Valinhos",
"firstname": "N/A",
"lastname": "N/A",
"middlename": null,
"prefix": null,
"suffix": null,
"person_type": "PF",
"document": "43448871703",
"state_registry": null,
"created_at": "2019-07-24 13:03:29"
},
{
"id": "3188",
"customer_id": "299",
"region": "São Paulo",
"region_id": 1234,
"country_id": "BR",
"street": [
"Av Paulista"
],
"company": "Teste",
"telephone": "(19) 99999-9999",
"postcode": "",
"city": "Valinhos",
"firstname": "N/A",
"lastname": "N/A",
"middlename": null,
"prefix": null,
"suffix": null,
"person_type": "PJ",
"document": "84047942000115",
"state_registry": null,
"created_at": "2019-07-24 13:03:30"
}
]
}
在此 API 响应中有两个同名字段 "id"。如何获取这两个字段的值?
谢谢
看看这篇文章:https://techeplanet.com/parse-json-array-using-rest-assured/
@Test
public void verifyJSONArrayResponse() {
JsonArray jsonArray = new JsonArray();
jsonArray = given().baseUri("http://<your host>")
.basePath("<your path>")
.get().as(JsonArray.class);
for (int i = 0; i < jsonArray.size(); i++) {
JsonObject jsonObject = jsonArray.get(i).getAsJsonObject();
System.out.println(jsonObject.get("id").getAsString());
}
}
您需要稍微调整一下,首先从顶级响应对象中提取 items
(您的数组)。
您可以使用 JsonPath 轻松做到这一点:$.items[*].id
这将为您提供两个 ID。
由于您使用的是 REST-assured,您可以像这样直接从响应本身中提取您想要的内容:
List<Integer> = given()
.spec(yourRequestSpecification)
.get("/your_api_endpoint") // returns Response
.then() // returns ValidatableResponse
.extract() // returns ExtractableResponse
.path("items.id"); // Groovy GPath syntax
Jayway's jsonpath and REST-assured's jsonpath 使用不同的语法
您可以试试下面的代码。
import org.json.JSONArray;
import org.json.JSONException;
import org.testng.annotations.Test;
import bsh.ParseException;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
public class KeyValueJsonArrayExample {
@Test
public void getResponseAsJsonArray() throws ParseException, JSONException {
RequestSpecification myreq = RestAssured.given();
Response MyRes;
MyRes = myreq.get("http://localhost:3000/posts/");
String jsonResponse = MyRes.asString();
System.out.println("Full String is" + jsonResponse);
JSONArray jsonArray = new JSONArray(jsonResponse);
for (int i = 0; i < jsonArray.length(); i++) {
org.json.JSONObject jsonObject1 = jsonArray.getJSONObject(i);
String value1 = jsonObject1.optString("id");
String value2 = jsonObject1.optString("auhtor");
String value3 = jsonObject1.optString("title");
System.out.println("The values are" + value1 + value2 + value3);
}
}
}
另外如果你只需要获取特定的索引,请尝试
Response response=given().contentType(ContentType.JSON).get("http://localhost:3000/posts");
JsonPath jsonPathEvaluator = response.jsonPath();
String title = jsonPathEvaluator.get("title[2]");
String author=jsonPathEvaluator.get("author[2]");
我正在尝试使用 Rest-Assured 和 Java.
为 GET API 创建测试自动化此 API 具有以下响应正文:
{
"items": [
{
"id": "3185",
"customer_id": "299",
"region": "São Paulo",
"region_id": 1234,
"country_id": "BR",
"street": [
"Av Paulista"
],
"company": "Teste",
"telephone": "(19) 99999-9999",
"postcode": "",
"city": "Valinhos",
"firstname": "N/A",
"lastname": "N/A",
"middlename": null,
"prefix": null,
"suffix": null,
"person_type": "PF",
"document": "43448871703",
"state_registry": null,
"created_at": "2019-07-24 13:03:29"
},
{
"id": "3188",
"customer_id": "299",
"region": "São Paulo",
"region_id": 1234,
"country_id": "BR",
"street": [
"Av Paulista"
],
"company": "Teste",
"telephone": "(19) 99999-9999",
"postcode": "",
"city": "Valinhos",
"firstname": "N/A",
"lastname": "N/A",
"middlename": null,
"prefix": null,
"suffix": null,
"person_type": "PJ",
"document": "84047942000115",
"state_registry": null,
"created_at": "2019-07-24 13:03:30"
}
]
}
在此 API 响应中有两个同名字段 "id"。如何获取这两个字段的值?
谢谢
看看这篇文章:https://techeplanet.com/parse-json-array-using-rest-assured/
@Test
public void verifyJSONArrayResponse() {
JsonArray jsonArray = new JsonArray();
jsonArray = given().baseUri("http://<your host>")
.basePath("<your path>")
.get().as(JsonArray.class);
for (int i = 0; i < jsonArray.size(); i++) {
JsonObject jsonObject = jsonArray.get(i).getAsJsonObject();
System.out.println(jsonObject.get("id").getAsString());
}
}
您需要稍微调整一下,首先从顶级响应对象中提取 items
(您的数组)。
您可以使用 JsonPath 轻松做到这一点:$.items[*].id
这将为您提供两个 ID。
由于您使用的是 REST-assured,您可以像这样直接从响应本身中提取您想要的内容:
List<Integer> = given()
.spec(yourRequestSpecification)
.get("/your_api_endpoint") // returns Response
.then() // returns ValidatableResponse
.extract() // returns ExtractableResponse
.path("items.id"); // Groovy GPath syntax
Jayway's jsonpath and REST-assured's jsonpath 使用不同的语法
您可以试试下面的代码。
import org.json.JSONArray;
import org.json.JSONException;
import org.testng.annotations.Test;
import bsh.ParseException;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
public class KeyValueJsonArrayExample {
@Test
public void getResponseAsJsonArray() throws ParseException, JSONException {
RequestSpecification myreq = RestAssured.given();
Response MyRes;
MyRes = myreq.get("http://localhost:3000/posts/");
String jsonResponse = MyRes.asString();
System.out.println("Full String is" + jsonResponse);
JSONArray jsonArray = new JSONArray(jsonResponse);
for (int i = 0; i < jsonArray.length(); i++) {
org.json.JSONObject jsonObject1 = jsonArray.getJSONObject(i);
String value1 = jsonObject1.optString("id");
String value2 = jsonObject1.optString("auhtor");
String value3 = jsonObject1.optString("title");
System.out.println("The values are" + value1 + value2 + value3);
}
}
}
另外如果你只需要获取特定的索引,请尝试
Response response=given().contentType(ContentType.JSON).get("http://localhost:3000/posts");
JsonPath jsonPathEvaluator = response.jsonPath();
String title = jsonPathEvaluator.get("title[2]");
String author=jsonPathEvaluator.get("author[2]");