Forum Discussion

ManojKumar's avatar
ManojKumar
Occasional Contributor
8 years ago

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 h...
  • groovyguy's avatar
    8 years ago

    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*/
    }