How to find value from xml based on tag using JavaScript function in Karate

Viewed 145

If I want to get the value of an element in karate, I can use "response //RecordA/RecordSequence". How to get the same value in a function using below approach. I have tried .getElementsByTagName, .find and karate.get but it does not work under a function to find a specific value based on tag. Any help appreciated to find value based on tag under function.

Background:
* def myxml =
      """
        function(tags) 
         {
          var xmlValue= tags.getElementsByTagName("RecordA"); // Getting error as 
                                                              .getElementsByTagName is not supported
          
          return xmlValue;
        }        
      """ 

Given request 
when soap action ''
Then status 200
* def resxml = response
* def myresult= myxml(resxml)
And assert result100 = '<expected_desc1>'|| result100 = '<expected_desc1>'
1 Answers

Here you go:

* def temp = <root><hello>world</hello></root>
* def fun = function(data){ return karate.xmlPath(data, '/root/hello') }
* def val = fun(temp)
* match val == 'world'
Related