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

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.

Related

Flowable - concat dynamic variable and a string in http task (url)

I need to convert the base url according to the production and other environments.
I am using script task before a http task to perform this logic.
baseUrl = http://localhost:8080
baseUrl, is the output of the script task. Now I need to add this base url as a prefix in http task url
Url = ${baseUrl}/application/find (something like this).
I am getting the following issue
Unknown Property used in the expression ${baseUrl}/application/find
Script
var env = execution.getVariable("env")
if(env == "prod") {
var baseUrl = "http://localhost:8080";
execution.setVariable("baseUrl", baseUrl);
}
Please assist.
This typically means that it is unable to find a property in the expression (as the message says). The only expression you are using is baseUrl which means that the issue is around the baseUrl. The concatenation as you have done it is correct and doesn't need to have an adaption.
You should check if the variable really exists, this you can do by introducing a wait state before your HTTP task and check afterwards if the variable is created. Rather than using outputs, you can also use the Java API in your script task to create the variable:
execution.setVariable("baseUrl", "http://localhost:8080");
Assuming you are using Spring Boot, for your specific use-case it would be also an option to use the application.properties to specify your base-url and then refer to the baseUrl with the following expression:
${environment.getProperty("baseUrl")}/application/find
This will allow you to change the baseUrl independent of your process definition.

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.

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

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']