Asserting over a dynamic boolean

Here is a basic example of using setNextRequest.

I’m running this against Postman Echo with a very basic request body.

image

With the following in the pre-request scripts…

if (!pm.collectionVariables.get("booleans")) {
    pm.collectionVariables.set("booleans", JSON.stringify([]));
}

let array = JSON.parse(pm.collectionVariables.get("booleans"));

if (array.length === 0) {
    array = [true, false];
}

pm.test("boolean array is not empty", () => {
    pm.expect(array).to.be.an("array").that.is.not.empty;
});

var currentBoolean = array.shift();
console.log(currentBoolean);

pm.collectionVariables.set("bool", currentBoolean);
pm.collectionVariables.set("booleans", JSON.stringify(array));

This would probably answer your original question, as you can use the bool variable for your assertion and it would keep resetting the Boolean array, so it won’t be random, but will go, true, false, true, false, indefinitely.

However, to loop though both options, you can do this using the following in the tests tab.

var array = JSON.parse(pm.collectionVariables.get("booleans"));

var expectedResponse = pm.collectionVariables.get("bool");

var actualResponse = pm.response.json().data.Boolean;

pm.test(`response boolean = ${expectedResponse}`, () => {
    pm.expect(actualResponse).to.eql(expectedResponse); 
});

if (array.length > 0){
    postman.setNextRequest("setNextRequest Loop"); // Change to what your request name is

} else {
    postman.setNextRequest(null);
}

And the results are in…

image

One iteration, which has run the same request twice with a matching test.

Please note: You need to run this through the collection runner as setNextRequest() only works with the runner.

On a side note, you would normally get your test to fail which I can’t do as I’m only testing what I sent, so it will always pass.