Variables not getting reset with new iteration

Your question may already have an answer on the community forum. Please search for related topics, and then read through the guidelines before creating a new topic.

Here’s an outline with best practices for making your inquiry.

My question:
How do we update/reset the variable after each iteration?

Details (like screenshots):

I am trying to use postman to do data testing on some pagination endpoints. Also, using data file to run iterations. Issue is, variable is not getting reset after iteration is finished. I am using postman’s echo endpoint as POC.

My scenario: I want to run api once. Depending on record count I get, I want to jump to last page, and validate the final page records. For this I am trying to update the pageNumber in tests and then re-running the api with updated values.

Here is the data file that I am using in runner,

[ { “pageNumber”: 0, “pageSize”: 2 }, { “pageNumber”: 0, “pageSize”: 5 }, { “pageNumber”: 0, “pageSize”: 10 } ]

Steps:

  1. In postman runner, select data file. This should give 3 iterations.
  2. Save response, so we can validate the executions
  3. Run Postman

Iteration 1 runs fine. pageNumber value is updated correctly. Ideally, for iteration 2, pageNumber value should be reset to 0, as that’s what I provide from my data file. But postman persists the previously calculated value, and does not update this value.

URL = https://postman-echo.com/get?pageNumber={{pageNumber}}&pageSize={{pageSize}}&totalRecords=46

Below is the execution log from console. Highlighted in red, pageNumber should get updated for new iteration, but it doesn’t.

image

Could someone please suggest what am I doing wrong here? or if I am missing out something wrt how variable update works with data files.

How I found the problem:
While testing the actual API. So I created a sample using postman echo api, and replicated the issue.

I’ve already tried:
Moving variables to environment and collection, but no luck.

Hi @anshulkhandelwal02

What’s in your pre-req scripts?

Those console.log statements don’t appear to show the same text as what’s being output in your test tab screenshot.

Do you have other work going on inside your pre-req?

Are you trying to ‘reset’ the variables, or are you simply trying to read them sequentially from the data file?

This is my pre-request script.

What my ask is that, with every iteration from data file, variables should be set as per value form data file. Currently, the value of pageNumber is not getting updated when new iteration starts. It’s picking up the last value.

[ { “pageNumber”: 0, “pageSize”: 2 }, { “pageNumber”: 0, “pageSize”: 5 }, { “pageNumber”: 0, “pageSize”: 10 } ]

Is this the entire data file, or are there other lines?
I tried to add it to a test script as-is and it didn’t like it (I assume because it’s incorrectly formatted JSON?).

It’s an odd one this because on face value I would expect what you have to work… :thinking:

Here is the updated one,

[{"pageNumber": 0,"pageSize": 2},{"pageNumber": 0,"pageSize": 5},{"pageNumber": 0,"pageSize": 10}]

And yes, this is entire data file.
My actual scenario is hitting my project API, and verify pagination scenarios.
I created this sample to show how the variables are not getting updated when new iteration starts.

Iteration 1 will have 2 requests, with pageNumber getting set as 23 in 2nd request.
In iteration 2, pageNumber should get set as 0 at start as that’s what the value is in data file. But value still remains 23.

This line, “sets” the variable;

pm.variables.set("pageNumber", (Math.floor(responseData.args.totalRecords / responseData.args.pageSize)));

Is taking the total records, which is hardcoded in your URL as ‘46’;
image

And then divides it by {{pageSize}}
image

46 / 2 = 23

Hope this helps!

You are missing the point. I am talking about pageNumber. If you see my ques, I have added an image of console logs. Iteration 0 is all good. In Iteration 1, PageNumber should get set as 0 as that’s what the value is in the data file. But it’s still remaining as 23. Then what’s the point of using data files, as the variable is not taking its value from the data file.

Expected Result:
Iteration 0 - Request 1 - page number = 0
Iteration 0 - Request 2 - page number = 23
Iteration 1 - Request 1 - page number = 0
Iteration 1 - Request 2 - page number = 9
Iteration 2 - Request 1 - page number = 0
Iteration 2 - Request 2 - page number = 4

Yes, you are right, I didn’t fully read the question, apologies…
I also didn’t realise there was a second request being made…

So, try this…

Use “pm.iterationData” to grab the variable from the data file.
Put this line of code in your pre-req in request 1.

pm.variables.set("pageNumber", pm.iterationData.get("pageNumber"));

Like this;


Then change “setNextRequest” to trigger the 2nd request

postman.setNextRequest('Request 2');

Like this;


Then change the “setNextRequest” in request 2, to point back at request 1
(Request 1 triggers Request 2 and Request 2 triggers Request 1)

postman.setNextRequest('Request 1');

Like this;

This is my output;

I think this is what you were asking for?