How to detect last step in collection Runner

Hi
How to detect the last step in collections Runner?

For collection Runner I provide a data from CSV file. I allow some variables to be missing if in previously lines were defined. So I use variables to share them. But I don’t want them for future use so I need clean them in the last step. I use this at Tests tab:

if ( pm.iterationData.has("parent") && pm.iterationData.get("parent") ){
    pm.environment.set("parent", pm.iterationData.get("parent"));
}

Maybe there is another way to do it? Another type of variable, only for Runner, global for all iterations, but I don’t know.

I tried also with pm.collectionVariables, the same.

Hey @dracorp

Welcome to the Postman community! :trophy:

Just trying to clarify what you mean as the last step - Do you mean the last request of that iteration or the final request of all the iterations?

This would remove that variable, if that’s what you mean?

pm.environment.unset('parent')  

Hi @dannydainton

Just trying to clarify what you mean as the last step - Do you mean the last request of that iteration or the final request of all the iterations?

Good question.
I mean final request of all the iterations. In on Pre-request Script I have defined:

if (pm.iterationData.has("parent") && !pm.iterationData.get("parent")) {
    pm.variables.set( "parent", pm.collectionVariables.get("parent") );
}

And my CSV data looks like:

parent,title,space,body
parent_page,title_page,space_page
,another_title
,yet_another_page

The 1st is the header, 2nd defines parent=parent_page, title=title_page, and space=space_page. For the 4rd and 5th I omit parent and space variable.
For the 1st iteration on Test tab I set two collection variables:

if ( pm.iterationData.has("parent") && pm.iterationData.get("parent") ){
    pm.collectionVariables.set("parent", pm.iterationData.get("parent"));
}
if ( pm.iterationData.has("space") && pm.iterationData.get("space") ){
    pm.collectionVariables.set("space", pm.iterationData.get("space"));
}

So on the last step(iteration) I have or I want to unset those variables. I want to perform some cleaning operations to avoid mistakes.

Maybe I’m getting off to it wrong?

Hi @dracorp

Welcome to the community! :clap:

Great question! and a common use case might I say. Personally, I haven’t done this and can’t remember how others have. Just shooting from the hip here and thinking out loud, maybe you can add one more entry at the bottom of your CSV, thats kinda a dummy entry, and contains dummy data. However, this entry has an indication that you have completed all the entries you care about, and is the “last entry”. From here you can do some cleanup / teardown, and skip attempt to execute this last entry at all. Moreso, its just an indicator to end, and not an actual test, so you can skip any tests and it shouldnt then affect your test results.

Hope this helps!

Orest

Yes, I was thinking about this solution. However, I don’t like it because you have to remember to add extra line to the data. I prefer more elegant logic in code.