在空手道 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.
更新新信息
关于下面彼得的评论,这是我需要的,我正在尝试将其设置如下:
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.
但是如果你有太多用户,你的配置和环境变量会显得很麻烦.
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,
按照 peter 的建议使用 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屋!