Forum Discussion

kevman's avatar
kevman
Occasional Contributor
13 years ago

Loop in assertion to check multiple nodes??

Hi,

I'm trying to create an assertion that the following response, which returns multiple nodes and need an assertion that if there is one node with specific user returned, then considered as pass. (In this case, if I have a user, 'bbb' in response, then pass.)

The number and order of users returned are random, so I couldn't use fixed XPath content check, like
assert '.../ns1:name[0]/text()'=aaa,
assert '.../ns1:name[1]/text()'=bbb
assert '.../ns1:name[2]/text()'=ccc

What I've tried is, make a loop like this, but never works. Is there any way to make this kind of assertion working?

for (i=0;i<count;i++){
assert '.../ns1:name/text()'
}

===Response===
{Users:
{User: [
{userid:aaa,
name:naa,
score:saa},

{userid:bbb,
name:nbb,
score:sbb},

{userid:ccc,
name:ncc,
score:scc},
....]}

3 Replies

  • you can use a map to check your values.


    map = [
    'ID' : ['name', 'score'],
    'ID1' : ['name1', 'score1']
    ]

    //in your code, where you go through each node
    ....
    if(map['.../ns1:userid[i]/text()']) {
    assert map['.../ns1:userid[i]/text()''][0] = '.../ns1:name[i]/text()'
    assert map['.../ns1:userid[i]/text()''][1] = '.../ns1:score[i]/text()'
    }
    ...






    here is an example click me
  • kevman's avatar
    kevman
    Occasional Contributor
    Thanks, I didn't use map for my original question, because my response is json, and somehow I found a way to use jslurper and worked.
    But I use your idea of map into another issue to concat 2 different data files.

    Thanks again.
  • Actually its almost the same code, see the example:



    import groovy.json.JsonSlurper

    map = [
    10 : ['name_1', 1],
    20 : ['name_2', 2],
    30 : ['name_3', 4]
    ]

    response =
    """
    {"Users":
    {"User": [
    { "userid":10,
    "name":"name_1",
    "score":1},
    { "userid":20,
    "name":"name_2",
    "score":2},
    { "userid":30,
    "name":"name_3",
    "score":3},
    ]
    }
    }
    """


    def slurper = new JsonSlurper()
    def result = slurper.parseText(response)

    result.Users.User.each{
    log.info it.name + ' : ' + it.score + ' : ' + it.userid

    assert map[it.userid][0] == it.name, "Error in name. Got ${it.name} expected ${map[it.userid][0]}"
    assert map[it.userid][1] == it.score, "Error in score. Got ${it.score} expected ${map[it.userid][1]}"
    }