cancel
Showing results for 
Search instead for 
Did you mean: 

How to do assertions using using groovy script

Highlighted
Contributor

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

6 REPLIES 6
Highlighted
Community Leader

Hi,

 

to achieve the simplest assertion You can try to get value from the list and assert it:

 

def list = [5, 6, 7, 8]
assert list.get(2) == 6 : "test failed"

 

Read this topic's:

https://stackoverflow.com/questions/12645142/groovy-assert-how-to-display-the-value

http://grails.asia/groovy-assert-examples

 

Hope this will help You.

Highlighted
Community Leader

@roja1 :

 

If possible could you please provide sample response.

 


Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal

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

Highlighted
Community Leader

You get something in format:

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

I understand that I'm just in the content of your response from the server.

 

Then you can write a script (but in groovy) that will allow you to read the above data yes?

 

The answer is usually "plainText", does it look like that to you?

Highlighted

@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.


Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal
Highlighted

Hi @HimanshuTayal ,

 

Sorry for the delay in the response.

 

I'm not saving excel file into properties test step. I have an excel file which has a test step name in one column, fields(comma seperated) which we are expecting in the response body in one column. Now in my groovy script I want to read the fields against a particular test step and check if those fields are present my response are not.

 

 

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors