如何使用soapUI中的groovy脚本声明具有特定值的xml元素

时间:2014-01-04 18:46:15

标签: groovy soapui assert

建议我的xml在

之下
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <GetCitiesByCountryResponse xmlns="http://www.webserviceX.NET">
         <GetCitiesByCountryResult><![CDATA[<NewDataSet>
<Tables>
  <Table>
    <Country>British Indian Ocean Territory</Country>
    <City>Diego Garcia</City>
  </Table>
</Tables>
<Tables>
  <Table>
    <Country>India</Country>
    <City>Ahmadabad</City>
  </Table>
  <Table>
    <Country>USA</Country>
    <City>Akola</City>
  </Table>
  <Table>
    <Country>India</Country>
    <City>Aurangabad</City>
  </Table>
</Tables>

我想断言,其中一个城市元素是“艾哈迈德巴德”。我怎么能用soapUI和groovy来做呢

1 个答案:

答案 0 :(得分:1)

假设你的xml在一个字符串中,如:

def xml = '''<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
            |    <soap:Body>
            |        <GetCitiesByCountryResponse xmlns="http://www.webserviceX.NET">
            |            <GetCitiesByCountryResult><![CDATA[<NewDataSet>
            |                <Tables>
            |                    <Table>
            |                        <Country>British Indian Ocean Territory</Country>
            |                        <City>Diego Garcia</City>
            |                    </Table>
            |                </Tables>
            |                <Tables>
            |                    <Table>
            |                        <Country>India</Country>
            |                        <City>Ahmadabad</City>
            |                    </Table>
            |                    <Table>
            |                        <Country>USA</Country>
            |                        <City>Akola</City>
            |                    </Table>
            |                    <Table>
            |                        <Country>India</Country>
            |                        <City>Aurangabad</City>
            |                    </Table>
            |                </Tables>
            |            </NewDataSet>]]></GetCitiesByCountryResult>
            |        </GetCitiesByCountryResponse>
            |    </soap:Body>
            |</soap:Envelope>'''.stripMargin()

然后,您可以执行此操作以获取城市名称列表:

def cdata = new XmlSlurper().parseText( xml )
                            .Body
                            .GetCitiesByCountryResponse
                            .GetCitiesByCountryResult
                            .text()
def cities = new XmlSlurper().parseText( cdata ).Tables.Table.City*.text()

然后你可以通过以下方式查看你所在的城市:

assert cities.contains( 'Ahmadabad' )