Forum Discussion

LK's avatar
LK
New Contributor
2 years ago

How to execute same test step again until the condition is satisfied

I have a situation where I need to check a field in the API response. It changes after every 5 minutes, so need to trigger the API, check the response , if it has not reached the expected value, again check it after 5 minutes. Is there a way to do it? I tried it through data source loop, with property wait, but it stops the execution on the first failure and does not trigger the api again. Any help would be highly appreciated.

3 Replies

  • KarelHusa's avatar
    KarelHusa
    Champion Level 2

    LK,

    you can either use the Conditional Goto Test step or Groovy scripting.

     

    There's a comprehensive article on controlling the test case flow using both these approaches in SoapUI/ReadyAPI docs.

     

    Best regards,

    Karel

     

    • LK's avatar
      LK
      New Contributor

      Hi Karel, thanks for your reply. I tried using groovy script and I am getting this error- groovy.lang.MissingPropertyException: No such property: java for class: Script4
      error at line: 7

       

      Scrpit:

      import groovy.json.JsonSlurper

      //def loopProperties = testRunner.runTestStepByName("REST Request")

      //def count =Integer.parseInt (testRunner.testCase.testSuite.getPropertyValue( "availableEvStationCount" ))

      def availableEvStationCount= testRunner.testCase.getPropertyValue( java.lang.string("availableEvStationCount") )
      testRunner.testCase.setPropertyValue("availableEvStationCount", "availableEvStationCount" )
      //Integer count = scount as Integer
      if (availableEvStationCount<2)
      {
      Thread.sleep(5000)
      } else {

      testRunner.runTestStepByName("REST Request")
      }

       

      Could you please help.

      • KarelHusa's avatar
        KarelHusa
        Champion Level 2

        LK,

        you can:

        1. call your API
        2. save the result into a property
        3. execute the Groovy script

        See the screenshot below.

         

         

        In the Groovy script, I would suggest utilizing testRunner.gotoStepByName( "RESTRequest"):

         

        // The result is saved in a property value as string, transform into an integer
        def result = Integer.parseInt(testRunner.testCase.getPropertyValue("result"))

        if (result < 50) {
        sleep(1000)
        testRunner.gotoStepByName("RESTRequest")
        }

         

        If the result is 50 or higher, the loop is finished, and your test case continues.

         

        Best regards,

        Karel