[英]Get random field from Rest Assured Json response with multiple items
從Ruby遷移到Java,我需要從多項響應中隨機解析並獲取一個字段。
這是我用來獲取響應的ApiCall方法:
Response response = given().
headers(this.headers).
params(this.body).
when().
post(this.url).
then().
contentType(ContentType.JSON).
statusCode(200).
extract().
response();
它給了我這個JSON結構:
{
"Contents": {
"Title": "Search results",
"Count": "10",
"Page": "1",
"TotalCount": "1",
"TotalPages": 2,
"Genres": [
"Genre_1",
"Genre_2",
"Genre_3"
],
"Contents": [
{
"title": "content1_title",
"original_text": "original text 1",
"full_text": "Sample full sized text 1",
"short_text": "Sample short text 1",
"children_ids": {
"item": [
1,
2
]
},
"children_uuids": {
"item": [
"item1_uuid",
"item2_uuid"
]
},
"parent_ids": {
"item": [
1
]
},
"parent_uuids": {
"item": [
"item1_uuid"
]
},
"aired_from": "1994-01-01",
"aired_to": "1994-12-31",
"tags": {
"item": [
""
]
},
"available_condition1": 0,
"available_condition2": 1,
"price_condition1": "0.00",
"price_condition2": "13.00"
},
{
"title": "content2_title",
"original_text": "original text 2",
"full_text": "Sample full sized text 2",
"short_text": "Sample short text 2",
"children_ids": {
"item": [
1,
2
]
},
"children_uuids": {
"item": [
"item1_uuid",
"item2_uuid"
]
},
"parent_ids": {
"item": [
1
]
},
"parent_uuids": {
"item": [
"item1_uuid"
]
},
"aired_from": "1998-01-01",
"aired_to": "1998-01-31",
"tags": {
"item": [
""
]
},
"available_condition1": 0,
"available_condition2": 1,
"price_condition1": "0.00",
"price_condition2": "13.00"
}
]
},
"Success": true
}
問題是我需要得到一個隨機的“標題”字段,該字段至少有一個“children_uuids”來自響應中的一個“child_uuids”。
據我了解,步驟如下:
1)獲取“Contents.Contents”項的總大小。 2)獲得0到項目總數之間的隨機數。 3)使用該數字選擇一個具有“Contents.Contents。[n] .title”格式或類似的項目。
試過以下但沒有成功:
JsonPath jsonPath = new JsonPath(response.toString());
List<?> list = jsonPath.getList("Contents.Contents.title.flatten()");
System.out.println(list);
這給了我以下錯誤:
io.restassured.path.json.exception.JsonPathException: Failed to parse the JSON document
作為參考,在Ruby中代碼將是:
amount = @result['api_response']['Contents']['Contents'].count
amount = amount - 1
a = rand(0..amount)
while @result['api_response']['Contents']['Contents'][a]['children_uuids']['item'].nil? do
a = rand(0..amount)
#children_uuids = $result['api_response']['Contents']['Contents'][a]['children_uuids']
end
#price_hd = @result['api_response']['Contents']['Contents'][a]['price_hd']
content_title = @result['api_response']['Contents']['Contents'][a]['title']
更新:我已經部分工作了......我找到了一種方法從列表中選擇一個項目:
String contentTitle = response.then().extract().path("Contents.Contents[0].title");
但是找不到使用這個jsonpath的方法
String contentTitle = response.then().extract().path("Contents.Contents.[?(@.children_uuids)].uuid");
第二行給了我:
java.lang.IllegalArgumentException:
Invalid JSON expression:
Script1.groovy: 1: unexpected token: [ @ line 1, column 45.
nRootObject.Contents.Contents.[?(@.child
^
提前致謝。
在我看來,這在REST保證中很難做到,甚至可能是Java。
我可以建議你看看空手道 (免責聲明:我是開發者)。 因此,您可以調用JavaScript函數來生成隨機數,將其分配給變量,然后形成路徑表達式以完全按照您的意願執行操作:
Feature:
Scenario:
* def data = read('data.json')
* def size = (data.Contents.Contents.length)
* def index = Math.floor(Math.random() * size)
* print 'selected index: ' + index
* def item = (data.Contents.Contents[index])
* def children_uuids = item.children_uuids
* match children_uuids == { item: ['item1_uuid', 'item2_uuid'] }
更新:我找到了一個包含以下代碼的解決方案:
// get random content data
Integer contentAmmount = response.body().path("Contents.Contents.size()");
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(contentAmmount);
while (response.body().path(
String.join("", "Contents.Contents[", Integer.toString(randomInt),
"].children_uuids.item")).toString().isEmpty() ) {
randomInt = randomGenerator.nextInt(contentAmmount);
}
String contentTitle = response.then().extract()
.path(String.join("", "Contents.Contents[", Integer.toString(randomInt), "].title"));
String contentUuid = response.then().extract()
.path(String.join("", "Contents.Contents[", Integer.toString(randomInt), "].uuid"));
String contentChildrenUuid = response.body().path(
String.join("", "Contents.Contents[", Integer.toString(randomInt),
"].children_uuids.item"));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.