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

Environment variables don't get set from after-response scripts when using the test runner #7927

Open
1 task done
suttonb opened this issue Sep 7, 2024 · 20 comments
Open
1 task done
Labels
C-improvement Category: Improvement / Enhancement PA-testing Package: Insomnia Testing S-verified Status: Verified by maintainer

Comments

@suttonb
Copy link

suttonb commented Sep 7, 2024

Expected Behavior

I have a request that sets an environment variable in an after-response script. When running that request as part of a test run, I would expect the environment variable to be set after the request has run.

Actual Behavior

No environment variables have been set after the request has run.

Reproduction Steps

  1. Go to Documents
  2. Create new document
  3. Go to Collection tab
  4. Create new HTTP request
  5. Enter any valid url for the request url
  6. Go to Scripts tab in the request
  7. Select After-response
  8. Enter insomnia.environment.set("variable_name", "variable_value");
  9. Go to Tests tab in the document
  10. Create new test suite
  11. Create new test in the suite
  12. For the request in the new test, select the request you created
  13. Run the test
  14. Check environment variables for a variable named variable_name

Is there an existing issue for this?

Additional Information

No response

Insomnia Version

Version: Insomnia 9.3.3
Build date: 7/31/2024

What operating system are you using?

Windows

Operating System Version

Windows 11 Home version 23H2

Installation method

download from insomnia.rest

Last Known Working Insomnia version

No response

@suttonb suttonb added B-bug Bug: general classification S-unverified Status: Unverified by maintainer labels Sep 7, 2024
@subnetmarco
Copy link
Member

@ihexxa to take a look.

@ihexxa
Copy link
Contributor

ihexxa commented Sep 9, 2024

Hello @suttonb as the difference between underlying mechanism, script capability is not supported in the original test tab at the moment. But the collection runner feature will be released in week so probably you could have a try with it, which supports all scripting interfaces.

@subnetmarco
Copy link
Member

Ah, I totally missed that this was about the test suites. @suttonb we are in the process of supporting the new SDK into the test feature as well, but it's not ready yet.

@suttonb
Copy link
Author

suttonb commented Sep 9, 2024

That sounds great, thank you! Will the collection runner have the ability to create customized run orders from the requests in the collection? For example, say I have a set of requests labeled A - F. I have one workflow where I want to run them all A - F in order, but I may have another workflow where I want to run A B C A D B F. Or another where I want to run D B E C F C A. Insomnia's test suites allow me to create those custom orders without needing to maintain multiple copies of the same requests, will the collection runner work the same way?

@ihexxa
Copy link
Contributor

ihexxa commented Sep 11, 2024

The collection runner supports re-ordering requests, while it requires workaround if one request appears more than 1 time in the workflow. There are some minor differences between the original tests and the runner.

@suttonb
Copy link
Author

suttonb commented Sep 11, 2024

Ok, thank you for the clarification!

@ihexxa ihexxa added PA-testing Package: Insomnia Testing S-verified Status: Verified by maintainer C-improvement Category: Improvement / Enhancement and removed S-unverified Status: Unverified by maintainer B-bug Bug: general classification labels Sep 12, 2024
@arkaitzosa
Copy link

Hi! Can we use the collection runner for CI with inso CLI? we need to execute pre-request script and using test suites is not posible.
Thanks!

@subnetmarco
Copy link
Member

@arkaitzosa within 30 days you will be able to do so.

@NadjaKvalit
Copy link

@subnetmarco hello. Could you please provide an update on fixing this issue? As I understand it, this issue is related to my problems as well: I can’t get folder environment variables in after-response scripts to execute tests there. I can only get global environment variables. However, if this variable is declared as a Variable from Response -> Body Attribute, it is read as a formula, not a value from the response. However, if I check this variable in Edit Tag - Live Preview, I see the correct value.
Thank you.

@ihexxa
Copy link
Contributor

ihexxa commented Oct 12, 2024

Hello @NadjaKvalit, this issue is mainly related to the "test tab" and I believe your request is related to the scripting capability. We may prioritize the folder-level environment variables accessing after the collection runner is fully completed. However regarding the response body formula, it is only evaluated after the pre-request script has been executed together with other tags and so it is by design.

