Using environment variables in Karate DSL testing

瘦欲@ 提交于 2021-02-07 08:55:31

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/52820409/using-environment-variables-in-karate-dsl-testing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!