在空手道DSL测试中使用环境变量 [英] Using environment variables in Karate DSL testing

查看:86
本文介绍了在空手道DSL测试中使用环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