1
votes

I'm new to soapui and I'm trying to do a property transfer of the element in the list options which ha selected = true. The response is in JSON and I can select the element with the code $.response.optionList[0].options[1].id but rather then give the element number I would like to choose whichever has selected = true. Is this something you do with a groovy script? Does anyone have any suggestion?

{    
    "response": {

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

1 Answers

1
votes

Are you using property transfer test step after the rest request step?

You can use Script Assertion for the rest request step to achieve and remove property transfer test step.

Use below script:

//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())

If you need to access the above id from previous step response and use it later steps, use property expansion say ${#TestCase#ID}

You can quickly try the above script online Demo