How to increase Variable value based on the iteration being run in Postman
Asked Answered
R

2

13

I have an API request that I need to run in Postman-Collection-Runner thru multiple iterations. The API request uses Variable.

How can I make this variable to automatically increase with each iteration (or maybe set the iteration value as another Variable)?

Responsion answered 30/7, 2019 at 21:3 Comment(0)
Z
29

If I understand your question correctly, you would like to assign different values to a variable in the request in different iterations which is achievable in 2 ways.

a) Using data files

https://learning.getpostman.com/docs/postman/collection_runs/working_with_data_files/

The data files could be in JSON or CSV format. Unfortunately, there is no way in Postman to tie the variable values to another variable unless you want to do it in a hacky way!

b) Pre-request & Tests scripts

1- Initialise the environment variable in the Pre-request Scripts like this:

var value = pm.environment.get("var");

if( !value) {
    pm.environment.set("var", 1);
}

2- Increment the variable value in Tests

var value = pm.environment.get("var");

pm.environment.set("var", value+1);

This creates an environment variable and increments it after each iteration. depending on how you structure your collection you might need to consider flushing/resetting the environment variable to be ready for the next run

It worth mentioning that Pre-request Scripts and Tests running before and after the requests respectively, so you can write any scripts that would like to run after the request in the Tests. It shouldn't be necessarily a test!

Zabaglione answered 31/7, 2019 at 18:46 Comment(4)
Thanks @Mehran. But I'm looking for something (probably) more simple. I do not want to use any data-files. But what I want is that my Variable i.e. {varId} starts from 10 for the first iteration, and then will increase automatically by +1 for every next iteration. Thus after 10s iteration the last valu will be '20' etc.Responsion
Thanks @Mehran! I think that is what I need. I'll give it a try.Responsion
One more question @Mehran, do you know if there is a way to set that value property back to null/empty at the end of the iterations (i.e. if I run it for 100 iterations, after the run #100 this property will be reset back to null)?Responsion
@Responsion pm.info.iteration gives you the iteration number (starts from 0) and pm.info.iterationCount is for the total number of iterations. so a simple check in the Tests section will work. you probably need sth like this for it: if (pm.info.iteration == pm.info.iterationCount-1){ pm.environment.set("var", <TheDesiredFinalValue>); } else{ var value = pm.environment.get("var"); pm.environment.set("var", value+1); } Zabaglione
C
14

1. Using Global pm.* functions and Variables in Pre-Request Scripts/Tests

Pre-Request script - runs before executing the request

Tests - runs after executing the request

a.

pm.variables.set("id", pm.info.iteration);

Ex: example.com/{{id}}/update gives

example.com/0/update

example.com/1/update etc...

Number of iterations is set in Collection Runner. pm.info.iteration key has the current iteration number, starting at 0.

b.

var id = +pm.globals.get("id");
pm.globals.set("id", ++id);

The variables can be in any scope - globals/collection/environment/local/data.

In Collection Runner, check the Keep Variable Values checkbox, to persist the final value of the variable in the session (here id).

Note: If the variable is accessed via individual scopes (via pm.globals.* or pm.environment.* or pm.collectionVariables.*), then the mentioned checkbox should be toggled as required. Else if accessed via local scope (pm.variables.*), the value will not be persisted irrespective of the checkbox.

Ex: Same as above

More on variables and scoping


2. Using Dynamic variables

These variables can be used in case random values are needed or no specific order is necessary.

a. $randomInt - gives a random Integer within 1 - 1000.

Ex: example.com/{{$randomInt}}/update gives

example.com/789/update,

example.com/265/update etc...

b. $timestamp - gives current UNIX timestamp in seconds.

Ex: example.com/{{$timestamp}}/update gives

example.com/1587489427/update

example.com/1587489434/update etc...

More on Dynamic variables


Using Postman 7.22.1, while answering this. New methods may come in future.

Cnidoblast answered 21/4, 2020 at 17:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.