【问题标题】:Using Groovy in soapUI to extract a JSON field在soapUI 中使用Groovy 提取JSON 字段
【发布时间】:2019-02-25 15:23:41
【问题描述】:

我正在使用带有 groovy 的 soapui 进行自动化 api 测试,我发布了一个请求 REST,这是我的回复:

 {`enter code here`
     "firstname": "aaa",
     "lastname": "bbb",
     "address": "test",
     "city": "city",
     "country": "country",
     "default": "false",
     "id": "326"
  }, {
     "firstname": "ddd",
     "lastname": "eee",
     "address": "test",
     "city": "city",
     "country": "country",
     "default": "True",
     "id": "67"
  }, {
     "firstname": "hhh",
     "lastname": "yyy",
     "address": "test",
     "city": "city",
     "country": "country",
     "default": "false",
     "id": "345"
  }, {
     "firstname": "ooo",
     "lastname": "hh",
     "address": "test",
     "city": "city",
     "country": "country",
     "default": "false",
     "id": "3211"
  },

我想恢复默认为“true”的用户的id

【问题讨论】:

  • 搜索 JsonSlurper 示例

标签: api groovy automation soapui


【解决方案1】:

首先,您发布的响应不是有效的 json。 应该是这样的:

[
  {
    "firstname": "aaa",
    "lastname": "bbb",
    "address": "test",
    "city": "city",
    "country": "country",
    "default": "false",
    "id": "326"
  },
  {
    "firstname": "ddd",
    "lastname": "eee",
    "address": "test",
    "city": "city",
    "country": "country",
    "default": "True",
    "id": "67"
  },
  {
    "firstname": "hhh",
    "lastname": "yyy",
    "address": "test",
    "city": "city",
    "country": "country",
    "default": "false",
    "id": "345"
  },
  {
    "firstname": "ooo",
    "lastname": "hh",
    "address": "test",
    "city": "city",
    "country": "country",
    "default": "false",
    "id": "3211"
  }
]

如果你只是对 groovy 脚本感兴趣,这里是:

package json

import com.eviware.soapui.support.XmlHolder
import groovy.json.*


//define the location of the JSON response
def response = context.expand('${post_json_data#Response}').toString()

//parse response as json
def json_response = new JsonSlurper().parseText(response)

//iterate on the list with if condition
json_response.eachWithIndex {
    item, index ->
        if(item.default == 'True') {
            log.info "index: ${index}"
            log.info "Item default value: ${item.default}"
            log.info "Item id value : ${item.id}"
        }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多