1

I have an API that gets the status of a job. It parses the response body and looks for the "state:" of the job.

If it's INPROGRESS I'm telling the request to wait 10 seconds and rerun, but this doesn't happen, it just moves on to the next.

This is the test I'm running:

pm.environment.unset("JobStatus");
var jsonData = pm.response.json();
pm.environment.set("JobStatus", jsonData['progress-update'].state);

const sleep = (milliseconds) => {
  return new Promise(resolve => setTimeout(resolve, milliseconds));
};

pm.environment.get("JobStatus");

if (    environment.JobStatus == "DONE") 
{
  postman.setNextRequest("Export Results");
}
else if (    environment.JobStatus == "INPROGRESS")
{
  sleep(10000).then(() => {
  postman.setNextRequest("Get Job Status");
})}
else {
  postman.setNextRequest(null);
}

The data has a main task that I'm checking for and then many sub-tasks with the same state INPROGRESS string.

{
    "progress-update": {
        "@type": "parallel-progress",
        "job": {
            "@href": "/api/space/job-management/jobs/4755716"
        },
        "taskId": 4755716,
        "jobName": "Compare Config-4755716",
        "state": "INPROGRESS",
        "status": "UNDETERMINED",
        "percentage": 10,
        "subTask": [
            {
                "@type": "percentage-complete",
                "state": "INPROGRESS",
                "status": "UNDETERMINED",
                "percentage": 0
            },

The variable is being set correctly, but my else if statement is not being followed.

enter image description here

EDIT:

I changed up the logic to set the variable in a test on Request 4 and do the checking on Pre-Script Request 5. This should send collection runner back to Request 4 until the JobStatus == "DONE" but it's not.

I added some additional code to help me see how it's performing from the console:

    const sleep = (milliseconds) => {
  return new Promise(resolve => setTimeout(resolve, milliseconds));
};

pm.environment.get("JobStatus");

if (    environment.JobStatus === "DONE") 
{
  postman.setNextRequest("Export Results");
  console.log("DONE")
  console.log(environment.JobStatus)
}
else if (    environment.JobStatus === "INPROGRESS")
{
  sleep(10000).then(() => {
  postman.setNextRequest("Get Job Status");
  console.log("INPROGRESS")
  console.log(environment.JobStatus)
})}
else {
  postman.setNextRequest(null);
  console.log("ELSE")
  console.log(environment.JobStatus)
}

When this is executed I get

INPROGRESS

INPROGRESS

Printed in the console. and it just finishes the request, without ever sending itself back to request 4 to retry.

Why is the else if logic being followed but the setNextRequest being ignored?

0

2 Answers 2

0

What you're trying to do is best accomplished using collection runner and setNextRequest. I've spent hours trying to accomplish the same thing you're trying in the same way and got nowhere. This is your best bet:

https://learning.getpostman.com/docs/postman/scripts/branching-and-looping/

This will unfortunately only work in collection runner but the idea is that in a single request you do all your if statements and based on the logic you'll setNextRequest("same_request") or setNextRequest(null)

2
  • This is exactly what I am doing, my code already includes these commands and they are being utilized from collection runner. I'm just confused as to why my logic isn't working. JobStatus = INPROGRESS so it should wait 10 seconds and then check JobStatus again, until it's either DONE or something else. Instead it just continues on to the next job as if it's "DONE".
    – Sean Liles
    Nov 21, 2019 at 18:59
  • I've had problems with sleep myself before but i dont recall the problem around it, i believe it had something to do with async calls. In any case what ultimately worked for me was to use Postman's "sleep" endpoint: "postman-echo.com/delay/5" Nov 21, 2019 at 21:12
0

For those that may run into this I found the issue, or rather two issues.

My environment.JobStatus variable was not being set and i couldn't figure out how to get it to do so. So i scrapped the environment variable and just declared it within JS.

Secondly, my postman.setNextRequest("Get Job Status") was being ignored because it was nested within the sleep function. Not sure why that caused it but i dropped it out of the sleep and it loops perfectly fine now.

var jsonData = pm.response.json();
var jobState = jsonData['progress-update'].state

const sleep = (milliseconds) => {
  return new Promise(resolve => setTimeout(resolve, milliseconds));
};


if (    jobState === "DONE") 
{
  console.log(jobState);
}
else if (    jobState === "INPROGRESS")
{
  sleep(10000).then(() => {
  console.log(jobState);
})
    postman.setNextRequest("Get Job Status");
}
else {
  console.log("ELSE Clause was matched.");
  console.log(jobState);
  postman.setNextRequest(null);
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.