How to call a particular javascript function from

2019-02-20 12:36发布

问题:

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()

回答1:

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



标签: karate