Forum Discussion

TejasParmar's avatar
TejasParmar
Occasional Contributor
6 years ago
Solved

Re: How to run a loop dynamically using values received in a response in SOAP UI Pro

Hi Lucian,

 

Thanks for the prompt response :) . I tried with script you gave, currently it is not going into while loop. Not sure what the issue is. If you can please help me on that. the result and modified script is as below

 

def referenceIds = []

// Get raw response
rawResponse = testRunner.testCase.testSteps["findReferenceforTypes-Delete Operation"].testRequest.response.contentAsString
log.info rawResponse     //first log response
// Find all reference ids in the GetReferenceIds test step
while ( rawResponse.indexOf( "referenceID\" : ") != -1 ){
rawResponse = rawResponse.substring( rawResponse.indexOf( "referenceID\" : \"") + "referenceID\" : \"".length() )
referenceIds.add( rawResponse.substring( 0, rawResponse.indexOf( "\"" ) ) )
rawResponse = rawResponse.substring( "referenceID\" : \"".length() )
log.info referenceIds  //check for referenceIds
}

// Get the current iteration
def currentIteration = testRunner.testCase.testSteps["DataSource3"].currentRow
// Iterate for each element in the referenceIds list
if (currentIteration < referenceIds.size) {
result["ReferenceId"] = referenceIds[currentIteration]
}

 

 

Result

Thu Apr 12 15:00:24 IST 2018:INFO:[{"referenceID":"e4e1c0ab62b850860162b9265c12005b","nativeSourceIDValue":"NativeSourceID762","masterID":"f126a218-38ef-4e76-bc68-470cc7d0c117","referenceTypeID":"e4e1c0ab62b850860162b9265bbe005a","targetReferenceTypeID":null},{"referenceID":"e4e1c0ab62b850860162b9265c68005c","nativeSourceIDValue":"NativeSourceID861","masterID":"7e8b57bb-4d8e-4039-9a7f-22bbb84990e0","referenceTypeID":"e4e1c0ab62b850860162b9265bbe005a","targetReferenceTypeID":null}]
Thu Apr 12 15:00:24 IST 2018:INFO:[:]

  • The response comes as "referenceId":"value" (no blanks between the key and value). You just have to modify the while loop as in:

     

    // Find all reference ids in the GetReferenceIds test step
    while ( rawResponse.indexOf( "referenceID\":") != -1 ){
    	rawResponse = rawResponse.substring( rawResponse.indexOf( "referenceID\":\"") + "referenceID\":\"".length() )
    	referenceIds.add( rawResponse.substring( 0, rawResponse.indexOf( "\"" ) ) )
    	rawResponse = rawResponse.substring( "referenceID\":\"".length() )
    	log.info referenceIds  //check for referenceIds
    }

    Let me know if it works!

6 Replies

  • TejasParmar's avatar
    TejasParmar
    Occasional Contributor

    I have a test case where i have to extract particular ids from a response. The response has for eg. 4 ids ID1, ID2, ID3 & ID4

    Now i want to use groovy script and extract all 4 ids save it somewhere(not in excel or any external file). My script should also run a loop for 4(since it has 4 ids in response) iteration for a GET service and in each iteration i want to use a different ID (ID1, ID2, ID3 & ID4). Please help me on this issue

    • Lucian's avatar
      Lucian
      Community Hero

      Can you post a response example?

       

      Also, please post a request example in which we can see how do you want to use your IDs later on.

      • TejasParmar's avatar
        TejasParmar
        Occasional Contributor

        Hi Lucian,

         

        [
        {
        "referenceID" : "e4e2802c62a8fc820162b3401d7f0087",
        "nativeSourceIDValue" : "NativeSourceID165",
        "masterID" : "6b441ad3-b726-40a0-a034-dea6e57b07a8",
        "referenceTypeID" : "e4e2802c62a8fc820162b33ff7ed0086",
        "targetReferenceTypeID" : null
        },
        {
        "referenceID" : "e4e2802c62a8fc820162b3404f1f0088",
        "nativeSourceIDValue" : "NativeSourceID690",
        "masterID" : "ad2361b4-4b43-4b18-ab6e-0b67acc32897",
        "referenceTypeID" : "e4e2802c62a8fc820162b33ff7ed0086",
        "targetReferenceTypeID" : null
        }
        ]

         

        Above is the response i am getting. Now i want a create a datasource step after this response with groovy script. I want that datasource to give me referenceID[0], referenceID[1], etc for each iteration so i can use it in next request. And also the groovy script should run iteration dynamically for the number of referenceID's received. Currently the script i have has issues and returns only one value with an error.Below is the script and error

         

        Error -

        org.codehaus.groovy.runtime.GStringImpl cannot be cast to java.lang.String

         

        Script :- 

        jsonResponseMessage = testRunner.testCase.testSteps["abc"].testRequest.response.contentAsString

        def i = 0, ReferenceID, j = 0

        def sActualArray= new ArrayList<String>()

        def jsonSlurper = new JsonSlurper().parseText jsonResponseMessage
        def actualsize = jsonSlurper.size()

        actualsize.times{

        if(jsonSlurper.referenceID[i]!=null)ReferenceID=jsonSlurper.referenceID[i] else ReferenceID=''

        sActualArray[i]="$ReferenceID"
        i++
        }

        actualsize.times{
        result["ReferenceID"] = sActualArray[0];
        j ++
        }

    • Lucian's avatar
      Lucian
      Community Hero

      Ok, this is pretty simple since there is a special DataSource step in SoapUI Pro which allows you to loop based on a groovy generated set of data. All you would need is this:

       

       

       

      The groovy script to extract the reference id can be done nicer but for the purpose of this demonstration it does its job. I also attached the SoapUI project for you.

       

      Lucian.

  • Lucian's avatar
    Lucian
    Community Hero

    The response comes as "referenceId":"value" (no blanks between the key and value). You just have to modify the while loop as in:

     

    // Find all reference ids in the GetReferenceIds test step
    while ( rawResponse.indexOf( "referenceID\":") != -1 ){
    	rawResponse = rawResponse.substring( rawResponse.indexOf( "referenceID\":\"") + "referenceID\":\"".length() )
    	referenceIds.add( rawResponse.substring( 0, rawResponse.indexOf( "\"" ) ) )
    	rawResponse = rawResponse.substring( "referenceID\":\"".length() )
    	log.info referenceIds  //check for referenceIds
    }

    Let me know if it works!