Karate: How to use a Javascript function from a DIFFERENT feature file

Viewed 8542

I have created a feature file that will contain lots of javascript functions.

From within a DIFFERENT feature file I want to use ONE of those functions (and pass in a value).

How do I do this please?

My feature file is called SystemSolentraCustomKarateMethods.feature

Here is the current content (it currently contains just one function):

Feature: System Solentra Status Test

Background:

* def checkreturneddatetimeiscorrect =

#The following code compares the passed in datetime with the current systemdatetime and
#makes sure they are within 2 seconds of each other

"""
function(datetime) {

var datenow = new Date();
karate.log("***The Date Now  = " + datenow.toISOString() + " ***");
var timenow = datenow.getTime();
karate.log("***The Time Now in Milliseconds  = " + timenow+ " ***");


karate.log("***The Passedin Date  = " + datetime + " ***");
var passedintime = new Date();
passedintime = Date.parse(datetime);
karate.log("***The Passed in Time = " + passedintime+ " ***");

var difference = timenow - passedintime;
karate.log("***The Time Difference = " + difference + " milliseconds ***");



return (difference < 2000)

}
"""
2 Answers

Thanks Peter I have figured out how to do this now.

(1) The feature file that contains the functions MUST have the Feature, Background and Scenario tags - even if your file does NOT contain any scenarios. (*see my example file below)

(2) In the feature file that you are calling FROM add the following code to the Background section:

* call read('yourfilename.feature')

(3) You can now use the functions within the called feature file

Here is the structure of the feature file I am calling:

Feature: Custom Karate Methods
This feature file contains Custom Karate Methods that can be called and used from other Feature Files

Background:

* def *nameofyourfunction* =

#Comment describing the fuction

"""
function() {

*code*

}
"""

****Scenario: This line is required please do not delete - or the functions cannot be called****

I think you've already seen the answer here, and this question is an exact duplicate: https://stackoverflow.com/a/47002604/143475 (edit: ok, maybe not)

Anyway, I'll repeat what I posted there:

  • there is no problem when you define multiple functions in one feature and call it from multiple other features
  • you will anyway need a unique name for each function
  • when you use call for that feature, all the functions will be available, yes, but if you don't use them, that's okay. if you are worrying about performance and memory, IMHO that is premature optimization
  • if that does not sound good enough, one way to achieve what you want is to define a Java class Foo with a bunch of static methods. then you can do Foo.myMethodOne(), Foo.myMethodTwo() to your hearts content. I would strongly recommend this approach in your case, because you seem to be expecting an explosion of utility methods, and in my experience, that is better managed in Java, just because you can maintain that code better, IDE support, unit-tests, debugging and all

Hope that makes sense !

Related