Forum Discussion

tixix's avatar
tixix
New Contributor
9 years ago

How to return value of a JavaScript function evaluated by Groovy

Hi Guys,   I have the following code:   import javax.script.*; def engine = new ScriptEngineManager().getEngineByName("javascript") assert engine.eval(''' function myFunction(p1, p2) { ret...
  • tixix's avatar
    tixix
    9 years ago

    Thanks Rupert for your fastanswer,

     

     

    a) I checked this post and it helped me a lot one week ago (Thanks again!!!)

    The problem that I have right now is the a bit different:

    In my testcase, I use only Groovy as scripting language and it is fine (so far so good). BUT the service i'm testing right now uses some Javascript Libraries. I DONT want to change the script language to JS, because then, all my groovy scripts will not work again. Thats why this work around.

     

    But It is solved now... i was thinking too complicated .

     

    import javax.script.*;
    
    def engine = new ScriptEngineManager().getEngineByName("javascript")
    
    def a =  engine.eval('''
    
    function myFunction(p1, p2) {
        return p1 * p2; // The function returns the product of p1 and p2
    }
    	myFunction(6, 4)
    
    
    ''') 
    
    log.info(a)

     

    Thanks.