I have a scenario where the field on which I am putting the condition is absent in the response. Eventually, after some retry attempts, that field appears in the response and the condition is satisfied. Still, the test case is marked as failed with the initial reason that the field is undefined.
Asked
Active
Viewed 38 times
1
-
never had this reported before. I think the only way to make any progress on this is to follow this process: https://github.com/karatelabs/karate/wiki/How-to-Submit-an-Issue – Peter Thomas May 23 '23 at 11:25
-
Thanks, Peter. I will submit the issue with the details – Deepak Rai May 23 '23 at 15:20
1 Answers
1
I think you may be able to proceed if you write better JS logic. By the way, if zip_local
is a string, you can avoid the toString()
. So try this:
* def isValid =
"""
function() {
let zipLocal = karate.get('response.address.zip_local');
return zipLocal ? zipLocal.includes('4521') : false;
}
"""
# ...
* retry until isValid()
Also refer this answer: https://stackoverflow.com/a/55823180/143475
Take the help of someone good at JavaScript if needed.

Peter Thomas
- 54,465
- 21
- 84
- 248
-
1Thanks for your guidance. I just tweaked your function a little and it worked. I needed to check the variable is not undefined before my validation. * def isValid = """ function(x,y) { if(typeof x !== "undefined") return x.contains(y) } """ – Deepak Rai May 24 '23 at 08:24
-
@DeepakRai great, `karate.get()` should normally take care of undefined etc. but ok. please accept the answer – Peter Thomas May 24 '23 at 10:50