cancel
Showing results for 
Search instead for 
Did you mean: 

Groovy script for Assertion to check value

SOLVED
Highlighted
Contributor

Groovy script for Assertion to check value

Hi

 

I am trying to write a script assertion, where in response after a particular "id" i have to check the status of other property,

 

Below is my sample response

 

{
"gr" : [
{
"args" : [
{
"Id" : "abcd",
"State" : "success"
},
{
"Id" : "d1234",
"State" : "failed"
},
{
"Id" : "a123",
"State" : "success"
},
{
"Id" : "T123",
"State" : "success"

},
{
"Id" : "d1234",
"State" : "success"
},
]
},
]
}

 

in the response the id: "d1234" repeats twice, where as i have to check the State of the "d1234" which is available after the id="T123"  after research i have done below script assertions where as its checking both d1234 and the assertions is failing.

 

import groovy.json.JsonSlurper

def response = messageExchange.response.responseContent
def jsonsl = new JsonSlurper().parseText(response)

def res = context.expand( '${checking#Response#$[\'gr\'][0][\'args\']}' )
def Result = new JsonSlurper().parseText(res)
def Count = Result.size()
def count = 0
for ( i=0;i < Count; i++){
if (jsonsl.gr[0].args[i].'Id' == 'd1234'){
def status = jsonsl.gr[0].args[i].'State'
log.info (status)
assert status == 'success' : "d1234 failed"
count ++
}
}

assert count != 0 : "d1234 is missing"

 

So, Please let me know how to check the state of d1234 which is available after T123 only. The node value of T123 is not constant. 

 

Thanks

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Contributor

Re: Groovy Script Required to check assertion depending on Environments

Hi @HimanshuTayal 

 

Thanks for the Update.

 

I tried modifying the Code like below, its working now.. Before i was facing error. it was failing in some environments

 

import groovy.json.JsonSlurper

def response = messageExchange.response.responseContent
def jsonsl = new JsonSlurper().parseText(response)
def Result = jsonsl.gr[0].args
def count = Result.size()
def flag = false
for (int i=0;i < count; i++){
	if (Result[i].Id == "abcd"){
		for (int j=i ; j < count; j++){
			def id = Result[j].Id
			if(id == "d1234"){				
				def status = Result[j].State
				assert id == "d1234" : "d1234 not found"
				assert status == 'success' : "d1234 failed"
				flag = true;
			}
		}
	}
}
assert flag , "d1234 is missing"

 

Thanks

View solution in original post

14 REPLIES 14
Highlighted
Community Hero

Re: Groovy script for Assertion to check value

It is not guaranteed always in the list that it will occur in the same place. it needs to different strategy.

- It is surprising that how will same id will have different status. Isn't it?
- Is it ok to get all the matching status'es for the given id and see if it has a success?



Regards,
Rao.
Highlighted
Contributor

Re: Groovy script for Assertion to check value

@nmrao 

 

- It is surprising that how will same id will have different status. Isn't it?  Yes some times both the status will be same some times it may vary. 
- Is it ok to get all the matching status'es for the given id and see if it has a success? The requirement is to check the state of the id:d1234 after T123 only.  

 

I am not sure how to put the condition using groovy script to get the state of  the id:d1234 comes after T123. Please help

Thanks

 

Highlighted
Community Leader

Re: Groovy script for Assertion to check value

Hi @doubtsreadyapi ,

 

Please try below groovy hope it will help you out:

 

 

import groovy.json.JsonSlurper

def response = messageExchange.response.responseContent
def jsonsl = new JsonSlurper().parseText(response)
def Result = jsonsl.gr[0].args
def count = Result.size()
def flag = false
for (int i=0;i < count; i++){
	if (Result[i].Id == "T123"){
		for (int j=i ; j < count; j++){
			def id = Result[j].Id
			if(id == "d1234"){				
				def status = Result[j].State
				assert id == "d1234" : "d1234 not found"
				assert status == 'success' : "d1234 failed"
				flag = true;
			}
		}
	}
}
assert flag , "d1234 is missing"

 

 

 

 


Click "Accept as Solution" if my answer has helped, and remember to give "kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal
Highlighted
Community Hero

Re: Groovy script for Assertion to check value

Already stated that list does not guarantee the order.
What if both d1234 are present after T123 with status'es failed and success?
Clear some context is missing from the original test or due to simplification of the problem.


Regards,
Rao.
Highlighted
Contributor

Re: Groovy script for Assertion to check value

@nmrao 

 

That wont happen one d1234 will be before T123 and one d1234 will be after T123 only, 

Highlighted
Contributor

Re: Groovy script for Assertion to check value

@HimanshuTayal 

 

Thanks your code has worked, 

Highlighted
Community Hero

Re: Groovy script for Assertion to check value

Highlighted
Community Leader

Re: Groovy script for Assertion to check value

@doubtsreadyapi 

 

I am glad that it worked 🙂 👍


Click "Accept as Solution" if my answer has helped, and remember to give "kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal
Highlighted
Contributor

Groovy Script Required to check assertion depending on Environments

Hi @

 

I previous post

 https://community.smartbear.com/t5/SoapUI-Pro/Groovy-script-for-Assertion-to-check-value/m-p/202883#...

 

As i mentioned earlier my sample response like below,

 

{
"gr" : [
{
"args" : [
{
"Id" : "abcd",
"State" : "success"
},
{
"Id" : "d1234",
"State" : "failed"
},
{
"Id" : "a123",
"State" : "success"
},
{
"Id" : "T123",
"State" : "success"

},
{
"Id" : "d1234",
"State" : "success"
},
]
},
]
}

 

Now i have to keep the assertion for first "d1234" parameter, here the
problem is in some environments "d1234" only comes once which is after "T1234"
Which we already done assertion, Now how to handle the first "d1234" which is coming
after "abcd" but this "d1234" will not come in some environments as per requirements
So still the assertion must get pass in other environments if the "d1234" does not exist also.
Please help me how we can achieve it.
Thanks

New Here?
Join us and watch the welcome video:
Announcements
TechCorner Leaderboard
Compete with community members in the TechCorner Challenge and get into the Leaderboard!

Rank Participant Points Earned
1 msiadak 12
2 nmrao 8
3 HimanshuTayal 3
Top Kudoed Authors