Reputation: 87
My requirement:
Say, I have set the dbusername and dbpassword in karate-config.js, how can I call dbusername in a feature file and assign it to a variable?
I see some way as follows
# if the js file evaluates to a function, it can be re-used later using the 'call' keyword
* def someFunction = read('classpath:some-reusable-code.js')
If I have declared some-reusable-code.js as follows,
function fn() {
var env = karate.env; // get system property 'karate.env'
var myBaseUrl = 'https://reqres.in/api/users/2'
var dbusername = 'myDBUserName'
var dbpassword = 'myDBPassword'
karate.log('karate.env system property was:', env);
if (!env) {
env = 'dev';
}
var config = {
env: env,
myVarName: 'someValue'
myUrl: 'http://reqres.in/users'
}
if (env == 'dev') {
// customize
// e.g. config.foo = 'bar';
} else if (env == 'e2e') {
// customize
}
return config;
}
But when I read the entire, 'some-reusable-code.js', how could I assign 'dbusername' or 'myNarName' or 'myUrl' to myVar as a local variable in myuseful.feature?
Will it be possible to give a working example please?
Upvotes: 1
Views: 161
Reputation: 58153
Please read the docs: https://github.com/intuit/karate#configuration
You are missing the part where you return dbusername
as part of the config
returned when the function exits. Then you don't need to do anything extra in your feature, dbusername
will be available as a (global) variable. Try this hard coded karate-config.js
and understand it:
function fn() {
def config = {};
config.dbusername = 'myDBUserName';
config.dbpassword = 'myDBPassword';
return config;
}
now in your feature:
* print 'dbusername:', dbusername
You can find a working example here: https://github.com/intuit/karate/tree/master/karate-demo
Upvotes: 1