Groovy XML 响应的脚本断言使用 soapui?

Groovy Script Assertion for XML response using soapui?

我只使用 JSON 响应,为了验证我使用了下面的脚本。现在我需要对 XML 响应进行类似的验证。 XML 如何做到这一点?

import groovy.json.JsonSlurper 

def response = messageExchange.response.responseContent
def slurper = new JsonSlurper()
def json = slurper.parseText response

assert json.name == "ABCD"
assert json.status == "Success"

您可以简单地使用与 JsonSlurper 非常相似的 XmlSlurper class。假设这是你的 XML,你可以这样做:

def xml = '''<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>
  <food>
    <name>Belgian Waffles</name>
    <price>.95</price>
    <description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
    <calories>650</calories>
  </food>
  <food>
    <name>Strawberry Belgian Waffles</name>
    <price>.95</price>
    <description>Light Belgian waffles covered with strawberries and whipped cream</description>
    <calories>900</calories>
  </food>
</breakfast_menu>
'''

def root = new XmlSlurper().parseText(xml)

assert root.food[0].name.text() == 'Belgian Waffles'

请记住 new XmlSlurper().parseText(xml) returns 引用第一个(根)XML 节点元素的节点。然后,您可以执行与 JsonSlureper class.

几乎相同的操作