Control the order of a collection run with setNextRequest
A collection run sends requests in tree order: each folder's subfolders first, then its requests. That's right for most runs. Sometimes you need a loop (poll until a job finishes), a skip (no need to create a user who already exists) or a branch (test the refund path only if the payment went through).
pm.execution.setNextRequest does that, in the app's runner and in requestarium run-collection, the same way as in Postman.
How it works
Call it in a pre-request or post-response script. When the current request finishes, the runner goes on with the first request of that name in the run, instead of the next one in the tree:
pm.execution.setNextRequest('Check status');Rules worth knowing:
- The name is the request's name, not its path. If two requests share a name, the first one in run order wins, so give jump targets unique names.
- A name that isn't in the run ends the iteration, the same as
null. A typo stops the run early rather than silently going on. - Legacy
postman.setNextRequest(...)calls are converted topm.execution.setNextRequestwhen you import a Postman collection. pm.info.requestNameandpm.info.iterationtell a script where it is.
Loop: poll until a job is done
Three requests in a folder: Start export, Check status, Download export.
// Start export — post-response
pm.environment.set('jobId', pm.response.json().id);
pm.environment.set('polls', 0);// Check status — post-response
const status = pm.response.json().status;
const polls = Number(pm.environment.get('polls')) + 1;
pm.environment.set('polls', polls);
if (status === 'running' && polls < 30) {
pm.execution.setNextRequest('Check status'); // ask again
} else {
pm.test('export finished', () => pm.expect(status).to.equal('succeeded'));
}Run it with a delay so you don't hammer the API:
requestarium run-collection "Exports" --env Staging --delay 2000--delay (or Delay between requests in the app) waits between every request, the repeated ones included. With 30 polls and 2 seconds, the job has a minute to finish. Always cap the loop yourself; the 1,000-jump limit is a safety net, not a timeout.
For another approach from the shell, see test async API jobs.
Skip: don't create what's already there
// Find test user — post-response
const users = pm.response.json().data;
if (users.length) {
pm.environment.set('userId', users[0].id);
pm.execution.setNextRequest('Get user'); // skip "Create test user"
}Branch: only test the refund if the payment succeeded
// Create payment — post-response
const payment = pm.response.json();
if (payment.status !== 'succeeded') {
pm.test('payment succeeded', () => pm.expect(payment.status).to.equal('succeeded'));
pm.execution.setNextRequest(null); // nothing after this makes sense
}Ending the iteration early is better than sending ten requests that fail for the same reason. If you want the whole run to stop at the first failure, use Stop on first failure or --bail instead.
Loop over a list
Delete every record the run created, one request at a time:
// Delete next — pre-request
const ids = JSON.parse(pm.environment.get('createdIds') ?? '[]');
pm.environment.set('currentId', ids.shift() ?? '');
pm.environment.set('createdIds', ids);// Delete next — post-response
const left = JSON.parse(pm.environment.get('createdIds'));
if (left.length) pm.execution.setNextRequest('Delete next');The URL is DELETE {{baseUrl}}/records/{{currentId}}. See store objects and arrays in variables for how createdIds is built.
When not to use it
Jumps make a run harder to read. Before adding one, check whether a simpler tool fits:
- Fixed order? Order the requests in the tree.
- Setup and clean-up? A test suite has Setup and Clean up roles, and clean-up always runs. See setup and clean-up steps.
- Same requests for many inputs? Use a data file.