Shubham Jain
Shubham Jain

Reputation: 17553

How to write a recursive function in groovy which wait for a particular value in response | SOAP UI

I have a GET request which is looking for value complete in response but this value can be processing or request_is_in_process if the process in server is not complete.

Now I need a groovy script or trick in soap-ui which can keep processing this request until it do not find value as complete in response.

For more clarification I have created a separate project for this request and it is going to save my value in global properties.

import groovy.json.JsonSlurper

responseContent = testRunner.testCase.getTestStepByName("Checking_Request").getPropertyValue("response")
slurperresponse = new JsonSlurper().parseText(responseContent)
log.info (slurperresponse.products.status)



def valueFromPreviousResponse = slurperresponse.products.status
context.testCase.testSuite.setPropertyValue('Status_of_Product_import_request', valueFromPreviousResponse)

com.eviware.soapui.SoapUI.globalProperties.setPropertyValue( "Status_of_Product_import_request", valueFromPreviousResponse )

Above code is setting up my value awesomely fine. Just I need to keep running that teststep until it do not find the value as complete

Note:- I am testing on REST API. Everything is in JSON format. Another thing is I am using free version of SOAP-UI, not an pro version

I am new in SOAP-UI and for groovy as well. so any suggestion will be helpful/welcome.

Thanks in advance!! :)

Upvotes: 0

Views: 2199

Answers (2)

Shubham Jain
Shubham Jain

Reputation: 17553

Below code works for me:

def i=0
// Recursive Function to check response again and again
while( slurperresponse.products.status!= "complete" ) {
    project = testRunner.getTestCase().getTestSuite().getProject().getWorkspace().getProjectByName("REST Project 1")
    testSuite = project.getTestSuiteByName("Product(PIM-API)");
    testCase = testSuite.getTestCaseByName("Checking_Responses");
    runner = testCase.run(new com.eviware.soapui.support.types.StringToObjectMap(), false);
    sleep(new Random().nextInt(10) * 2000)
    log.info (slurperresponse.products.status)

// My other testcase setting the latest value of my required response in global properties and in below code I am checking the latest value

    slurperresponse.products.status = com.eviware.soapui.SoapUI.globalProperties.getPropertyValue( "Status_of_Product_import_request" )
    i++
    log.info (i)
    if(i==5)
    {
        assert false
        }
}
assert true

Thanks to user1207289 :)

Upvotes: 0

user1207289
user1207289

Reputation: 3253

Try this

  While (slurperresponse.products.status!= "complete") {
          testRuner.testcase.testStepByName("yourStepName").run(testRunner,testRunner.getRunContext())
    }

Upvotes: 1

Related Questions