2

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)

}
"""
Matthew Hayhurst
  • 69
  • 1
  • 2
  • 8

2 Answers2

3

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****
Matthew Hayhurst
  • 69
  • 1
  • 2
  • 8
  • well. now it is clear that your question was not clear and totally misleading. glad you got it working. I guess you never looked at the demo examples. anyway, if you had used `Scenario` instead of `Background` above you would have saved all of us a lot of time :P – Peter Thomas Nov 24 '17 at 10:16
  • I thought javascript functions should be stored in the background....and that only Cucumber should be within the Scenario section so it is easy to read. – Matthew Hayhurst Nov 24 '17 at 10:26
  • 1
    fair enough, sorry about that. don't you think it is rude to not upvote answers on Stack Overflow when a lot of time was spent typing it out ? – Peter Thomas Nov 24 '17 at 10:44
  • 1
    OK done that (I've never used Stack Overflow before so I don't know the etiquette). I still think that my functions are better in the Background of the called file. If I move it to the Scenario section then the function is ran when when the file is called. I only want to run it when required which I can do if I leave it in the background. – Matthew Hayhurst Nov 24 '17 at 10:53
  • 2
    peace. as far as I know there should not be a difference. one suggestion, look at line 17 here, it is a way of using a JS function from a file, which may be all that you need: https://github.com/intuit/karate/blob/master/karate-demo/src/test/java/demo/search/dynamic-params.feature#L17 – Peter Thomas Nov 24 '17 at 10:59
  • 1
    Thanks Peter. Yes I was going to use a .js file originally but in Karate you can only have 1 function per .js file. You suggested using a .feature file which I have now done. I will use this as a master file to contain several javascript functions that can be used in other feature files - now I know how to do it. Regards Matt – Matthew Hayhurst Nov 24 '17 at 11:06
  • that's right. the approach you arrived at (in your answer to the original question here) is a good one. I'm not sure if you can mark your own answer as "accepted" but if you can, please do so. – Peter Thomas Nov 24 '17 at 11:09
  • 1
    It says I can accept an answer tomorrow. I'll do it on Monday. Thanks for your help. – Matthew Hayhurst Nov 24 '17 at 16:01
2

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 !

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248