【问题标题】:How to check if a value displays in particular format in XML response in soapui如何检查一个值是否在soapui的XML响应中以特定格式显示
【发布时间】:2016-04-18 19:53:46
【问题描述】:

我正在尝试使用脚本断言或 groovy 代码来检查 xml 响应中元素下的值是否以特定格式退出。

示例响应:

                 <ns5:price>99.99</ns5:price>
                 <ns5:date>2016-04-04</ns5:date>
                 <ns5:quantity>1</ns5:quantity>

例如,我想检查是否存在任何价格,格式为5,4,日期相同,以查看格式为yyyy-mm-dd 的日期和数量是否不是0

所有值都是动态的。

我想知道我们是否可以使用脚本断言,或者可以使用soap ui pro 的指向和单击。

我只是在学习soapui pro和groovy。

谢谢。

【问题讨论】:

  • 这些值是否在响应中重复?您可以使用脚本断言。
  • 检查here关于日期格式

标签: groovy soapui


【解决方案1】:

您可以在 SOAP 测试步骤 中创建一个脚本断言 并在那里进行验证。在脚本断言中,您可以使用XmlSlurper 解析响应,然后使用findAll 获取所需的节点并执行所有断言。你可以使用类似的东西来做到这一点:

// from script assertion get the response 
def response = messageExchange.getResponseContent()
// parse the XML
def xml = new XmlSlurper().parseText(response)
// get all prices
def prices = xml.'**'.findAll { it.name() == 'price' }
// check that each one has at "max 5 digits.max 4 digits"
prices.each { assert it ==~ /\d{0,5}\.\d{0,4}/ }
// get all dates
def date = xml.'**'.findAll { it.name() == 'date' }
// check the date has yyyy-MM-dd format
date.each { assert it  ==~ /\d{4}\-\d{2}\-\d{2}/ }
// get all quantities
def quantity = xml.'**'.findAll { it.name() == 'quantity' }
// check that all quantities are > 0
quantity.each { assert it.text().toInteger() > 0 }

注意:要向您的请求添加 脚本断言,请单击 SOAP 请求左下角的 断言 选项卡 面板:

然后右键单击它并选择添加断言

在新面板中,从左侧菜单中选择 Script,然后在右侧选项中选择 Script Assertion

最后使用提供的代码并进行检查:)

【讨论】:

  • @user6221615 很高兴帮助你,如果答案能帮助你思考accept it:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-03
  • 2019-03-15
相关资源
最近更新 更多