将变量从config.js文件传递到特性文件中的Java方法的方法是什么?

huangapple go评论69阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年3月15日 20:28:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744694.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定