Run a request only once and the rest request normal

Hello, I have a collection in newman with multiple request, that requires checking that for every instance and all the lang of that instance, different services are working. After that, I generate a report (with htmlextra, as is the one I know that allows me to modify the template of the report) with the results of the test.

To collection take a csv like the next one to be execute.

instance, lang
a, en
a, es
b, en
b, es

The problem is that some services doesn’t require checking for lang, only for instance. For most of the request, is fine to execute the request with a-en, a-es, b-en and b-es, but, for one specifc request, is only relevant the instance, so I would only need to execute the request for a and b, only once for each instance.

Note that here I have 2 instances with 2 langs each, but the final csv I have working on have 900 entries, I cannot repeat the same request for the same instance 80 times in the prod environment.

So, I would need some way of running one request only one time for instance, not for every line in the input file.

I have already tried throwing and error in the pre-request-script when it was duplicated (I use a env var to storage the instances already used), but that stops every request, not only the request I want.

I know I could create a new collection only for that request, but that would generate a different report, and I need to have only one report. I also know that I could use allure report that allows to merge two or more reports, but I have made changes in the template, as I need to show in the report the env vars used for each request in an easy way to easy analyze without having to open each iteration.

So, is there a way using the pre-request-script or something to run a request only when the value of a env var change?

Hi @danielf

Off the top of my head, I think you would need to write a conditional statement and use postman.setnextrequest();

For example;

if (condition 1 is true) {
   postman.setNextRequest("HTTP Request 1");
} else if (condition 2 is true) {
  postman.setNextRequest("HTTP Request 2");
} else {
   postman.setNextRequest(null);
}

So, being the one time request name “Request1” and the next one in the collection “Request2”, in my pre-request-script of “Request1” I would have to write something like this, right:

if (pm.environment.get("already_run") is false) {
   pm.environment.set("already_run", true);
   postman.setNextRequest("Request1");
} else {
   postman.setNextRequest("Request2");
}

Yes.

Here are another couple of examples of it;

I have read the documentation, it saids that " The postman.setNextRequest() function is always executed at the end of the current request.". I have tried with the next code, in “Request1”, but, as the setNextRequest is run after the request, the “Request1” was still executed in all the iterations.

if (pm.environment.get("alreadyRun") == undefined) {
    pm.environment.set("alreadyRun", true);
} else {
    postman.setNextRequest("Request2");
}

So, in order to run only one time, I had to move that script to the “Request0”. As the “Request0” runs before the “Request1” (the one I want to execute only once), the script now jumps it after the first iteration to the “Request2”

Thx @w4dd325 for pointing me to setNextRequest

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.