How to do conditional check in karate response? - karate

Is there any way to check if object is empty then ok if not then check if it matches a fixed structure?
I mean, something like:
* def expectedDelayEntries = response.delayEntries == "{}" ? {} : '#(delayEntries)'

Yes. Refer to the docs: https://github.com/intuit/karate#conditional-logic
Do this in 2 steps.
* def expected = condition ? { foo: '#string' } : { bar: '#number' }
* match response == expected

Related

Replace Json key in Karate [duplicate]

This question already has an answer here:
Karate API function/keyword to substitute JSON placeholder key with argument passed
(1 answer)
Closed 1 year ago.
I need to send a Json to an endpoint but I need to replace a key with a variable.
I've this code
.....
* def idJson = response.id
Given path <mypath>
And headers {Authorization: '#(auth)'}
And request read('classpath:myjson.json')
.....
The myjson.json file is
{
"a":
{
...
"b":false,
"c":true
},
"d":
{
'#(idJson)':
{
"Key":[
.....
]
}
}
}
But the value is not replace in the json. When I perform the request I see that the json contains the string '#(idJson)' instead of the value of the variable. Any idea about how to solve this?
Thank you
Embedded expressions cannot be used to modify a JSON key. You have to use a JS operation like this:
* def idJson = 'foo'
* def body = {}
* body[idJson] = 'bar'
* url 'https://httpbin.org/anything'
* request body
* method post
* status 200
* match response.json == { foo: 'bar' }
Note that d could be replaced like this so you can still read the file and be dynamic:
{ "d": "#(temp)" }
And temp can be JSON that is defined in your feature before reading the file.
If this is too troublesome, please contribute code :)

Karate API : How to assert a json/response node value which returns null or an array object for different data

I've got a request that returns a response node(itemCanBe) value in two possible ways, depending on the 'itemNum'. How do I assert this ? below attempt is not working
* match res..itemCanBe == null || res..itemCanBe[*] contains ['Colgate']
itemCanBe returning null
{
"itemDetails": {
"1234": {
"itemNum": "1234",
"itemCanBe": null
}
}
}
itemCanBe returning array
{
"itemDetails": {
"4567": {
"itemNum": "4567",
"itemCanBe": [
"Colgate",
"Sensodine"
]
}
}
}
See this thread for details: https://github.com/intuit/karate/issues/1202#issuecomment-653632397
This should be good enough to solve your problem.
This will actually work:
* def temp = get[0] response..itemCanBe
* match temp == '##[]'
Also refer: https://stackoverflow.com/a/50350442/143475

Is there a way to match dynamic object keys?

I'm looking for a simple technique to match objects where the key may not be known in advance (e.g. we may fetch the schema as part of the test). As a contrived example:
Scenario:
* def result = { foo: 'bar' }
* def key = 'foo'
Then match result == { '#(key)': 'bar' }
...which doesn't currently work.
Once you realize there is a JavaScript engine behind the scenes, you will get even more ideas :)
* def result = { foo: 'bar' }
* def key = 'foo'
* def expected = {}
* expected[key] = 'bar'
Then match result == expected
Also do a search for other answers [karate] dynamic you will find many interesting examples such as this one: https://stackoverflow.com/a/57226061/143475

Karate doesn't check cases if expected number is a larger, how do I handle it?

I have given match response as #number
But for a value of 15547786385661 the case just gets skipped.how do I handle it?
You must be making some simple mistake. Try pasting the two lines below into a fresh scenario and see it work:
* def response = { foo: 15547786385661 }
* match response == { foo: '#number' }

Karate - How to validate output with multiple templates?

Is there any way to validate output of a request with one of multiple templates?
for example:
{
"pendingInvitesCount":#number,
"acceptedInvitesCount":#number,
"rejectedInvitesCount":#number
}
or
[]
Not really recommended as testing best practice, but this should give you some ideas:
* def actual = []
* def expected = (actual.size() == 0 ? '#[0]' : { a: '#number', b: '#number' })
* match actual == expected