How to use variable with response object

Hi,

My testcase has multiple scenarios like listed below. Instead of using numbers with response1 and request1, I would like to use variable i and increment the i as I go along.
Because response is not a string, I can’t say response+i or responsei. I would appreciate some ideas on how to go about using variable i instead of using hard coded numbers.

Thanks in advance.

Current test Scenarios:
response1 = WS.sendRequestAndVerify(findTestObject(‘ABC/GetUserId’, [(‘UserId’) : “TestUser01”]))
request1 = WSResponseManager.getInstance().getCurrentRequest()

response2 = WS.sendRequestAndVerify(findTestObject(‘ABC/GetUserId’, [(‘UserId’) : “TestUser02”]))
request2 = WSResponseManager.getInstance().getCurrentRequest()

response3 = WS.sendRequestAndVerify(findTestObject(‘ABC/GetUserId’, [(‘UserId’) : “TestUser03”]))
request3 = WSResponseManager.getInstance().getCurrentRequest()

Would like to change to:

def = i

responsei = WS.sendRequestAndVerify(findTestObject(‘ABC/GetUserId’, [(‘UserId’) : “TestUser01”]))
requesti = WSResponseManager.getInstance().getCurrentRequest()

i = i+1;
responsei = WS.sendRequestAndVerify(findTestObject(‘ABC/GetUserId’, [(‘UserId’) : “TestUser02”]))
requesti = WSResponseManager.getInstance().getCurrentRequest()

i = i+1;
responsei = WS.sendRequestAndVerify(findTestObject(‘ABC/GetUserId’, [(‘UserId’) : “TestUser03”]))
requesti = WSResponseManager.getInstance().getCurrentRequest()

You could investigate the use of a List, or Array, or some other Collection that have indices that could serve your purpose.

Maybe like:
def response = []
def request = []

response.add( WS.sendRequestAndVerify(findTestObject('ABC/GetUserId', [('UserId') : "TestUser01"])))
request.add( WSResponseManager.getInstance().getCurrentRequest())

response.add( WS.sendRequestAndVerify(findTestObject('ABC/GetUserId', [('UserId')) : "TestUser02"])))
request.add( WSResponseManager.getInstance().getCurrentRequest())

response.add( WS.sendRequestAndVerify(findTestObject('ABC/GetUserId', [('UserId') : "TestUser03"])))
request.add( WSResponseManager.getInstance().getCurrentRequest())

for (int i = 0 ; i < response.size(); i++) {
    println("your no. ${i} response is ${response[i]}")
}

for (int i = 0 ; i < request.size(); i++) {
    println("your no. ${i} request is ${request[i]}")
}
1 Like

I’ll try, but this is going to be a big change for all the scripts.
Thanks

If I define the request = [], then how can I set the request name?

request.setName(“CreateUser”)

Thanks in advance

You are creating an empty List when you define the List as

List<String> request = []
or
List request = []

Now, if you are trying to put the String, CreateUser, into the List, then:
request.add("CreateUser")

this will put CreateUser at the end of the list (and if it was empty, then this will be the first entry).

If you are trying to type the String CreateUser out, then you can use:
request.get(0)
or
request[0]

https://docs.oracle.com/javase/8/docs/api/java/util/List.html

Thank you for the quick response. I understand what you are trying to say for the List. Howerver if I add my responses and
requests into a List then how would I set request name.

Originally, I had the following steps and I wanted to change it so I wouldn’t have to use the response1 and response2…etc

response1 = WS.sendRequestAndVerify(findTestObject(‘ABC/GetUserId’, [(‘UserId’) : “TestUser01”]))
request1 = WSResponseManager.getInstance().getCurrentRequest()
request1.SetName(“CreateUser”)

Now I’m changing how my response and request are saved by using List.

List response = []
List request = []

response.add( WS.sendRequestAndVerify(findTestObject(‘ABC/GetUserId’, [(‘UserId’) : “TestUser03”])))
request.add( WSResponseManager.getInstance().getCurrentRequest())
request.SetName(“CreateUser”) - this is what I don’t know how to do when using List

If you look at my thought bubble above, I showed how to display the requests and responses as Lists. So, rather than just the variable, you need the index as well. (Lists start at zero.)

request[0].setName("CreateUser")

or

request.get(0).setName("CreateUser")

I’ve tried both ways you have proposed here, now I’m getting an error.

List response = []
List request = []

response.add(WS.sendRequestAndVerify(findTestObject(‘ABC/GetUserId’, [(‘UserId’) : “TestUser03”])))
request.add(WSResponseManager.getInstance().getCurrentRequest())
request.get(0).SetName(“CreateUser”)

No signature of method: com.kms.katalon.core.testobject.RequestObject.SetName() is applicable for argument types: (java.lang.String) values: [CreateUser]
Possible solutions: setName(java.lang.String), getName(), getAt(java.lang.String)

Oh, the reference seems to be “setName” with a lowercase “s”, not a capital as you have it.

How about print out what you have in the List just to see if we have captured the references?

List response = []
List request = []

response.add(WS.sendRequestAndVerify(findTestObject('ABC/GetUserId', [('UserId') : "TestUser03"])))
request.add(WSResponseManager.getInstance().getCurrentRequest())

for (int i = 0 ; i < response.size(); i++) { 
    println(“your no. ${i} response is ${response[i]}”) 
}

for (int i = 0 ; i < request.size(); i++) { 
    println("your no. ${i} request is ${request[i]}") 
}

request.get(0).setName("CreateUser")
1 Like

Thank you so much for your help. As you mentioned, the issue was the S with uppercase in setName.
Once I fixed that, list worked.