【问题标题】:XPath result returns an arrayXPath 结果返回一个数组
【发布时间】:2017-11-21 14:19:57
【问题描述】:

我最近升级到了 SoapUI Pro (ReadyAPI),但遇到了以下问题。我有一个这样的 XPath 断言:

声明命名空间ns2='http://some.schema.tld/foo/bar/1.0';//ns2:GenerateOrResetPasswordFault//faultCode

它与我在 faultCode 元素中获得的特定值相匹配。但是,我在同一个父级中获得了其中两个,因此响应包含类似

//..
<GenerateOrResetPasswordFault>
    <faultCode>123</faultCode>
    <faultCode>456</faultCode>
</GenerateOrResetPasswordFault>
//...

我之前有两个断言,一个与 123 匹配,另一个是相同的 XPath,与 456 匹配并且它有效。现在切换断言失败后,因为 XPath 实际上返回 [123,456] 作为结果。

请问最好的处理方法是什么?

【问题讨论】:

  • 那是不是不允许断言列表?
  • @Rao 是的,但不幸的是,我不能相信列表的顺序相同。是否可以做一个列出元素的断言,但不是文字比较?
  • 然后就可以使用脚本断言了。你同意吗?
  • @Rao 如果这是唯一的选择,那么我别无选择。我将不得不将此转发给我们的一些不太热衷于脚本编写的测试人员,这有点问题,所以我试图找到一个更简单的解决方案。我很惊讶它实际上比在 SoapUI 中更复杂
  • 辍学,请检查解决方案,看看是否有帮助。

标签: xml xpath groovy soapui ready-api


【解决方案1】:

这里是实现相同的脚本断言。这样,您也不需要有多个 xpath 断言。

脚本断言:跟随内联 cmets

//Check if the response is ok
assert context.response, 'Response is empty or null'

//Define your expected fault codes
def expectedCodes = [123, 456]


def actualCodes = []
if (context.response.contains('faultCode')) {
  //Get the actual fault codes from xml response by parse and find 
  actualCodes = new XmlSlurper().parseText(context.response).'**'.findAll {it.name() == 'faultCode' }*.text() as Integer[]
  log.info "Actual fault codes are : ${actualCodes}"

  //Check both expected and actual are matching
  assert expectedCodes.sort() == actualCodes.sort()
} else {
  throw new Error('Response does not contain faultCode elements')
}

对于给定的xml数据,您可以在线快速试用demo

【讨论】:

  • 太棒了,您只需对数组进行排序!非常感谢,去试试看..
猜你喜欢
  • 1970-01-01
  • 2014-08-07
  • 2021-06-26
  • 2021-05-01
  • 1970-01-01
  • 2016-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多