0

I have a collection with two folders, one for POSTs and one for GETs

Collection

At the collection level, I have set variables

Collection variables

And the following collection-level scripts to be run after every request:

requestLast = pm.variables.get("requestLast");
requestCurrent = pm.variables.get("requestCurrent");
statusGet = pm.variables.get("statusGet");

requestLast = requestCurrent;
requestCurrent = pm.request.name;

I want to always be keeping track of the previously run request, so I can return to it when necessary.

In the 'positivePosts' folder I have the following test script:

if(statusGet === 0) {
    postman.setNextRequest("resultsPositive");
}
else {
    statusGet = 0;
}

pm.variables.set("requestLast", requestLast);
pm.variables.set("requestCurrent", requestCurrent);
pm.variables.set("statusGet", statusGet);

The individual POST requests have no test scripts.

The results folder does not have any tests, but the resultsPositive GET has this test script:

var jsonData = JSON.parse(responseBody);
schema = pm.variables.get("schemaPositive");
tests["Valid Schema"] = tv4.validate(jsonData, schema);
tests["Status code is 200"] = responseCode.code === 200;

statusGet = 1;
postman.setNextRequest(requestLast);

pm.variables.set("requestLast", requestLast);
pm.variables.set("requestCurrent", requestCurrent);
pm.variables.set("statusGet", statusGet);

There are no pre-request scripts anywhere in the collection.

When running the collection, I would expect this order:

  1. postRich
  2. resultsPositive
  3. postAllProperties
  4. resultsPositive
  5. postMinimum
  6. resultsPositive

However, what I actually see is:

  1. postRich
  2. postAllProperties
  3. postPositive

I also don't understand why postPositive is not run after postRich.

Scott
  • 115
  • 1
  • 5
  • I'm pretty sure that you can only currently read or get from the collection variables and not create or set them in the way that you're trying to do. – Danny Dainton May 17 '18 at 15:31
  • The documentation suggests it's possible. It seems bananas to not be able to change a variable across tests.https://www.getpostman.com/docs/v6/postman/environments_and_globals/variables – Scott May 17 '18 at 16:26
  • You can create the variable through the UI like you have done and reference it in the `.get()` function but I don’t think it goes the other way using the `.set()` like the global and environment variables do. That page you links say that it’s done differently too. – Danny Dainton May 17 '18 at 17:44
  • But I'm able to correctly modify the variables and retrieve the modified variables later, it just doesn't work for the first request. – Scott May 18 '18 at 08:17
  • If you were to take one of your single requests and do something like `pm.variables.set("some_number", 1)` in the Tests tab - I'm sure it would never set that as a collection level variable. This can only be done by manually setting this in the collection folder UI. Do you persist the variables when using the collection runner? – Danny Dainton May 18 '18 at 08:24

0 Answers0