Using environment variables in Karate DSL testing
Asked Answered
S

1

7

I'd like to incorporate GitLab CI into my Karate testing. I'd like to loop through my tests with different user names and passwords to ensure our API endpoints are responding correctly to different users.

With that in mind, I'd like to be able to store the usernames and passwords as secure environment variables in GitLab (rather than in the karate-config as plain text) and have Karate pull them as needed from either the karate-config or the feature files.

Looking through the docs and StackOverflow questions, I haven't seen an example where it's being done.

Updating with new information

In regards to Peter's comment below, which is what I need I am trying to set it up as follows:

set client id in karate-config:
var client_id = java.lang.System.getenv('client_id');

in the actual config object:
clientId: client_id

In my feature file tried to access it:
* def client_id = clientId

It still comes through as null, unfortunately.

Soviet answered 15/10, 2018 at 15:49 Comment(0)
P
9

You can read environment variables in karate using karate.properties,

eg,

karate.properties['java.home']

If this helps you to read the environment variables that you are keeping securely on your gitlab, then you can use it in your karate-config for authentication.

But your config and environment variable will look cumbersome if you are having too many users.

If you want to run a few features with multiple users, I would suggest you look into this post,

Can we loop feature files and execute using multiple login users in karate

EDIT:

Using java interop as suggested by peter:

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

to see which are all variables are actually exposed try,

var evars= java.lang.System.getenv();
karate.log(evars);

and see the list of all environment variables.

Psychogenesis answered 15/10, 2018 at 16:43 Comment(6)
Babu: I think this is what the OP is looking for: github.com/intuit/karate/issues/547#issuecomment-427017681Morris
@PeterThomas That is exactly what I'm looking for. I've updated my question with what I'm trying and not succeeding with.Soviet
@Soviet ok. I've tried it and it works, try the slightly more explicit way of using command-line arguments as per the answer. else please use this process: github.com/intuit/karate/wiki/How-to-Submit-an-IssueMorris
@Soviet -Dsome.name=foo on the command line, and karate.properties['some.name'] to get the value in the karate-config.jsMorris
Peter: How did I forget interop :P, yeah passing from the command line would be the best option.Psychogenesis
Thanks again @Peter Thomas and Babu, I had been using the mvn -Dclient_id="foo" approach before, but was trying to move away from it. It looks like the issue with getting the environment variables through getenv() is that they are set when the environment is stood up, so it may work alright in gitlab, but not locally.Soviet

© 2022 - 2024 — McMap. All rights reserved.