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

Add access to environment variables in karate-config.js #547

Closed
ianrenauld opened this issue Oct 3, 2018 · 8 comments
Closed

Add access to environment variables in karate-config.js #547

ianrenauld opened this issue Oct 3, 2018 · 8 comments
Labels

Comments

@ianrenauld
Copy link

This is more a feature request than a bug...

When I try to access environment variables in karate-config.js I get an error when process.env.my_variable is seen in the code (error is 'javascript function call failed: "ReferenceError: "process" is not defined'). This is the way we access environment variables in Node.js, but I think that the karate-config.js file does not support these types of Node.js extensions to Javascript?

I like the ability to set my environment through karate.env, but I have some configurable elements (a proxy address, which is often specified in the HTTPS_PROXY environment variable) that is not fixed for a specific environment, but can still be changed. I thought I would be able to set my proxy by reading that environment variable and setting the proxy in karate-config.js, but it seems reading environment variables is not supported. I think this might be worthwhile to add to give a bit more flexibility for complex environments.

@ptrthomas
Copy link
Member

this is supported: karate.properties['my_variable']

please refer: https://github.com/intuit/karate#dynamic-port-numbers

@ianrenauld
Copy link
Author

I was referring to environment variables defined by export in Unix and set in Windows. The Java System Properties you are referring to are nice, I can probably use that, but it's not as great as the environment variables set at the OS level.

Thanks for the help and keep up the good work!

@ptrthomas
Copy link
Member

@ianrenauld AH. sorry, I read some of these issues in a hurry.

I think you have a reasonable workaround as follows, hurray for Java interop ;)

var systemPath = java.lang.System.getenv('PATH');

java.lang.System.getenv('PATH') will even work in feature files. how does that sound ? I'll add this trick to the docs. not inclined to add syntax for this, as you are the first ever to request this.

@ianrenauld
Copy link
Author

Yes! This is amazing, thank you so much!

@stutipai
Copy link

stutipai commented Jul 13, 2022

Can you please show how we can access the environment variables from feature file or from karate-config.js?
I tried adding as follows in the karate-config.js:
var bu = karate.properties['URL']
config.baseUrl = bu

But it did not work. Also tried adding in feature file in the "Background" Section as

  • url java.lang.System.getenv('URL')

But this hasn't worked too.

My requirement is that code needs to be deployed on Google Cloud Platform and read the values from the environment variables that are set there.

@ptrthomas
Copy link
Member

@stutipai please use stack overflow: https://stackoverflow.com/a/52821230/143475

@stutipai
Copy link

There seems to be a little confusion on what karate.properties[] fetches. Does it pick the values for variables only when supplied from the mvn command or is it capable of recognising the environment variables from Gitlab storage?

@ptrthomas
Copy link
Member

environment. I am locking this thread

@karatelabs karatelabs locked and limited conversation to collaborators Jul 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants