This question already has answers here:
Karate: Is there a http-request hook in karate, that gets called automatically after every API call, and whose behaviour I can modify? [duplicate]
(2 answers)
Closed 1 year ago.
Is it possible to configure afterScenario hook globally?
I can configure per .feature file like this:
Background:
* url baseMhsServiceUrl
* configure afterScenario =
"""
function(){
var uri = karate.prevRequest.url;
karate.log('Request was: ' + uri);
}
"""
But, I tried this in the karate.config.js and it is not working at all.
karate.configure('afterScenario', "function(){var uri = karate.prevRequest.url;\nkarate.log('Request was: ' + uri);}");
Could not find such an example in the docs.
No, do consider contributing code.
But you have the option of writing a RuntimeHook: https://github.com/intuit/karate/wiki/1.0-upgrade-guide#hooks
Related
This question already has an answer here:
how to dynamically set an value in json read from file in Karate
(1 answer)
Closed 1 year ago.
I have my request body in a *.json file and same *.json request body with different parameters is used in multiple test scenarios. How to pass variables in this scenarios.
I tried below
* def requestBody = call read ('successScenario.json') { id: 'U123', age: 23, date: currentDate }
And request requestBody
Please note I can't use examples as one of my variable is currentDate function so how to pass currentDate variable via examples.
This is not working. If I don't pass parameters here it works fine. I don't want to define these parameters in karate-config.js file as it is scenario dependent variables.
When I use above in *.js file it works fine but the response body is JSON i can't make a *.js file for that.
Please note that there is no such thing as call read('some.json'). In other words, JSON is not something you can call.
Please take some time to read this part of the docs: https://github.com/karatelabs/karate#call-vs-read
If you want to "data drive" a JSON file, use "embedded expressions".
This is explained well here: https://stackoverflow.com/a/50093102/143475
This question already has an answer here:
Is it possible to evaluate a variable inside a call read() in karate? [duplicate]
(1 answer)
Closed 1 year ago.
I have a requirement like this -
feature1 calls the featurebase file.
In featurebase file, I am reading a json.
I want to configure the file reading via featurefile1.
I have my scripts like this.
FeatureBase -
Background:
* url baseUrl
* def requestBody = read('#(file)')
Feature1 -
Background:
* url baseUrl
* def callFeature = call read('FeatureBase.feature') { file: 'json1.json'}
This code doesn't work and gives me file not found error.
The '#(foo)' trick applies only to JSON. Please read this: https://github.com/intuit/karate#rules-for-embedded-expressions
Please change your code to:
* def requestBody = read(file)
Yes it is that simple, just like plain JavaScript.
This question already has answers here:
Karate: Is there a http-request hook in karate, that gets called automatically after every API call, and whose behaviour I can modify? [duplicate]
(2 answers)
Closed 1 year ago.
Is it possible to configure afterScenario hook globally?
I can configure per .feature file like this:
Background:
* url baseMhsServiceUrl
* configure afterScenario =
"""
function(){
var uri = karate.prevRequest.url;
karate.log('Request was: ' + uri);
}
"""
But, I tried this in the karate.config.js and it is not working at all.
karate.configure('afterScenario', "function(){var uri = karate.prevRequest.url;\nkarate.log('Request was: ' + uri);}");
Could not find such an example in the docs.
No, do consider contributing code.
But you have the option of writing a RuntimeHook: https://github.com/intuit/karate/wiki/1.0-upgrade-guide#hooks
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'];
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