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) {
    return p1 * p2; // returns the product of p1 and p2
}
	myFunction(6, 4)


''') == 24

The assertion works, BUT I want the value to returned, so that I can use it for the next calculation.... How can I do this?

 

THX

 

 

  • 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.

  • rupert_anderson's avatar
    rupert_anderson
    Valued Contributor

    Hi,

     

    It looks quite interesting the approach you're taking, using inline JavaScript I mean.

     

    I didn't totally understand what what value you wanted returned? As it looks like you aready get one returned and you compare it to 24?

     

    Also, is there any particular reason that you:

     

    a) don't use the out-of-the-box JavaScript support - see http://community.smartbear.com/t5/SoapUI-Open-Source/I-wan-t-to-use-SoupUI-Free-Version-with-JavaScript-assertion/m-p/112713#M19534

     

    b) use Groovy instead for your function? Is it that you have a JavaScript library or something, not shown in the post?

     

    Thanks,

    Rupert

    • tixix's avatar
      tixix
      New Contributor

      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.

      • rupert_anderson's avatar
        rupert_anderson
        Valued Contributor

        Hi,

         

        Thanks for the reply, its nice to understand more, makes sense now.

         

        I did think that you had actually solved the problem when I saw the post e.g. just capture the return value first as you have shown :-)

         

        Thanks for sharing the solution back and maybe mark the post as solved with your own answer as the solution if you're happy to, the green tick could help others! 

         

        Regards,

        Rup