Vladyslav Ogol
Vladyslav Ogol

Reputation: 25

Karate: Scenario fails if contains __arg and run in 'stand-alone' mode

I have faced a problem when I try to run a Scenario containing built-in __arg variable as 'stand-alone' (not 'called'), then my test fails with an error (I do not @ignore the called one as in order to use it in both 'called' and 'stand-alone' modes):

evaluation (js) failed: __arg, javax.script.ScriptException: ReferenceError: "__arg" is not defined in <eval> at line number 1
stack trace: jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:470)

Following two simple features should be enough to reproduce.

called-standalone.feature:

Feature: Called + Stand-alone Scenario
  Scenario: Should not fail on __arg when run as stand-alone
    * def a = __arg
    * print a

caller.feature:

Feature: Caller
  Scenario: call without args
    When def res = call read('called-standalone.feature')
    Then match res.a == null

  Scenario: call with args
    When def res = call read('called-standalone.feature') {some: 42}
    Then match res.a == {some: 42}

Putting these two features into the skeleton project and run mvn test will show an error.

I'm expecting this should work as the docs say that "‘called’ Karate scripts ... can behave like ‘normal’ Karate tests in ‘stand-alone’ mode".

‘called’ Karate scripts don’t need to use any special keywords to ‘return’ data and can behave like ‘normal’ Karate tests in ‘stand-alone’ mode if needed

Upvotes: 1

Views: 1269

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58088

All Karate variables have to be "defined" at run time. This is a rule which can not be relaxed.

So you should re-design your scripts. The good thing is you can use karate.get() to set a "default value".

* def a = karate.get('__arg', null)

That should answer your question.

Upvotes: 2

Related Questions