Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ternary conditional logic in Karate with undefined variable

Tags:

nashorn

karate

I have a Karate feature file, let's called it A.feature, that is intended to be re-used by other feature files. By using shared scope, A.feature can use some variables, for instance the country, defined in the calling feature file. I want these parameters to be optional, but with a default value defined in A.feature. To do that I'm using ternary conditional logic, for instance:

* def myCountry = (country ? country : 'us')

However when country is not defined, a

ReferenceError: "country" is not defined

is thrown.

Does anybody have any idea how to resolve that, or if there is a Nashorn or Karate bug ?

If you want the complete stacktrace let me know.

like image 462
Lukas Cardot Avatar asked Jan 20 '26 14:01

Lukas Cardot


2 Answers

This will work:

* def country = typeof country == 'undefined' ? 'us' : country

EDIT - Karate now has a convenient API to do this:

* def country = karate.get('country', 'us')
like image 151
Peter Thomas Avatar answered Jan 23 '26 20:01

Peter Thomas


A simpler way is to use default values:

* def country = karate.get('country', 'us')
like image 44
Karl Molina Avatar answered Jan 23 '26 19:01

Karl Molina