0

Is it possible to skip (or repeat once again) the iteration through the collection using JSON data file in the Collection Runner like:

    if(pm.environment.get("skip").to.eql("yes"){
      \\pm.iterationData.GOTOITERATION(2)  <--PSEUDOCODE
    }

I was thinking that if I would be able to access the whole datafile (array of objects), it will be possible to write such thing:

    var currentIterationData;
    function ChangeCurrentIteration(iterationNumber) 
{ currentIterationData =
data[iterationNumber] // here I want to access element of data's array
}

But don't I go in wrong direction? does my question have sence? thank you.

Borys Fursov
  • 548
  • 2
  • 5
  • 15
  • Can you clarify the question please? Not really sure what you mean by points 1 and 2, also not sure what you need to do with `data.toJSON()` – Danny Dainton May 16 '18 at 17:04
  • @DannyDaiton , 1: VariableScope is some king of js objects in Postman Sandbox which represent's collection of Key:Value pairs, and has some methods(or functions, so you understand me). 2: I noticed that it is actually a JS object. so it makes no sense. – Borys Fursov May 17 '18 at 07:31
  • Yeah I understand what is it and how it works within the Postman application - What I don’t understand is how you would like to use it and your overall question. Is this a series of questions or one question? If there are more than 1, separate them out into their own question - you will be able to explain a single question in more detail and receive a prompt response. – Danny Dainton May 17 '18 at 07:40
  • @DannyDainton I just tried to rewrite the question – Borys Fursov May 17 '18 at 08:37

1 Answers1

1

You could achieve this with the below code:

var testID =pm.iterationData.get("testID");
if(testID.includes("<substring>")) {
    postman.setNextRequest(null);

};

Where the "testID" is one of the key value pair in data file and pm.iterationData.get("key") method retrieves the value. Based on the condition, the next request will be skipped and next iteration will continue. You could also repeat a particular request by adding:

postman.setNextRequest(<requestToRepeat>); 
AmerllicA
  • 29,059
  • 15
  • 130
  • 154
Peri
  • 34
  • 4
  • May you pls explain a little bit more. If i understood correctly this solution doesn't give me an oppotrunity to "jump back" to iteration? – Borys Fursov May 23 '18 at 10:27
  • @BorysFursov - I suppose you wanted to 'repeat' a particular request with the same object (from an array of objects) until you get a desired outcome. If that is the case, you can repeat that request like this.`var res = JSON.parse(responseBody); if (res.valueTocheck !== "expectedValue") { postman.setNextRequest("same_request"); }` This repeats with the same data until the outcome is received. please be aware you may get an error if the request us unsuccessful after three attempts. – Peri May 24 '18 at 13:26
  • thanks, but when will I have that error after 3 attempts? f.e.when there are `ReferenceError` whole run stops, what do you mean as unsuccessful request? – Borys Fursov May 24 '18 at 13:52
  • I meant the connection timeout error. Ive faced it before but worth trying that out. But I hope it answered your question. – Peri May 25 '18 at 04:46