Search code examples
nashornkarate

Ternary conditional logic in Karate with undefined variable


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.


Solution

  • 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')