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'];
Related
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.
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.
This question already has an answer here:
How to create global variable in karate? [duplicate]
(1 answer)
Closed 1 year ago.
I'm trying to configure different testing targets via karate-config-<env>.js files located in the same directory.
When I try to execute the tests against the different target-systems:
mvn test -Dkarate.env=int02 (tried: -DargLine="-Dkarate.env=int02")
the karate-config-int02.js file is not executed and the test execution gets stuck somewhere.
I've read the documentation, but for now I found no working example.
I am working with karate 0.9.4 on macOS with Java 1.8 in a maven 3.6.0 example-project for a prof of concept.
Extending the pom file, as shown below, was also not working:
<properties>
<karate.env>int02</karate.env>
</properties>
I though that via the -Dkarate.env=int02 I could ensure that the karate-config-int02.js would be used to configure the instance specific properties I need.
I do have a line in both karate-config files like:
karate.log('karate-config|karate-config-int02 is called')
but I always see:
karate-config is called
The simplest way and how 90% of projects do it is with just the one karate-config.js and then some if else JS logic as per the docs. Maybe you can stick to that.
var env = karate.env || 'dev';
var config = { someUrlBase: 'https://localhost:8080/' };
if (env == 'stage') {
// over-ride only those that need to be
config.someUrlBase = 'https://stage-host/v1/auth';
} else if (env == 'e2e') {
config.someUrlBase = 'https://e2e-host/v1/auth';
}
return config;
Else please follow this process so we can figure out what could be wrong: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue
I found in the karate docs that the java method can be run like this:
* def JavaDemo = Java.type('com.app.DBUtils').prepareData(arg1, arg2)
I created karate-config.js file where I stored environment variables. Now I need to run some java methods after every scenario, but just for some environments. So I have there some conditions.
But I didn't find a way to run a java method from karate-config.js after every scenario. Is it possible?
Yes if you wrap it in JS or a Feature: https://github.com/intuit/karate#hooks
var fun = function(){ var MyClass = Java.type('com.myco.MyClass'); MyClass.doWork() }
karate.configure('afterScenario', fun);
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']