@nomeaning25
Copy link

nomeaning25 commented Oct 21, 2024

Hello. I have a similar issue and don't know if this is related. If I set a variable in after-response, the value is not set to collection variables to be used in the next request I run by hand. But if I add a pre-request scirpt where I read the variable and add try to get it the the variable seems to be set just fine.

This is my example:
Test one - post-response script

var responseJson = insomnia.response.json();
insomnia.collectionVariables.set("access_token", responseJson.access_token);

Test two - pre-request script

insomnia.request.addHeader({key: 'Authorization', value: 'Barrer ' + insomnia.collectionVariables.get("access_token") });

And this works, but I would still like to add this through Headers or Auth part of the request.

@ihexxa
Copy link
Contributor

ihexxa commented Oct 22, 2024

Hello @nomeaning25 , hm if you can't find the persisted variable in the environment modal, probably you could check the "base environment", as it is mapped from Postman's sdk.

@nomeaning25
Copy link

Where can I find that. If I click on my main folder and go under environment, the variable is there but its empty. It is not changed during the post-response script.

@ihexxa
Copy link
Contributor

ihexxa commented Oct 22, 2024

@nomeaning25 You can find environment modal by clicking edit icon in the left-top part:

image

Then choose the "base environment" (the top one but it could be renamed) and see if the variable field in the right json content.

image

Besides, insomnia.environment.set is a similar approach but which persists variables to the selected environment.

@nomeaning25
Copy link

nomeaning25 commented Oct 22, 2024

Yes, I can see the value here. How can I fetch it to the Headers tab? From base environment? Since _.variable is empty. I get nothing in Live Preview. Could the issue be in variable name? It's "access_token". Neither {{access_token}} under Custom or "_.access_token" works

@ihexxa
Copy link
Contributor

ihexxa commented Oct 22, 2024

@nomeaning25
I may not fully understand your problem, probably sharing some screenshots could help.
You can just reference it as normal like {{ access_token }} or {{ _.access_token }}, as Insomnia also reads from the base environment for variables.

Here is a more detailed doc: https://docs.insomnia.rest/insomnia/environment-variables

@nomeaning25
Copy link

nomeaning25 commented Oct 22, 2024

Yes, that is how I'm trying to access them but they are not filling from environment. Maybe the problem is that I also have the same variables set in Folder envirnomnet? This was created when I imported my Postman Collection. Can I save the variables there?

image
image

Upon the After-response that was created after import was:


var responseJson = insomnia.response.json();

insomnia.collectionVariables.set("access_token", responseJson.access_token);

And it created the folder environment.

EDIT: Yes, if I delete them from my Folder Settings and change my after-response script to store the variable in environment insetad od collectonVariables, then it works. But in case I import a large collection from Postman this would be a pain to fix all by hand.

@ihexxa
Copy link
Contributor

ihexxa commented Oct 22, 2024

@nomeaning25 I removed the screenshot may might contain your credentials.

Maybe the problem is that I also have the same variables set in Folder envirnomnet?

Yes, Insomnia reads variables by this order: folder envs -> selected workspace's environment (if exists) -> workspace base environment -> global selected env -> global base env.

Yes, if I delete them from my Folder Settings and change my after-response script to store the variable in environment insetad od collectonVariables, then it works. But in case I import a large collection from Postman this would be a pain to fix all by hand.

There might be workaround currently, you could move content from "the top folder environment" to the base environment, that should work.

@nomeaning25
Copy link

Thank you. Will try that for now.

@alexndrperz
Copy link

I had the same issue, and the solution was create a Collection Environment, select it and execute the typical script after-response,
I don't know if it's the same context, Insomnia seems to store the environment variables of the script in the Collection Environment.

var response = JSON.parse(insomnia.response.body)
insomnia.environment.set("variable", response.variable)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-improvement Category: Improvement / Enhancement PA-testing Package: Insomnia Testing S-verified Status: Verified by maintainer
Projects
None yet
Development

No branches or pull requests

7 participants