cancel
Showing results for 
Search instead for 
Did you mean: 

Repeat a test step until desired property value is in the response

New Contributor

Repeat a test step until desired property value is in the response

Hi,

 

I'm tryin to build a new test step for my API testing, but I don't get the desired result.

Below you can see the JSON data from my REST response. So the goal here is to make sure that e.g. at 15pm when the test runs, the JSON response would be checked for the right "reasonCode". In my case it is "EM_004". The response is not always there right on time at 15pm, so that's why I think I need some kind of a loop so I can check for a period of 5 minutes or something like that if the right "reasonCode" is already there. So as soon as the right "reasonCode" is received the loop should stop and the test case should go on.

 

 

 

 

{
"id" : "data",
"title" : "data",
"reasonCode" : "EM_005",
"publishedAt" : "data",
"reference" : {
"productType" : "data",
"deliveryDate" : "data",
              }
},
{
"id" : "data",
"title" : "data",
"reasonCode" : "EM_004",
"publishedAt" : "data",
"reference" : {
"productType" : "data",
"deliveryDate" : "data",
              }
},
{
"id" : "data",
"title" : "data",
"reasonCode" : "EM_002",
"publishedAt" : "data",
"reference" : {
"productType" : "data",
"deliveryDate" : "data",
              }
}

 

 

 

 

Unfortunately I'm a total noob at groovy-scripting and stuff like that so it would be very cool if you could help me out here. If there are any further question just write me a pm or direct response, please.

 

Thanks a lot in advance,

4nes team

6 REPLIES 6
Community Hero

Re: Repeat a test step until desired property value is in the response

Hey @4nes-team,

I think i can help, but im typing this out on my phone right now.
I'll respond when im back in my house tomorrow when i have my laptop....unless one of the coders (@HimanshuTayal, @nmrao, @ChrisA) answers this first as i've seen them answer this if/then/else issue before now (and my groovy is rubbish!)

Ta

Rich
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Frequent Contributor

Re: Repeat a test step until desired property value is in the response

Hi @4nes-team ,

 

one loop can resolve your question. But I did not see a complete JSON response, assume that you get a response with a list. Run your test case

 

/**
* TEST CASE
*   REST STEP
*   GROOVY STEP
*/

import groovy.json.JsonSlurper

def teststep = testRunner.testCase.testSteps['REST STEP']
// get REST response
def response = teststep.testRequest.response.contentAsString  // context.expand( '${REST STEP#Response}' )
def json = new JsonSlurper().parseText(response)

// assume you get a list named "MyNode"
if (json.MyNode.any {it.reasonCode == 'EM_004}) {
	testRunner.gotoStepByName("Next Step")
} else {
	Thread.sleep(1000)  // set your delay time
	testRunner.gotoStepByName("REST STEP")
}


Thanks,
/Aaron

 

Tags (1)
New Contributor

Re: Repeat a test step until desired property value is in the response

Hi Aaron,

 

thank you for your quick reply.

 

I tried to implement your code into my testcase but apparently I'm doing something wrong. Could you take a look? You can see the error in the screenshot I attached.

 

Thanks again.

4nes team

New Contributor

Re: Repeat a test step until desired property value is in the response

Hi,

 

is there any possibilty to get an answer to my question? I really hope that there is a solution to my issue.

 

Thanks.

 

Best regards, 

4nes team

Frequent Contributor

Re: Repeat a test step until desired property value is in the response

Hi @4nes-team ,

 

No more workaround to support you since I did not get anything from your attachment. I attached groovy script that is groovy GDK built-in package, no need other dependency. So you should not run into those weird issue.

Can you please paste more details if possible? Maybe community would help you figure it out.

 

I did not login community all the time as I have a new project to work on now. Whatever, will go through all of new update and try to answer questions if I can.

 

Thanks

/Aaron

Community Manager

Re: Repeat a test step until desired property value is in the response

Thank you community!

 

@4nes-team could you let us know if you solved this one? 


Sonya Mihaljova
Community and Education Specialist

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors