insomnia icon indicating copy to clipboard operation
insomnia copied to clipboard

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

Open suttonb opened this issue 2 years ago • 22 comments

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 avatar Sep 07 '24 04:09 suttonb

@ihexxa to take a look.

subnetmarco avatar Sep 07 '24 15:09 subnetmarco

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.

ihexxa avatar Sep 09 '24 08:09 ihexxa

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.

subnetmarco avatar Sep 09 '24 14:09 subnetmarco

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?

suttonb avatar Sep 09 '24 15:09 suttonb

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.

ihexxa avatar Sep 11 '24 07:09 ihexxa

Ok, thank you for the clarification!

suttonb avatar Sep 11 '24 15:09 suttonb

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!

arkaitzosa avatar Sep 19 '24 06:09 arkaitzosa

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

subnetmarco avatar Sep 23 '24 17:09 subnetmarco

@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.

NadjaKvalit avatar Oct 11 '24 19:10 NadjaKvalit

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.

ihexxa avatar Oct 12 '24 09:10 ihexxa

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.

nomeaning25 avatar Oct 21 '24 09:10 nomeaning25

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.

ihexxa avatar Oct 22 '24 03:10 ihexxa

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.

nomeaning25 avatar Oct 22 '24 05:10 nomeaning25

@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.

ihexxa avatar Oct 22 '24 07:10 ihexxa

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

nomeaning25 avatar Oct 22 '24 07:10 nomeaning25

@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

ihexxa avatar Oct 22 '24 08:10 ihexxa

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.

nomeaning25 avatar Oct 22 '24 09:10 nomeaning25

@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.

ihexxa avatar Oct 22 '24 09:10 ihexxa

Thank you. Will try that for now.

nomeaning25 avatar Oct 23 '24 06:10 nomeaning25

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)

alexndrperz avatar Oct 28 '24 14:10 alexndrperz