virendra chaudhary
virendra chaudhary

Reputation: 179

How to call a particular javascript function from .js file in karate feature file

Suppose I have saved followings functions in a Utility js file.

function getCurrentDate(){
return 'date';
}

function getMonth(){

return 'Oct';
}

Please help me how any of these methods can be accessed in feature file.

I tried following code but it is not working.

* def fun = call read('Utility.js')

* def result = getData()
or
* def result = fun.getData()

Upvotes: 2

Views: 13551

Answers (2)

girishKumarTallam
girishKumarTallam

Reputation: 1

* def data = karate.read('classpath:<path>/getRandomString.js')(<inputToTheFunctionIfAny>);

The above code works.

Upvotes: 0

Peter Thomas
Peter Thomas

Reputation: 58153

In Karate, a JS file can contain only one function and it does not need a name, take a closer look at the examples.

I don't really recommend combining multiple functions into one file, it just makes things much harder to maintain. But if you really insist, here's how:

function() {
  return {
    getCurrentDate: function(){ return 'date' },
    getMonth: function(){ return 'month' }
  }
}

EDIT: a much better answer is here: https://stackoverflow.com/a/49384760/143475

Upvotes: 6

Related Questions