How to validate whether a sub-string is occuring in every object in an array in the response?
Sample response
{
"result": {
"list": [
{
"proceduresId": 280,
"name": "new batch workflow 123new batch workflow 123",
"templateId": null,
"isLocked": 0
},
{
"proceduresId": 281,
"name": "latest_trendsnew batch workflow 123",
"templateId": null,
"isLocked": 0
},
{
"proceduresId": 282,
"name": "dark knightnew batch workflow 123",
"templateId": null,
"isLocked": 0
}
Hi,
I am trying to write an assertion to check whether the string "batch" occurs in the name field ?
Please help.
Thanks
harsha
You can use below groovy code to verify whether all node contains batch or not.
def json = new JsonSlurper().parseText(res) def list_size = json.result.list.size() for(int i = 0 ; i<list_size ; i++){ def name = json.result.list[i].name assert name.contains("batch") : "Didn't contains batch in name tag "+name }
Below is the Script Assertion (one liner)
assert new groovy.json.JsonSlurper().parseText(context.response).result.list.name.every {it.contains('batch')}, 'All names contain BATCH check failed'
Just convert the name string to upper case and check BATCH
assert new groovy.json.JsonSlurper().parseText(context.response).result.list.name.every {it.toUpperCase().contains('BATCH')}, 'All names contain BATCH check failed'
harshabongle : You can use below code, it will convert given String to lower case and then validate whether particular word exist or not:
assert name.toLowerCase().contains("batch") : "Didn't contains batch in name tag "+name
If you don't want to convert to upper or lower case to compare, then use regular expression as shown below:
assert new groovy.json.JsonSlurper().parseText(context.response).result.list.name.every {it =~ /(?i)batch/}, 'All names contain BATCH check failed'