0

I have created two environments and need to setup some environment specific variables in 'Pre request script' of the first request based on the currently running environment in collection runner. Is there any way to do this?

Or Is there any way to get currently running environment name in 'Pre request script' of the request?

karel
  • 5,489
  • 46
  • 45
  • 50
prawin
  • 413
  • 7
  • 16

1 Answers1

0

In your Pre-Request Script, you can use the built-in "environment" variable to get a json object of all variable in that environment. I would add another variable (name it whatever you want) that has the same name as your environment... then I think you can use that to do conditional logic in your scripts.

Example:

if (environment.environment_name === "local") {
    //do something
}

NOTE: "environment" is a read-only variable. If you want to change anything in it, you have to use the setEnvironmentVariable API.

EDIT: Add an environment variable that contains the name of the environment

Ed Meacham
  • 543
  • 1
  • 5
  • 19
  • I'm already doing this to get all other environment variables.. but the problem which i'm facing is to get name of the currently running environment name in collection runner process.. need to create few environment variables based on this.. (Note : I've created these environments through Manage environment setting)... – prawin Dec 12 '17 at 21:29
  • I don't think there is [currently] a way to get the name of the environment, unless you add a variable to that environment that has the name in it... it's a hack, but unless I am not understanding what you mean, you should be able to add an environment variable that tells you which environment you're in. I will update my answer with a screenshot of what I mean. – Ed Meacham Dec 12 '17 at 21:47
  • Thanks for the screenshot. :) I'm trying to get the name of the environment ('SO Example' as per your example) in collection runner process.. – prawin Dec 12 '17 at 22:02
  • In the variable list, you'll see a variable called environment_name... I should have called that "SO Example" instead of local. – Ed Meacham Dec 12 '17 at 22:35
  • @prawin did you get this figured out? – Ed Meacham Dec 15 '17 at 02:21
  • I didn't find any way get environment name.. so i'm setting up all the required variables for the collection runner in first request of the collection.. – prawin Dec 15 '17 at 21:35
  • Right. There isn't a way to get the name of the environment from the environment you select... so to get around that you need to add an environment variable with the name of the environment when you set it up, like in my example. You shouldn't need to do it upon the first request, you should be able to add it from the "Manage Environments" section. (Those variable persist) – Ed Meacham Dec 16 '17 at 02:39