Karate after Scenario not running [duplicate] - testing

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

How to pass variables in read while calling other feature files in Karate? [duplicate]

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.

Karate gatling name resolver not working properly for GET call [duplicate]

This question already has answers here:
Is there an option for us to customize and group the test scenarios in the statics section of the Karate Gatling Report?
(2 answers)
Closed 1 year ago.
My name resolver isn't working for some reason.
I want to group all the calls together as a single entity in results but the result is showing it as separate call.
Issue in having them separately is I am having 500,000 such request and when gatling tries to generate report its giving heap dump memory issues.
Below is my feature file
Given path '/hometown/process?requestId='+__gatling.ID+'&includeSystemComments=true'
And header karate-name = 'Request History'
When method GET
Then status 200
I've also used
val protocol = karateProtocol("/hometown/process?requestId={requestID}&includeSystemComments=true" -> Nil)
protocol.nameResolver = (req, ctx) => req.getHeader("karate-name")
Still in the results I am getting all of them as separate Rest calls.
I wanted these calls in results to be grouped as single entity.
Below is what I have the results as:
Any help would be appreciable.
Actually I think you don't even need a name resolver. Just try this:
val protocol = karateProtocol(
"/hometown/process" -> Nil
)
Or also try without anything, e.g. val protocol = karateProtocol().
Else maybe there's some other call that is "escaping". No one will be able to tell from your question, so please follow this process: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue

Karate afterScenario global hook [duplicate]

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

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

How do I configure and use/switch between configuration environments using Karate [duplicate]

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