问题
I have a request which produces below response:
{
"totalRows": 10,
"colDefs": [
{
"entityAttributeId": "somestring",
"headerName": "somestring",
"field": "someNumber",
"entityPath": "",
"entityId": "somestring"
},
{
"entityAttributeId": "somestring",
"headerName": "somestring",
"field": "someNumber",
"entityPath": "somestring",
"entityId": "somestring"
},
{
"entityAttributeId": "somestring",
"headerName": "somestring",
"field": "1",
"entityPath": "",
"entityId": "somestring"
},
{
"entityAttributeId": "somestring",
"headerName": "somestring",
"field": "3",
"entityPath": "somestring",
"entityId": "somestring"
}
],
"rowData": [
{
"1": "",
"2": "s",
"3": "1939EX",
"4": "",
"rowMeta": {
"text/inthisform": {
"someid": "1939EX"
},
"somestring": {
"somestring": ""
}
}
},
{
"1": "",
"2": ",
"3": "1939A2",
"4": "",
"rowMeta": {
"text/inthisform": {
"someid": "1939A2"
},
"somestring": {
"somestring": "somevalue"
}
}
},
{
"1": "",
"2": "",
"3": "1939A1",
"4": "",
"rowMeta": {
"text/inthisform": {
"someid": "1939A1"
},
"somestring": {
"somevalue": "value"
}
}
},
{
"1": "",
"2": "",
"3": "193901",
"4": "",
"rowMeta": {
"sometext/inthisform": {
"somevalue": "193901"
},
"somevalue": {
"somevalue": "value"
}
}
}
]
}
Now I need to apply sorting feature over this field values."sometext/inthisform": { "somevalue": "193901" }, In Java, I can easily do this by storing this response an array and sort it and later compare it by my sort feature response.
With Karate, I have no clue, how to go forward.
I am trying below option: And def temp = ListDataSet_Response.rowMeta.rowData[*].3
or And def temp = ListDataSet_Response.rowMeta.sometext/inthisform.somevalue
to store the arraylist values. As Karate does not give any compilation or any kind of error information, I am not sure how to proceed in this cases?
Summary: I want to store the values of a field into an array and want to apply sort into it. After applying sort, I want to compare this new sorted array with my features sorted array response.
回答1:
Please refer to this example that even involves a case-insensitive sort. It is easy to dive into Java from Karate, especially for a complex use-case like this:
Scenario: case-insensitive sort
* def ArrayList = Java.type('java.util.ArrayList')
* def Collections = Java.type('java.util.Collections')
* def json = [{ v: 'C' }, { v: 'b' }, { v: 'A' }]
* def actual = $json[*].v
* match actual == ['C', 'b', 'A']
* def list = new ArrayList()
* eval for (var i = 0; i < actual.length; i++) list.add(actual[i])
* match list == ['C', 'b', 'A']
* eval Collections.sort(list, java.lang.String.CASE_INSENSITIVE_ORDER)
* match list == ['A', 'b', 'C']
Since your example is too complex, I can't provide a specific answer, kindly refer: https://stackoverflow.com/help/mcve
For a cleaner way to express the above code, refer the actual example, where you can move some of the code into JS: https://github.com/intuit/karate/blob/master/karate-junit4/src/test/java/com/intuit/karate/junit4/demos/sort-array.feature
回答2:
I found the solution by this way:
Store the JSONArray values with the help of jsonpath:
And def temp = $ListDataSet_Response.rowData[*].3
Call the sort functions directly in the feature file itself.
These lines added in feature file Background:
* def ArrayList = Java.type('java.util.ArrayList')
* def Collections = Java.type('java.util.Collections')
Added these line for Sorting in Ascending order:Created list and added values to this temp variable(stored response) into that list.Later,Applied sorting over that list.
And def listAsAscending = new ArrayList()
* eval for(var i = 0; i < sometextid.length; i++) listAsAscending.add(sometextid[i])
* eval Collections.sort(listAsAscending, java.lang.String.CASE_INSENSITIVE_ORDER)
* print listAsAscending
Similarly for Descending order:
And def listAsDescending = new ArrayList()
* eval for(var i = 0; i < sometextid.length; i++) listAsDescending.add(sometextid[i])
* eval Collections.sort(listAsDescending, Collections.reverseOrder())
* print listAsDescending
To compare the list, with my sort API response later:
* match sortedTemp == listAsAscending
* match sortedTemp == listAsDescending
来源:https://stackoverflow.com/questions/52928409/karate-api-testing-sorting-validation-scenario