首页 > 解决方案 > 如何在空手道中使用 JavaScript 函数根据标签从 xml 中查找值

问题描述

如果我想获取空手道中某个元素的值,我可以使用“response //RecordA/RecordSequence”。如何使用以下方法在函数中获得相同的值。我已经尝试过 .getElementsByTagName、.find 和 karate.get,但它在基于标签查找特定值的函数下不起作用。任何帮助都可以根据功能下的标签找到价值。

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>'

标签: javascriptkarate

解决方案


干得好:

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

推荐阅读