1

I am trying to use karate.call to invoke function of a JS file receiving two arguments (String, Array of String). However the array of string would not be passed on to the JS file.

The JS file contains:

function(query, fragments) {
// Here lies some code
// One of them includes fragments.length;
}

And I call the JS function on another JS file in this way:

//var query = 'Some string';
//var fragments = ['fragment1', 'fragment2'];
var clean = karate.call('../helper/helper.js', [query, fragments]);

I am able to pass query which is a string. But I was unable to pass the array of string. The error says:

TypeError: Cannot read property "length" from undefined

It seems the array of string did not get passed to the JS function. Any help will be greatly appreciated. Thanks!

1
  • 2
    Your .call() only passes one parameter, that array you build from query and fragments. If you used .apply() instead of .call() that would make sense. Commented Jun 10, 2019 at 12:56

5 Answers 5

2

You can read you function first and invoke is just like any other js function

var myFun = karate.read('../helper/helper.js');
var funCall = myFun(query, fragments);

or

 var myCall = karate.read('../helper/helper.js')(query, fragments);

this should work.

Sign up to request clarification or add additional context in comments.

Comments

2

.call takes parameters as comma separated values , you need to use .apply if you want to pass values as an array.

var clean = karate.call('../helper/helper.js', query, fragments);

will work...

1 Comment

This does not work as karate complains javascript function call failed: TypeError: Can not invoke method [jdk.internal.dynalink.beans.OverloadedDynamicMethod Object com.intuit.karate.core.ScriptBridge.call(String,Object) Object com.intuit.karate.core.ScriptBridge.call(String) ] with the passed arguments; they do not match any of its method signatures.
1

The answers here are missing an important clarification:

I often do single arg functions like:

* def concatParams =
  """
  function(s) {
    return "urldt=" + todaysDate + "&caseid=" + s.caseid
  } 
  """

And I will call that like so:

* def params = call concatParams {caseid: '3433344'}

But, when I want to do 2 params, I will define a function like so:

* def concatParams =
  """
  function(d,s) {
    return "urldt=" + d.date + "&caseid=" + s.caseid
  } 
  """

And unintuitively, neither of these will work:

* def params = call concatParams {date: '01/01/2020', caseid: '3433344'}
* def params = call concatParams '01/01/2020' '3433344'

To get it to work, instead I call it like this:

* def params = concatParams('01/01/2020', '3433344')

Documentation does not clarify this.

2 Comments

you are wrong. the documentation DOES clarify this: github.com/intuit/karate#javascript-functions
Uhm. Your case is different than the one I asked here.Like Peter said, I have no problem passing params like regular JS functions when it is declared inside a feature
0
 var clean = karate.call('../helper/helper.js', query, fragments);

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call

1 Comment

This does not work as karate complains javascript function call failed: TypeError: Can not invoke method [jdk.internal.dynalink.beans.OverloadedDynamicMethod Object com.intuit.karate.core.ScriptBridge.call(String,Object) Object com.intuit.karate.core.ScriptBridge.call(String) ] with the passed arguments; they do not match any of its method signatures.
0

call method accepts comma separated params and apply accept array so you need to replace call into apply. your code looks like

function karate(query, fragments) {
// Here lies some code
// One of them includes fragments.length;
}

var clean = karate.apply('../helper/helper.js', [query, fragments]);

3 Comments

I don't think there was a method of karate.apply(), as it is not available in the documentation too. I tried your recommendation and it says karate.apply is not a function github.com/intuit/karate
@Raymond please give a name to the function, the code is updated.
everyone: please ignore this answer, once you define a JS function, you can slap parentheses and arguments onto it just like NORMAL JS. what's worse, this example re-defines the built-in karate JS helper object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.