Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environmental variable not updated #4197

Closed
ivaylogeorgiev1 opened this issue Feb 15, 2018 · 7 comments
Closed

Environmental variable not updated #4197

ivaylogeorgiev1 opened this issue Feb 15, 2018 · 7 comments

Comments

@ivaylogeorgiev1
Copy link

ivaylogeorgiev1 commented Feb 15, 2018

Hello,

I have pre-request script on collection level with a method (self invoked )that uses the environment variable X
On request level i have pre request script that updates the environment variable X
then I'm calling the method from the collection level pre -request script and the X is not updated.

Is that a bug ?

@harryi3t
Copy link

Order of execution is

collection pre-req script -> req pre-req script -> req test script -> collection test script

So, any variable set from req level pre-req script will not be available in the collection pre-req script

@ivaylogeorgiev1
Copy link
Author

the variable is updated at the end when you inspect it, but it seems that when the collection level pre request script runs some kind of snapshots of the variables is made and the update of the request pre request script change on the variable is not considered.

@harryi3t
Copy link

Yes variable will be updated but because of the order of execution collection pre-req script cannot access it

// Order of execution

collection pre-req script  // Tries to get variable X: Not found
  |
  V
req pre-req script // create variable X
  |
  V
req test script
  |
  V
collection test script

// In the end: X is present

@ivaylogeorgiev1
Copy link
Author

ivaylogeorgiev1 commented Feb 16, 2018

here is an example- "try changing this value" - change this value in different requests

create test variable( in the case global) testVariable

Collection pre request scirpt

(function TestFunction(){
    
    console.log("Pre- request Script Folder = " + pm.globals.get("testVariable"));
    
    logTheVariable = function() {
        console.log(pm.globals.get("testVariable"));
    }
} ());


request pre req script

console.log("*1* " + pm.globals.get("testVariable"));
pm.globals.set("testVariable", "try changing this value");
console.log("*2* " + pm.globals.get("testVariable"));
logTheVariable();




so 2 gives the updated value but logTheVariable(); gives us the old value. At the end the variable is updated and if you call run again 1 will give you the new value

some kind of snapshot of the variables is made when collection pre req is called which i believe is a bug

@harryi3t
Copy link

@ivaylogeorgiev1 I was able to reproduce this.
Sharing javascript global variables or functions across scripts is not an intended feature and not reliable. It may work in some cases.

logTheVariable should not even be accessible to the request script.
If you are using this behaviour to keep common functions in collection-level scripts and reusing them in other request scripts, I would recommend you to serialize the common functions and store it in some Postman variable (global or local* variables) and the eval it in other places

// Collection script
function logTheVariable () {
  ...
}
pm.variables.set('logTheVariable', logTheVariable.toString())

// Request script
eval(pm.variables.get('logTheVariable'))
logTheVariable()

@ivaylogeorgiev1
Copy link
Author

yes, this work around is clear to me but I'm still thinking that this is a bug.

@lsloan
Copy link

lsloan commented Feb 19, 2018

@harryi3t, you wrote:

Sharing javascript global variables or functions across scripts is not an intended feature and not reliable. It may work in some cases.

Although it's not an intended feature, I think it's one that's desperately needed. Are there any plans to support it in the near future?

I think definitions from the various scripts should cascade:

  1. Collection pre-request
  2. Folder pre-request
  3. Request pre-request
  4. Collection test
  5. Folder test
  6. Request test

That is, code in script 𝒏 should be able to access definitions from scripts 1 through 𝒏.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants