Cookies aren't applying from karate-config.js - karate

I have faced a problem with setting global cookies from karate-config.js file.
I have two features:
login.feature as helpers
postItem.feature as test, which is also using cookieKey in path.
In login.feature I get this cookieKey like this:
* def cookieKey = responseCookies.user_cookie_key.value
To make postItem, I need to call login.feature, which contains credentials and cookieKey as part of path, to get cookies, which are using for postItem.
If I call login.feature inside postItem.feature in Background section, everything works fine.
But I want to move calling login.feature to karate-config.js because I dont want to put this to each future features.
And what I did in karate-config.js:
var response = karate.call('classpath:/helpers/features/login.feature');
var cookieKey = response.cookieKey;
karate.set('cookieKey', cookieKey);
I receive cookieKey and able to re-use it in my feaure postItem like this:
* def cookieKey = karate.get('cookieKey')
But problem in the next, cookies from login.feature, which is calling from karate-config.js is not applying for postItem because call is covered in variable, but even without variable, they aren't applying.
Could you help me to handle this?

Maybe you just need to use configure cookies: https://github.com/karatelabs/karate#configure
I also think if you use the alternate method signature of karate.call() it will work: https://github.com/karatelabs/karate#call-vs-read
var response = karate.call(true, 'classpath:/helpers/features/login.feature');

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.

Could I get some help on changing auth tokens mid test? [duplicate]

In my framework I have headers.js file. I am calling that js file on background of every feature file using the command * configure headers = read('headers.js'). This working as expected, some scenarios I need to change the 'client-id' value. For example headers.js has a client-id=xyz, I need to change client-id=abc for couple of scenarios rest all are using client-id as xyz (my headers have 20 different values, I don't want to write that in require feature files) Is there any way to modify one value from the headers.js file and use that as a header for the request?
The configured headers always is the last to be applied. So the best solution for you is to create a second headers-xyz.js and for only those needed scenarios do * configure headers = read('headers-xyz.js').
It is up to you to refactor the js code so that you can re-use most of it. For example, this might work:
function() {
var fun = karate.read('headers.js');
var res = fun();
res['client-id'] = 'xyz';
return res;
}

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

How to edit configured headers in karate framework

In my framework I have headers.js file. I am calling that js file on background of every feature file using the command * configure headers = read('headers.js'). This working as expected, some scenarios I need to change the 'client-id' value. For example headers.js has a client-id=xyz, I need to change client-id=abc for couple of scenarios rest all are using client-id as xyz (my headers have 20 different values, I don't want to write that in require feature files) Is there any way to modify one value from the headers.js file and use that as a header for the request?
The configured headers always is the last to be applied. So the best solution for you is to create a second headers-xyz.js and for only those needed scenarios do * configure headers = read('headers-xyz.js').
It is up to you to refactor the js code so that you can re-use most of it. For example, this might work:
function() {
var fun = karate.read('headers.js');
var res = fun();
res['client-id'] = 'xyz';
return res;
}

Using result from the first request and passing in the second request- POSTMAN

I want to use the result which is "id" and use it in the second request POST
Saving it as environmental variable in the first request(under tests)
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("id", jsonData.token);
Here is the second request looks like, not sure what I am doing wrong here
Your request is correct.
As long as you set an environment variable, you may USE this environment. From your print screen I see "no environment" on the top right corner.
I suggest either you create an environment where to save your environment variables and then use it or you use a global variable, postman.setGlobalVariable("id", ...) instead
Alex