5

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.

drew2
  • 87
  • 1
  • 1
  • 9

1 Answers1

7

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.

Babu Sekaran
  • 4,129
  • 1
  • 9
  • 20
  • 1
    Babu: I think this is what the OP is looking for: https://github.com/intuit/karate/issues/547#issuecomment-427017681 – Peter Thomas Oct 15 '18 at 16:57
  • 1
    @PeterThomas That is exactly what I'm looking for. I've updated my question with what I'm trying and not succeeding with. – drew2 Oct 15 '18 at 17:18
  • @drew2 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: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue – Peter Thomas Oct 15 '18 at 17:24
  • 1
    @drew2 `-Dsome.name=foo` on the command line, and `karate.properties['some.name']` to get the value in the `karate-config.js` – Peter Thomas Oct 15 '18 at 17:29
  • Peter: How did I forget interop :P, yeah passing from the command line would be the best option. – Babu Sekaran Oct 15 '18 at 17:38
  • 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. – drew2 Oct 15 '18 at 17:40