Skip to content
Advertisement

Karate framework – How to check conditional OR in match contains response

I am trying to use a match contains to verify my schema response and data types and sometimes it returns a null and sometimes it will return a string, as an example. I am trying the following but I am getting the assertion failed because it did not evaluate to true.

I am trying the following:

JavaScript

The data returned for “firstName” for example is “firstName”:null,

Prior to the match each I am sending this:

JavaScript

I am not defining the schema, I have yet to figure out how to do this so I am not sure if that is my issue. I know this is probably how I should do this but I’m still learning.

Thanks for any help.

Advertisement

Answer

Ok I see one problem here:

JavaScript

That’s not going to work. The part that follows the #? has to be a valid JavaScript expression. This may be hard to understand if you are not familiar with JS but #string and friends are specific to the Karate match keyword and not something that works in JavaScript.

So what the above line is doing is checking if the value of firstName is equal to the literal string #string.

Fortunately Karate has a solution: (refer doc for ‘fuzzy matching‘):

JavaScript

Which is a convenience, the double ## means ‘optional’ that will match for both a string or null.

EDIT: for advanced cases, please read this: https://stackoverflow.com/a/50350442/143475

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement