Forum Discussion

4nes-team's avatar
4nes-team
Occasional Contributor
4 years ago

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

  • richie's avatar
    richie
    Community Hero
    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
  • aaronpliu's avatar
    aaronpliu
    Frequent Contributor

    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

     

    • 4nes-team's avatar
      4nes-team
      Occasional Contributor

      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

      • 4nes-team's avatar
        4nes-team
        Occasional Contributor

        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