英文:
How to pass a variable from config.js file to the java method in the feature file
问题
I have written a Java method which will accept certain args like secret key, pem file etc and return a Token. when I call this method in my feature file and provide the 'secret key' and 'pem file location' it returns me a token which I use in the headers to pass the authentication. This scenarios work perfect.
Now the problem is we have different secret key and pem file for diff enviornment, is there a way where I can provide these variables in the config.js and use them in feature file while calling the Java method?
Feature file snippet below
-
def token = Java.type('path.to.my.javaclass').javaMethod('secret_key', 'pemfilelocation')
-
def req_headers = {Authorization: '#("Bearer " + token)'
This above is working perfectly fine.
Now if I use below config.js
function fn() {
var env = karate.env;
var config = {
env: env,
secretkey: 'secretkey',
pemfileloc: 'pemfileloc'
}
if (!env) {
env = 'dev';
}
if (env == 'dev') {
config.secretkey = 'ewqewwqew'
config.pemfileloc = 'src/test/java/location';
but this below is not working and the token generated is not correct.
Background:
-
def secret = secretkey
-
def pemfile = pemfileloc
-
def token = Java.type('path.to.my.javaclass').javaMethod('#(secret)', '#(pemfile)')
-
def req_headers = {Authorization: '#("Bearer " + token)'
can anyone please help how to pass the variable from the config.js to the java methods?
英文:
I have written a Java method which will accept certain args like secret key, pem file etc and return a Token. when I call this method in my feature file and provide the 'secret key' and 'pem file location' it returns me a token which I use in the headers to pass the authentication. This scenarios work perfect.
Now the problem is we have different secret key and pem file for diff enviornment, is there a way where I can provide these variables in the config.js and use them in feature file while calling the Java method?
Feature file snippet below
-
def token = Java.type('path.to.my.javaclass').javaMethod('secret_key', 'pemfilelocation')
-
def req_headers = {Authorization: '#("Bearer " + token)'
This above is working perfectly fine.
Now if I use below config.js
function fn() {
var env = karate.env;
var config = {
env: env,
secretkey: 'secretkey',
pemfileloc: 'pemfileloc'
}
if (!env) {
env = 'dev';
}
if (env == 'dev') {
config.secretkey = 'ewqewwqew'
config.pemfileloc = 'src/test/java/location'
but this below is not working and the token generated is not correct.
Background:
-
def secret = secretkey
-
def pemfile = pemfileloc
-
def token = Java.type('path.to.my.javaclass').javaMethod('#(secret)', #'(pemfile)')
-
def req_headers = {Authorization: '#("Bearer " + token)'
can anyone please help how to pass the variable from the config.js to the java methods?
答案1
得分: 0
仅进行以下更改:
- def token = Java.type('path.to.my.javaclass').javaMethod(secret, pemfile)
变量就是这么简单。只需记住,每当在Karate中发生JS(通常在圆括号内),您可以“正常”使用变量。
还请阅读这个链接:https://github.com/karatelabs/karate#rules-for-embedded-expressions
英文:
Just make this one change:
* def token = Java.type('path.to.my.javaclass').javaMethod(secret, pemfile)
Variables are that simple. Just keep in mind that whenever JS is happening in Karate (typically within round brackets) you can use variables "normally".
Also read this: https://github.com/karatelabs/karate#rules-for-embedded-expressions
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论