Soapui 属性 转 JSON if 语句

Soapui property transfer JSON if statement

我是 soapui 的新手,我正在尝试 属性 传输 ha selected = true 列表选项中的元素。响应在 JSON 中,我可以 select 代码为 $.response.optionList[0].options[1].id 的元素,而是给出我想选择 selected = true 的元素编号。这是你用 groovy 脚本做的事情吗?有人有什么建议吗?

{    
    "response": {

        "optionList": [
        {
            "options": [
            {
                "id": 10,
                "selected": false
            },
            {
                "id": 11,
                "selected": true
            }
            ]  
        },
        {
            "options": [            
            {
                "id": 12,
                "selected": false
            }]
        }
        ]
    }
}       

您是否在休息请求步骤之后使用 属性 传输测试步骤?

您可以使用 Script Assertion 作为其余请求步骤来实现并删除 属性 传输测试步骤。

使用以下脚本:

//check if there is response
assert context.response, 'response is empty or null'
def json = new groovy.json.JsonSlurper().parseText(context.response)
def id
json.response.optionList.each{ optionItem ->
   optionItem.options.each { option ->
     if (option.selected == true) {
        id = option.id
     }
   }
}
log.info "id value when selected is true : ${id}"
context.testCase.setPropertyValue('ID', id?.toString())

如果您需要从上一步响应访问上面的 id 并在后面的步骤中使用它,请使用 属性 扩展说 ${#TestCase#ID}

您可以在线快速试用以上脚本Demo