Pass additional parameters to karate-config.js via command line via Maven - karate

I have additional settings that I need to pass to Karate when running via Maven that will be available in karate-config.js. Currently I can pass in a string using the karate.env property - is it necessary to encode my parameters as a JSON object and pass them in via this one property or can I do something like:
mvn test -DargLine="-Dkarate.env='production' -Dkarate.clientid='1234567890' ...
Such that I can then reference karate.clientid in karate-config.js where I can save it into the returned config object.
I'm sure I'm missing something obvious here...

Yes ! Refer to the documentation for karate.properties['karate.clientid'].

I've found a way, but I didn't use examples. What I've done:
In Gradle:
task api(type:Test) {
systemProperty "karate.clientId", System.properties.getProperty("karate.clientId")...
}
In karate-config.js (in var config):
clientId: karate.properties['karate.clientId'] || 'xyz'
In the command line:
'-Dkarate.clientId=abc'
If I don't set the clientId in my command line, the clientId will be 'xyz'.

You can pass the parameters like this
mvn test -D clientId=123 -D baseurl=test.com
and refer them in the karate-config.js as
karate.properties['clientId'] and karate.properties['baseurl']

Related

Environment switching is not working as documented in karate 1.0.0 [duplicate]

I want to want pass multiple arguments in mvn command and and that should be read in karate-config.js file.
e.g: mvn test -DargLine="-Dkarate.env='gdcStaging', -Dkarate.source='false', -Dkarate.natco='gdc'"
I didn't want to declare any .prop files, want to get these values at run time.
Below prop. are defined to read these arguments but unable to achieve from this:
var environment = karate.env;
var natco = karate.properties['karate.natco'];
var isLocal = java.lang.System.getenv('karate.source');
I need help to achieve this
karate-version=0.9.0
I had also referred to this link :Pass additional parameters to karate-config.js via command line via Maven
but not worked
Instead of using argLine try passing it directly,
mvn test -Dkarate.env=gdcStaging -Dkarate.source=false -Dkarate.natco=gdc
I suggest not to use karate. as a prefix to your arguments other
than karate.env, instead try using your application name.
eg,
-Dmyapp.source=false
coming to the karate-config.js
var natco = karate.properties['myapp.source']
This should work.

Not able to use variable defined in karate-config.js in my feature file

I want to set a Global variable for Base Test Data location which can be used in all of my feature files.
In karate-config.js i have made below changes -
var config = {
env: env,
INPUT_JSON_PATH: 'com/company/project/module/TestData'
}
And in my feature file I am trying to use it as -
Given path '/myService'
And request read('classname:INPUT_JSON_PATH/Exception_Handling/Mandatory_Fields_missing.json')
When method POST
Then status 400
But somehow its not getting resolved and I am getting error -
could not find or read file: classname:INPUT_JSON_PATH/Exception_Handling/Mandatory_Fields_missing.json
Any idea, what am i missing here?
Thanks,
Abhi
Just keep in mind that read() and other parts of the right-hand-side are treated as ordinary JavaScript. Maybe you were intending to do this:
And request read('classpath:' + INPUT_JSON_PATH + '/Exception_Handling/Mandatory_Fields_missing.json')

Issue with reading gradle command line params in karate-config.js [duplicate]

This question already has an answer here:
Using environment variables in Karate DSL testing
(1 answer)
Closed 2 years ago.
I am trying to pass some cmd line args from gradle to be used in karate-config.js.
Cmd: ./gradlew test -Denv=qa -Dmodule=payments
I looked at https://github.com/intuit/karate#command-line and followed similar steps and put this in build.gradle:
test {
...
systemProperty "karate.env", System.properties.getProperty("env")
systemProperty "karate.module", System.properties.getProperty("module")
}
Now in karate-config.js, I have code like below:
var environmentvar = karate.env;
var modulevar = karate.module;
environment var (karate.env) variable gets the correct value, but module var (karate.module) always shows as undefined. Any pointers on how to fix this?
Karate 0.9.4
JDK 1.8.0_231
Aren't you missing a karate. for e.g.:
System.properties.getProperty("karate.env")
Just didn't regcognized the important information that accessing karate.env works.
The environment variable karate.env is treated special. Using karate object to access other system properties the same way does not work.
You should read accessing system properties.
Solution: Use karate.properties['prop.name'] to access your module system variable.
In your case:
var environmentvar = karate.env;
var modulevar = karate.properties['module'];

karate| env specific config file

karate framework| I am trying to create multiple karate-config.js file for different env like 'test', 'stage' can somebody help me with an example how I can call env specific config values from different karate config file.
I refer this https://github.com/intuit/karate/blob/master/karate-demo/src/test/java/karate-config-contract.js
but doesn't clarify what exactly need to do for calling different config.
This part of the karate documentation explains how to check the karate.env property in your karate-config.js in order to set configurations and variables depending on your environment.
An other way to archive different configurations per environment is explained here: environment-specific-config
All these approaches solves the issue, to configure different urls for example in your test cases.
Hence, there is no need to call or check for the environment in your feature file in order to get different configuration values. It's done by karate. Just refer to the variables you assigned in karate-config.js.
You just do something like:
Background:
* url baseUrls.userSystem
Where your karate-config.js could look like:
function fn() {
if (!env) {
env = 'local';
}
var config = {
baseUrls : {
userSystem : "http://localhost"
}
}
if (env === 'dev') {
config.baseUrls.userSystem = "http://usersystem.default.svc"
}
return config
}
The approach above demonstrate how to use just one karate-config.js for all enviroments. One file for all.
If you want to create a karate-config-<env>.js per enviroment, follow the environment-specific-config documentation.
You will find here https://github.com/intuit/karate/tree/master/karate-demo/src/test/java a default karate-config.js that will be evaluated for every environment. The karate-config-contract.js will only be evaluated after the karate-config.js file has been evaluated if and only if the karate.env property is contract.
Please read the karate documentation. Peter did a great job to document nearly every little feature karate offers.

I want to pass multiple arguments in karate-config.js through mvn command

I want to want pass multiple arguments in mvn command and and that should be read in karate-config.js file.
e.g: mvn test -DargLine="-Dkarate.env='gdcStaging', -Dkarate.source='false', -Dkarate.natco='gdc'"
I didn't want to declare any .prop files, want to get these values at run time.
Below prop. are defined to read these arguments but unable to achieve from this:
var environment = karate.env;
var natco = karate.properties['karate.natco'];
var isLocal = java.lang.System.getenv('karate.source');
I need help to achieve this
karate-version=0.9.0
I had also referred to this link :Pass additional parameters to karate-config.js via command line via Maven
but not worked
Instead of using argLine try passing it directly,
mvn test -Dkarate.env=gdcStaging -Dkarate.source=false -Dkarate.natco=gdc
I suggest not to use karate. as a prefix to your arguments other
than karate.env, instead try using your application name.
eg,
-Dmyapp.source=false
coming to the karate-config.js
var natco = karate.properties['myapp.source']
This should work.