在空手道DSL测试中使用环境变量 [英] Using environment variables in Karate DSL testing
问题描述
我想将GitLab CI
并入我的Karate
测试中.我想用不同的用户名和密码循环测试,以确保我们的API端点正确响应不同的用户.
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.
考虑到这一点,我希望能够将用户名和密码作为安全的环境变量存储在GitLab
中(而不是以纯文本形式存储在karate-config
中),并根据需要将Karate
拉出它们从karate-config
或功能文件中获取.
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.
浏览文档和StackOverflow问题,我还没有看到执行此操作的示例.
Looking through the docs and StackOverflow questions, I haven't seen an example where it's being done.
更新新信息
关于以下Peter的评论,这是我需要的,我尝试将其设置如下:
In regards to Peter's comment below, which is what I need I am trying to set it up as follows:
在karate-config中设置客户端ID:
var client_id = java.lang.System.getenv('client_id');
set client id in karate-config:
var client_id = java.lang.System.getenv('client_id');
:
clientId: client_id
在我的功能文件中尝试访问它:
* def client_id = clientId
In my feature file tried to access it:
* def client_id = clientId
不幸的是,它仍然为null.
It still comes through as null, unfortunately.
推荐答案
您可以使用karate.properties在空手道中读取环境变量,
You can read environment variables in karate using karate.properties,
例如
karate.properties['java.home']
如果这有助于您阅读在gitlab上安全保存的环境变量,则可以在karate-config中使用它进行身份验证.
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.
但是如果用户过多,您的config和environment变量将显得很麻烦.
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,
使用彼得建议的Java互操作:
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.
这篇关于在空手道DSL测试中使用环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!