Reputation: 55
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?
Upvotes: 2
Views: 1026
Reputation: 58058
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
Upvotes: 0