Forum Discussion

roja1's avatar
roja1
Contributor
5 years ago

How to do assertions using using groovy script

Hi,

 

I'm using soapUI for test execution. I want to do contains assertion using groovy script. I want to check if a certain list of fields are present in my response or not. For that I need to use contains assertion and as per the requirement I need to do it through groovy script. Please suggest me on how to achieve it.

Thank you in advance

    • roja1's avatar
      roja1
      Contributor

      Hi HimanshuTayal ,

       

      Thank you for your reply. What I'm trying to achieve is I will reading the fields from an excel file and check if they are present in the response or not.

       

      For eg:

      I will be reading cityCode and cityName from a column in excel file.

      My response is as below:

      [{
      "cityCode": "2345",
      "cityName": "abc"
      }]

       

      I need to check if both cityCode and cityName are present in the response or not

      So the comparison should be dynamic and automatic but not manual

      • HimanshuTayal's avatar
        HimanshuTayal
        Community Hero

        roja1 :

         

        I am assuming that you have saved your Excel file values into Properties step from where you can fetch and validate response values, you can use below groovy code to validate the values with the excel sheet values.

         

        def cityCode = ${Proeprties#cityCode}
        def cityName = ${Proeprties#cityName}
        def jsonSlurper = new JsonSlurper().parseText(response)
        assert cityCode == jsonSlurper.cityCode : "City Code value is different"
        assert cityName == jsonSlurper.cityName : "City Name value is different"

         

        Let me know in case you need more help.