使用soapui进行XML响应的Groovy Script断言?

时间:2017-11-06 09:17:55

标签: xml groovy 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"

1 个答案:

答案 0 :(得分:2)

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

def xml = '''<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>
  <food>
    <name>Belgian Waffles</name>
    <price>$5.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>$7.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)返回引用第一个(根)XML节点元素的节点。然后,您可以执行与JsonSlureper类相同的操作。