Forum Discussion

ManojKumar's avatar
ManojKumar
Occasional Contributor
7 years ago
Solved

Changing properties dynamically using groovy script

Hi All,

 

Thanks in advance for the help. I am stuck in below scenario for a while. Since I am new to coding/programming, it is relatively tough for me to understand what's wrong in the code. Any help would be greatly appreciated. Thanks again 

 

Scenario:

1) I have a REST service which gives me back a response with multiple nodes.

 eg: [
      {
      "Namespace": "dbo",
      "ObjectName": "WorkOrder",
      "IsCustomQuery": false,
      "CustomQuery": null,
      "Name": "[dbo].[WorkOrder]",
      "Description": "",
      "Fields": []
   },
      {
      "Namespace": "dbo",
      "ObjectName": "sysdiagrams",
      "IsCustomQuery": false,
      "CustomQuery": null,
      "Name": "[dbo].[sysdiagrams]",
      "Description": "",
      "Fields": []
   },
      {
      "Namespace": "Maintenance",
      "ObjectName": "Configuration",
      "IsCustomQuery": false,
      "CustomQuery": null,
      "Name": "[Maintenance].[Configuration]",
      "Description": "",
      "Fields": []
   }, and so on...

 

2. Now, I have to use each of this node as request and run another REST service(post). For example, If I have 3 nodes in step 1, I have to run 2nd REST service 3 times. One for each node.

 

My understanding was that, this could be achieved using set property(), get property() and FOR loop in groovy script.

 

Pseudocode:

1)      Store each of the node in the response as an item in the array.

2)      Find the number of nodes using sizeof()

3)      Using the number of nodes as the max limit, use FOR loop with the POST request inside the loop using each item in an array as property.

 

Code used:

 

import groovy.json.JsonSlurper

def response = context.expand( '${GET schema#Response}' ) /*GET schema is the GET request which returns the response with 12 nodes*/

def r = new JsonSlurper().parseText(response)

def n = r.size()

//log.info(r[1])

def i=0

for (i=0;i<n;i++)

{

testRunner.testCase.setValue("lists", r[i] as String)

def getLocalPropValue = testRunner.testCase.getPropertyValue("lists")

log.info(getLocalPropValue) /* to verify that each item is retrieved*/

testRunner.runTestStepByName("REST Request") /*REST request is the POST service which should be run once for each of the nodes*/

}

 

Most common error message: - The error is in the 9th line.

 

‘groovy.lang.MissingMethodException: No signature of method: com.eviware.soapui.impl.wsdl.WsdlTestCasePro.setValue() is applicable for argument types: (java.lang.String, java.lang.String) values: [lists, [CustomQuery:null, Description:, Fields:[], IsCustomQuery:false, Name:[dbo].[WorkOrder], Namespace:dbo, ObjectName:WorkOrder]] Possible solutions: setName(java.lang.String), setName(java.lang.String), getName(), getClass() error at line: 10 ’

  • Instead of setValue try using setPropertyValue, as seen below:

     

     

     

    	testRunner.testCase.setPropertyValue("lists" + i, r[i].toString())

     

    The above, I added i to the property name since your script was overwriting the list property with every iteration of the loop. So you want to take this groovy script and use it in a REST request, right?

     

     

     

    Then you need to set the request content too. Try this script and see if it helps:

     

     

    import groovy.json.JsonSlurper
    
    def response = context.expand( '${GET schema#Response}' ) /*GET schema is the GET request which returns the response with 12 nodes*/
    def r = new JsonSlurper().parseText(response)
    def n = r.size()
    //log.info(r[1])
    def i=0
    for (i=0;i<n;i++)
    {
    	testRunner.testCase.setPropertyValue("lists" + i, r[i].toString())
    	def getLocalPropValue = testRunner.testCase.getPropertyValue("lists" + i)
    	log.info(getLocalPropValue) /* to verify that each item is retrieved*/
    	testRunner.testCase.testSteps["REST Request"].testRequest.requestContent = r[i];
    	testRunner.runTestStepByName("REST Request") /*REST request is the POST service which should be run once for each of the nodes*/
    }

     

