1

I am using a postman to automate APIs.

Now I am using following request, lets say :-

{
  "customerId": "{{currentClientId}}"
}

Where clientid is a dynamic variable whose value is substituted dynamically as 1 , 2, 3,4 so on.. I call this request multiple times using setNextRequest call in this eg lets say 10.This is being done using a counter variable. I am initialising the counter in my previous request to 0 and using for loop with value as counter as 10 calling the request 10 times.There is no response in body just successful http code 204.

I want to store all these clientids coming in request into an environment Client array variable so I wrote a following pre-request script:-

counter = pm.environment.get("counter");
ClientArray = pm.environment.get("ClientArray");
ClientArray.push(pm.environment.get("currentClientId"));
pm.environment.set("ClientArray",ClientArray);

In Test Script, wrote following code :-

counter = pm.environment.get("counter");

if(counter<=10) {
    console.log("hi");
    postman.setNextRequest("Request");
    counter++;
    pm.environment.set("counter",counter);
    console.log("Counter",counter);
}

The above scipts is throwing TypeError | ClientArray.push is not a function.

Could someone please advise how to achieve this?

2
  • Are you using this in the runner? How does the setNextRequest command know that there are 10 different ids? Is that all the code you have in the pre-request? What does the actual response body look like? You need to provide more details if you want someone to help you out with this question. Commented Oct 18, 2019 at 21:43
  • Added some more details , Hope this gives sufficient information. Commented Oct 19, 2019 at 7:25

1 Answer 1

4

When you retrieve a value from an environment variable like you're doing:

ClientArray = pm.environment.get("ClientArray");

You're not getting an array, you're getting a string which is why you're getting that error. You need to treat the variable like a string, append the currentClientId much like you do for the counter. Something like:

var currentClientIds = pm.environment.get("ClientArray");
currentClientIds = currentClientIds + "," + currentClientId

When you're done appending i.e. out of your loop simply take the string and convert it to an array:

var currentClientIds = pm.environment.get("ClientArray");
var idsArr = curentClientIds.split(',');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.