How to call Karate-defined function from Javascript

Viewed 38

The scenario looks like this: Assume we have a helloworld.feature file that contains a scenario of just * print "Hello World!"

Then in another feature we do:

Scenario: call karate function from js
  * def foo = read('helloworld.feature')
  * def bar = 
  """
  function(){ foo() }
  """

  * bar()
  # or even * call bar

This will throw an exception. I've also tried karate.get("foo") to get the variable from karate scope and then try to call the result, same issue.

* foo() works fine, however.

Is this possible? Why or why not?

The upswing seems to be that, if you've defined a "feature function" in karate (i.e. using def x = read()), and you want to call the same feature in JS, you have to re-define the function again in JS.

1 Answers

When a feature is loaded, it is NOT JS, and the only thing you can do is call it.

* def foo = read('helloworld.feature')
* call foo

If you want to use conditional logic (or mix this into JS functions), note that karate.call() will also work:

* karate.call('helloworld.feature')
Related