Forum Discussion

harshabongle's avatar
harshabongle
Occasional Contributor
5 years ago

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...
  • HimanshuTayal's avatar
    5 years ago

    harshabongle :

     

    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
    }
  • nmrao's avatar
    5 years ago

    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'

     

  • nmrao's avatar
    nmrao
    5 years ago

    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'
  • HimanshuTayal's avatar
    HimanshuTayal
    5 years ago

    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

     

     

  • nmrao's avatar
    nmrao
    5 years ago

    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'