问题
I am using below code to get the JSON response from the array but I am able to get only till a position of 12. the script is passing but I am expecting more than 218 values to get printed.
Feature: Verify Branches
Background: For loop implementation
Given url ''
When method GET
Then status 200
* def i = 000
* def z = response.locations[i].zip
* def p = response.locations[i].phone
* def fun =
"""
function(locations){
for (var i = 0; i < response.locations.length; i++)
{
print(i)
print('Element at Location ' + i +':' + p)
}
}
"""
Scenario: Validate the locations
Given url ''
When method GET
Then status 200
* call fun z
* print z.length
z.length should give me 218 but giving 218 for z it is printing 10 and for p it is printing 12.
回答1:
Please use match each
in Karate. You never need to programmatically do it.
* def data = { foo: [{ bar: 1, baz: 'a' }, { bar: 2, baz: 'b' }, { bar: 3, baz: 'c' }]}
* match each data.foo == { bar: '#number', baz: '#string' }
来源:https://stackoverflow.com/questions/51382864/for-loop-is-not-working-as-expected-in-function-in-karate