【问题标题】:SOAP UI Assertion Help : To validate Total attribute and Total ValueSOAP UI 断言帮助:验证 Total 属性和 Total Value
【发布时间】:2016-03-02 16:01:52
【问题描述】:

我在 Google 和 Stackoverflow 中进行了搜索,但没有找到任何有用的信息,因此决定发布问题。

我收到来自 API 的 JSON 响应。

{
"CouponCode": [{
    "id": 56,
    "name": "BlackFriday"
}, {
    "id": 58,
    "name": "ThanksGiving"
}, {
    "id": 62,
    "name": "New Year"
}]}

我需要添加断言,以计算总共有 3 个 id 和 3 个 name。

所有 ID 和名称都不为空。我们不想发送空属性值。

我正在使用 SOAP UI 开源。请提供准确的代码或准确的参考。

确切的断言需要

  • 查找总 Id 和 Name 的大小
  • 查找总 ID 和名称大小。

如果 Id 为 3 且 3 Ids 值为 3..如果 JSON 在这种情况下出现,则断言将失败。

这个

{
"CouponCode": [{
    "id": 56,
    "name": "BlackFriday"
}, {
    "id": 58,
    "name": "ThanksGiving"
}, {
    "id": "",
    "name": "New Year"
}]}

【问题讨论】:

  • 使用已回答的问题。我比较了两个动态计数值,它的工作原理

标签: json groovy automated-tests soapui assertions


【解决方案1】:

下面的groovy script 使用json 的方式来检查预期的结果。

在测试用例的rest request 步骤之后添加groovy script 步骤。

用于提取相同内容的 Sudo 代码。

  1. 阅读json 文本。如果您不想使用固定响应,请从上一步响应中读取。创建对象。
  2. 确保您有预期的 id 和 name 计数。您也可以在测试用例自定义属性中定义它们,以防不想使用固定值并在脚本中每次都更改。
  3. 查找所有id 计数并检查预期值并在失败时显示错误消息。
  4. 与第 3 步类似,对名称执行断言。
//for testing using fixed response, you may aslo assign dynamic response.
def jsonText = '''
{
"CouponCode": [{
    "id": 56,
    "name": "BlackFriday"
}, {
    "id": 58,
    "name": "ThanksGiving"
}, {
    "id": 62,
    "name": "New Year"
}]}'''

def coupons =  new groovy.json.JsonSlurper().parseText(jsonText).CouponCode
//You may also read these values from test case properties
def expectedIdCount = 3
def expectedNameCount = 3
//assert the expected id count with find all coupon ids count of json response
assert expectedIdCount == coupons.findAll{it.id}.size(), "Coupon id count does not match"
//assert the expected name count with find all coupon names count of json response
assert expectedNameCount == coupons.findAll{it.name}.size(), "Coupon name count does not match"

同样可以使用script assertion 来实现其余步骤,这将避免额外的常规脚本步骤。但它可能需要对脚本进行少量更改,如下所示。

如何动态读取json 响应?

来自脚本断言
使用下面的行并从上面删除固定的 jsonText。
def jsonText = messageExchange.response.responseContent

来自 Groovy 脚本步骤 //替换下面的其余请求步骤名称 def jsonText = context.expand('${ReplaceStepName#Response}')

如何读取测试用例级别的属性以获得预期结果而不是脚本中的硬编码值?

id 定义一个测试用例级别的属性,比如EXPECTED_ID_COUNT,并像你提到的那样提供值3,同样,也为name 定义。

//read in script these properties
def expectedIdCount = context.testCase.getPropertyValue('EXPECTED_ID_COUNT') as Integer

【讨论】:

  • Rao - 这是什么?这里断言 expectedIdCount == coupons.findAll{it.id}.size()
  • 已添加评论,请查看。还有开头提到的sudo代码。
  • 我更新了我的问题。我想我不清楚。我们很亲密
  • 好的,你试过提供的脚本了吗?发生了什么?
  • 你的情况也得到处理,显示错误Coupon id count does not match. Expression: (expectedIdCount == coupons.findAll({ -> ... }).size()). Values: expectedIdCount = 3
【解决方案2】:

有几种可能的solutions。最简单的是使用 XPath 断言;请记住,在内部,SoapUI 会尽可能将所有内容都转换为 XML。

count(//*:id)

预期结果:

3

同样适用于您的name

【讨论】:

  • 我如何写两个 ..like count(//*:id) = id value 的断言?
  • @BostonStar 我不明白你的问题。 XPath 断言有两个窗口:XPath 语句和期望值。
  • 脚本下还有一个叫做“脚本断言”的窗口
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-29
  • 1970-01-01
  • 2012-09-26
相关资源
最近更新 更多