For 循环在 Karate 的 Function 中没有按预期工作

For loop is not working as expected in Function in Karate

我正在使用下面的代码从数组中获取 JSON 响应,但我只能获取到位置 12。脚本正在通过,但我希望打印超过 218 个值.

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 应该给我 218 但给 218 z 打印 10 而 p 打印 12.

请在空手道中使用match each。您永远不需要以编程方式执行此操作。

* def data = { foo: [{ bar: 1, baz: 'a' }, { bar: 2, baz: 'b' }, { bar: 3, baz: 'c' }]}

* match each data.foo == { bar: '#number', baz: '#string' }