For loop is not working as expected in Function in Karate

僤鯓⒐⒋嵵緔 提交于 2019-12-11 15:03:26

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!