Raphael De Lio
Raphael De Lio

Reputation: 45

Fuzzy Match in Conditional Karate API Testing Tool

We recently started using Karate as our integrated testing tool on the project we're currently developing and I've faced an issue recently which I'd like to know why is happening. Let's go through this:

One of the tests we do in all our APIs is the response time. When we started creating our tests we've created a series of common features that would be used for many different APIs tests. One of these features is the testGetAll.feature in which we send as parameters an endpoint, an optional list of paramenters, an authentication key and an optional response time.

Before we call the service, we have this code:

* def rTime = (__arg.rTime == '#notnull' ? __arg.rTime : MEDIUM_RESPONSE_TIME)

And then, to test if it was working, I've written the following:

* print "argRtime : " + __arg.rTime
* print __arg.rTime == '#notnull' 
* print "rTime : " + rTime 

And as result Ive got: argRtime = 3000 false rTime = 500

Why is this conditional false if __arg.rTime is not null?

Upvotes: 3

Views: 1530

Answers (2)

Alex D
Alex D

Reputation: 410

You can use fuzzy matchers on this line with karate.match from the karate object like this:

* def rTime = karate.match(__arg.rTime, '#notnull').pass ? __arg.rTime : MEDIUM_RESPONSE_TIME)

Upvotes: 2

Babu Sekaran
Babu Sekaran

Reputation: 4239

I suppose all the karate inbuilt fuzzy matching markers only work with match.

__arg.rTime == '#notnull'

is a simple javascript evaluation, not karate match, so here RHS will be considered as a string and evaluated.

However,

* match __arg.rTime == '#notnull'

will work perfectly.

But for your logic you can try,

* def rTime = ( __arg.rTime != null ? __arg.rTime : MEDIUM_RESPONSE_TIME)

Karate fuzzy matching marker should be only used with match

Upvotes: 3

Related Questions