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