Forum Discussion

harshabongle's avatar
harshabongle
Occasional Contributor
4 years ago
Solved

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

  • 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
    }
  • 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
    4 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
    4 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
    4 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'

     

     

     

     

8 Replies

  • 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
    nmrao
    Champion Level 3

    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'

     

    • harshabongle's avatar
      harshabongle
      Occasional Contributor

      I have a problem...contains() is looking for an exact match i.e. case sensitive.
      If the name contains "Batch", it is failing...
      How to make the entire name array to lowercase ? or Is there any other solution?? nmrao HimanshuTayal 

      • nmrao's avatar
        nmrao
        Champion Level 3

        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'