1

I am getting below as result as output db query :-

11:53:30.608 [pool-1-thread-1] INFO  com.intuit.karate - [print] AgendaPoints-> [
  {
    "CAAI_ID": 5,
    "CAAI_DOSSIERNUMMER": 268457,
    "CAAI_EVENT_ID": " ",
    "CAAI_ITEM_VOLGNUMMER": "1",
    "CAAI_ITEM_TITEL": "Hi Rohit",
    "CAAI_ITEM_TEKST": "test",
    "CAAI_ITEM_INFO_OF_STEM": "I",
    "CAAI_ITEM_STATUS": "ACTV"
  }

Since it is List type and when I match from response xml value as below:-

* def InfoOrVotingRight = $AgendaPoints[*].CAAI_ITEM_INFO_OF_STEM
* def fun = function(i){ return InfoOrVotingRight[i] == "I" ? "INFO" : "VOTINGRIGHT" }
* def infoOrVotingRight = karate.repeat(InfoOrVotingRight.length, fun)

Then match  response //shareholderMeeting[1]/resolution[*]/infoOrVotingRight == infoOrVotingRight

Then I get below error:-

//shareholderMeeting[1]/resolution[*]/infoOrVotingRight, actual: 'INFO', expected: [INFO], reason: actual value is not list-like

Below is the xml tag which I am trying to match :-

 <ns3:resolution>
          <ns3:description>test</ns3:description>
          <ns3:infoOrVotingRight>INFO</ns3:infoOrVotingRight>
          <ns3:label>1</ns3:label>
          <ns3:status>ACTIVE</ns3:status>
          <ns3:title>Hi Rohit</ns3:title>
        </ns3:resolution>

Some times this tag will come one time and sometimes it comes more than 1 times so when this xml tag comes 1 time it returns a String value but when it comes more than 1 time it returns like List value. So when it returns a list value it gets pass but when it returns a single value it gets failed. To overcome this issue I wrote below function so I can convert a list value into a String value when response returns a single xml tag . But I am not able to make this parameterized. The reason I want to make this as a generic function because this happens for multiple tags and I can not hard code the map(key value) for one specific tag and I want to use this function for other similar situations.

* def myfun = 
"""
function(arg,arg2){
if(arg.length == 1){
return arg[0].arg2
}
else{
var list = []

for(i=0;i<arg.length; i++){
list.push(arg[i].arg2)
}
return list

}

}



"""

* def funvalue = call myfun(AgendaPoints,CAAI_ITEM_INFO_OF_STEM)

Below is the hardcoded way of the same function :-

* def myfun = 
    """
    function(arg){
    if(arg.length == 1){
    return arg[0].CAAI_ITEM_INFO_OF_STEM
    }
    else{
    var list = []
    
    for(i=0;i<arg.length; i++){
    list.push(arg[i].CAAI_ITEM_INFO_OF_STEM)
    }
    return list
    
    }
    
    }
    
    
    
    """

    * def funvalue = call myfun AgendaPoints

Since AgendaPoints is a list and it contains map value and I need to pass 2nd argument in my function as string so it gets value of a key(as my 2nd argument) so i can get the value of map key as an argument. But it os not working as expected.

  • linked question should answer all your questions. tip: convert XML to JSON if needed. also see: https://stackoverflow.com/a/53474845/143475 - if still stuck, please re-ask but this time, please simplify your question – Peter Thomas Apr 12 '21 at 12:00

0 Answers0