Removing quotes from param values in karate request - karate

Scenario: I get response from a karate request which is in the form as below:
idValues = ["123:ABCD-F", "345:CFGB-F", "678:DERF-F"]
I then need to make a further request in a scenario where the values from array above is passed in karate's request * param as below:
Given path 'users','details'
* param = 123:ABCD-F, 345:CFGB-F, 678:DERF-F
When method get
Then status 200
Issue: I need to send this list of ids as comma separated values without the " quotes to the param keyword. If the values are passed to params with quotes, karate further encloses the ids within quotes (which is the correct behaviour i think) and because of this i do not get the expected response.
Wanted to know if there is a way to:
1. Take the values out of the array, remove the quotes from start and end.
2. Pass these as comma separated values to `param' keyword (as shown in the example above).
Thanks

2 suggestions.
karate.forEach() can do any kind of conversion you want. Here is an example. If you find this too ugly, use Java (search the docs for Java Interop):
* def idValues = ["123:ABCD-F", "345:CFGB-F", "678:DERF-F"]
* def fun = function(x, i){ var s = karate.get('temp'); if (i) x = ',' + x; karate.set('temp', s + x) }
* def temp = ''
* karate.forEach(idValues, fun)
* print temp
param will always encode, and as you said - it is the right behavior. So use url and manually concatenate the path you need. Refer this answer for more:
https://stackoverflow.com/a/55357704/143475

Related

want to assert inner list in karate

I have this kind of response in my API and I want to check that any of the list doesn't contain duplicate values.
[["1100","1100"],["123456"],["123456"],["123456"],["123516","110011"],["123515","110010"],["123514","110009"],["123513","110008"]]
when I use * match response == karate.distinct(response) , it compares all the values and not the values within the inner list like below
[["1100","2200"],["123456"],["123516","110011"],["123515","110010"],["123514","110009"],["123513","110008"]]
I only want to check whether inner list doesn't contain duplicate values, regardless of outer list elements.
This is the parent question https://stackoverflow.com/a/71807872/3664382, but now I'm stuck here -
Use match each: https://github.com/karatelabs/karate#match-each
And combine it with a "self" validation: https://github.com/karatelabs/karate#self-validation-expressions
* def fun = function(x){ return karate.match(x, karate.distinct(x)).pass }
* match each response == '#? fun(_)'
I have no idea how people end up with these weird situations. Please read this and I hope it makes sense: https://stackoverflow.com/a/54126724/143475
For completeness, you should take some time to understand JsonPath. This below will "flatten" the entire response into a single array:
* def temp = $response[*][*]

Karate - How to get the value of a specific attribute based on multiple attribute values in the specifc json array [duplicate]

I'm attempting to use a variable in the RHS of an JsonPath filter expression in a Karate test, similar to this:
* def name = 'A Name'
* def ids = $response[?(#.name == '#(name)')].id
If I use the literal string 'A Name' in the RHS of the expression it works as expected.
I've tried various ways to get the variable to evaluate:
'<name>', "#(name)", etc.
I suspect it is because I'm mixing JsonPath parsing with Karate parsing perhaps?
Read this first: https://github.com/intuit/karate#rules-for-embedded-expressions
And what you are looking for is this: https://github.com/intuit/karate#jsonpath-filters
* def ids = karate.jsonPath(response, "$.kittens[?(#.name=='" + name + "')].id")

Karate: parametric json path expressions [duplicate]

I'm attempting to use a variable in the RHS of an JsonPath filter expression in a Karate test, similar to this:
* def name = 'A Name'
* def ids = $response[?(#.name == '#(name)')].id
If I use the literal string 'A Name' in the RHS of the expression it works as expected.
I've tried various ways to get the variable to evaluate:
'<name>', "#(name)", etc.
I suspect it is because I'm mixing JsonPath parsing with Karate parsing perhaps?
Read this first: https://github.com/intuit/karate#rules-for-embedded-expressions
And what you are looking for is this: https://github.com/intuit/karate#jsonpath-filters
* def ids = karate.jsonPath(response, "$.kittens[?(#.name=='" + name + "')].id")

Karate - Matching 2 JSON objects for values but applying regex to one element of objects

I have 2 json as follows:
a: [{"code":"00","name":"A","iconUrl":"https:env1.test.png"}, {"code":"01","name":"B"}]
b: [{"iconUrl":"https:env2.test.png", "code":"00","name":"A"}, {"code":"01","name":"B"}]
I want to compare the 2 json objects. I tried match contains only.
My test is failing due to mismatch of env1 and env2 in iconUrl. By any chance is there a way out to resolve it by applying regex for iconUrl and not affecting the validation for code and number?
There are many possible ways, here is one:
* def first = [{"code":"00","name":"A","iconUrl":"https:env1.test.png"}, {"code":"01","name":"B"}]
* def second = [{"iconUrl":"https:env2.test.png", "code":"00","name":"A"}, {"code":"01","name":"B"}]
* second[0].iconUrl = '#string'
* match first == second

Certain expressions to validate json don't work

I am trying to print the following:
* print response.requests[?(#.friendlyId == '#(ORID)')]
where ORID is:
* def temp2 = response.teams[?(#.name == '<Name>')].requestedResources[0].resourceRequestFriendlyId
* def ORID = temp2[0]
The expression is giving value as null where as if i use JSON evaluator, I am getting the correct json.
Only JavaScript is supported on the right-hand-side of the print keyword. And JsonPath is not supported.
I will update the documentation to make this clear.