3 Replies

  • groovyguy's avatar
    groovyguy
    Champion Level 1

    Instead of setValue try using setPropertyValue, as seen below:

     

     

     

    	testRunner.testCase.setPropertyValue("lists" + i, r[i].toString())

     

    The above, I added i to the property name since your script was overwriting the list property with every iteration of the loop. So you want to take this groovy script and use it in a REST request, right?

     

     

     

    Then you need to set the request content too. Try this script and see if it helps:

     

     

    import groovy.json.JsonSlurper
    
    def response = context.expand( '${GET schema#Response}' ) /*GET schema is the GET request which returns the response with 12 nodes*/
    def r = new JsonSlurper().parseText(response)
    def n = r.size()
    //log.info(r[1])
    def i=0
    for (i=0;i<n;i++)
    {
    	testRunner.testCase.setPropertyValue("lists" + i, r[i].toString())
    	def getLocalPropValue = testRunner.testCase.getPropertyValue("lists" + i)
    	log.info(getLocalPropValue) /* to verify that each item is retrieved*/
    	testRunner.testCase.testSteps["REST Request"].testRequest.requestContent = r[i];
    	testRunner.runTestStepByName("REST Request") /*REST request is the POST service which should be run once for each of the nodes*/
    }

     

    • ManojKumar's avatar
      ManojKumar
      Occasional Contributor

      Hi @msiadak

       

      Thank you so much for your reply. I am able to see that each item in the array is given as request for the REST service. So the logic works fine. But, I face issue while the REST service is trying to post. It says that all the items are not enclosed in double quotes. Hence the service is not able to perform its intended function.

       

      The content which is given to the next REST service is not in proper json format. Please let me know your suggestion.

       

      Congratulations!!!I just saw that you have been awarded some gifts.


      groovyguy wrote:

      Instead of setValue try using setPropertyValue, as seen below:

       

       

       

      	testRunner.testCase.setPropertyValue("lists" + i, r[i].toString())

       

      The above, I added i to the property name since your script was overwriting the list property with every iteration of the loop. So you want to take this groovy script and use it in a REST request, right?

       

       

       

      Then you need to set the request content too. Try this script and see if it helps:

       

       

      import groovy.json.JsonSlurper
      
      def response = context.expand( '${GET schema#Response}' ) /*GET schema is the GET request which returns the response with 12 nodes*/
      def r = new JsonSlurper().parseText(response)
      def n = r.size()
      //log.info(r[1])
      def i=0
      for (i=0;i<n;i++)
      {
      	testRunner.testCase.setPropertyValue("lists" + i, r[i].toString())
      	def getLocalPropValue = testRunner.testCase.getPropertyValue("lists" + i)
      	log.info(getLocalPropValue) /* to verify that each item is retrieved*/
      	testRunner.testCase.testSteps["REST Request"].testRequest.requestContent = r[i];
      	testRunner.runTestStepByName("REST Request") /*REST request is the POST service which should be run once for each of the nodes*/
      }

       



      groovyguy wrote:

      Instead of setValue try using setPropertyValue, as seen below:

       

       

       

      	testRunner.testCase.setPropertyValue("lists" + i, r[i].toString())

       

      The above, I added i to the property name since your script was overwriting the list property with every iteration of the loop. So you want to take this groovy script and use it in a REST request, right?

       

       

       

      Then you need to set the request content too. Try this script and see if it helps:

       

       

      import groovy.json.JsonSlurper
      
      def response = context.expand( '${GET schema#Response}' ) /*GET schema is the GET request which returns the response with 12 nodes*/
      def r = new JsonSlurper().parseText(response)
      def n = r.size()
      //log.info(r[1])
      def i=0
      for (i=0;i<n;i++)
      {
      	testRunner.testCase.setPropertyValue("lists" + i, r[i].toString())
      	def getLocalPropValue = testRunner.testCase.getPropertyValue("lists" + i)
      	log.info(getLocalPropValue) /* to verify that each item is retrieved*/
      	testRunner.testCase.testSteps["REST Request"].testRequest.requestContent = r[i];
      	testRunner.runTestStepByName("REST Request") /*REST request is the POST service which should be run once for each of the nodes*/
      }

       


       

      • ManojKumar's avatar
        ManojKumar
        Occasional Contributor

        I have resolved the issue now. Thanks for your code though. It was very helpful. I had to convert the string back to json before running the second request.

         

        Thanks,

        Manoj