3
votes

Note: I'm using a simple test API for this inquiry hoping it will be better to understand my inquiry. The URL for the API is https://jsonplaceholder.typicode.com/users.

I'm setting up a Postman Collection with only one Post request to be called. On the Postman script, I parameterized only 4 fields for testing purposes. I used a csv file as test data with 2 iterations where the parameter "run" is intended to tell Postman if the iteration should be executed or not.

run,id,name,username,email
yes,11,Test One,testone,[email protected]
no,12,Test Two,testtwo,[email protected]

I researched online and saw the command postman.setNextRequest(null); as a command to stop the iteration. But still, when the run value is "no", the iteration still runs the API call.

I'm not sure if this is the correct command I need to skip an iteration during the Postman Collection run. If not, may I ask what is the correct command.

Thanks!

I've added a javascript code in the Pre-requisite tab to get the parameter value for "run" and evaluate it if the API corresponding to the iteration is to be executed or not

var run = pm.iterationData.get("run");
console.log("run = " + run);

if (run == "no") {
    console.log("Do NOT run iteration");
    postman.setNextRequest(0);
}
else {
    console.log("Run iteration");
}

console.log("Test Run!");

Expected: The Post API request call will be executed for iteration 1 because run = yes and will NOT be executed on iteration 2 because run = no.

Actual: The Post API request were called on both iterations 1 and 2. Also, when I checked on the console.log, the string "Test Run" were both logged.

run = yes
Run iteration
Test Run!
POST https://jsonplaceholder.typicode.com/users

run = no
Do NOT run iteration
Test Run!
POST https://jsonplaceholder.typicode.com/users
2

2 Answers

0
votes

postman.setNextRequest() is always executed at the end of the current request. This means that if you put this function before other code blocks anywhere in pre-request or test script, these blocks will still execute.

See documentation for more details.

0
votes

When using the iteration-functionality through a data file (json or csv), one should remove any postman.setNextRequest()-command from the pre-request script. Having this command will re-trigger the iteration, starting from the first element, in an infinit loop. Removing this command, will make the iteration function properly and looping through each separate iteration as expected.