13

How can I include groovy script from an external file? enter image description here

I was tried to use:

def script = new GroovyScriptEngine('d:/soapui/payment.v2').with { 
    loadScriptByName( 'proxy.groovy' ) 
} 
this.metaClass.mixin script

But I get:

enter image description here

Update

Is there exists some possibility to pack my methods into jar or something like this, and use them from Script TextArea?

CAMOBAP
  • 5,523
  • 8
  • 58
  • 93

4 Answers4

18

The simplest way is to use Groovy Test Step within SOAPUI to run the external script using the GroovyScriptEngine. I use GroovyUtils to find the Project's path so that the entire project can be kept in one place to aid source control, editing etc.

import groovy.lang.Binding
import groovy.util.GroovyScriptEngine

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )

// location of script file is relative to SOAPUI project file.
String scriptPath = groovyUtils.projectPath + "/groovy/"

// Create Groovy Script Engine to run the script.
GroovyScriptEngine gse = new GroovyScriptEngine(scriptPath) 

// Load the Groovy Script file 
externalScript = gse.loadScriptByName("Utility.groovy")  

// Create a runtime instance of script
instance = externalScript.newInstance()

// Sanity check 
assert instance!= null

// run the foo method in the external script
instance.foo()
Martin Spamer
  • 5,437
  • 28
  • 44
  • kinda late as wel, but happens when you export the project as a WAR? do you then need a groovy/ directory somewhere on the server? – Dave Mar 01 '17 at 20:34
  • I've not tried it, but if it held relative to the project it should work fine. – Martin Spamer Feb 23 '18 at 15:19
  • When i'm calling the method from the script using instance.gettestData("ReadPersonalDataTest.xls","dataSheet") , getting error "Missing Property Exception" – Arpan Saini Feb 23 '19 at 05:25
2

You can also create your scripts in java (eclipse) and then export it as a jar and add in soapui.

Here are the step:

  1. Create your code in java classes inside a package.
  2. Right click on the package and export (select jar)
  3. Add this jar to soapui /bin/ext folder (make sure to close soapui before this step)
  4. restart soapui and now you can import and use the scripts inside soapui as below.

  5. Create a groovy step and import the jar

    import package name.class name

  6. Call the function as:

    class name.function name(parameter);

1

Try this:

GroovyShell gs = new GroovyShell(getBinding()); 
gs.evaluate(new File('path/to/external.groovy').text);

Or even this:

evaluate(new File('path/to/external.groovy'));
Artem Zankovich
  • 2,319
  • 20
  • 36
  • I got `java.lang.Exception cannot get property 'requestContext' on null object` message for both. did you checked they before posting? – CAMOBAP Jan 21 '13 at 16:05
  • Check the code in your external file. Groovy engine ran it and got exception. Replace content of the external file with simple command to see how it works: log.info "Hello from external file" – Artem Zankovich Jan 21 '13 at 20:37
  • When I placed the content of the external file into Script TextArea it work perfect. Problem occurs each time when I tried access to `log`, `context`, `requestContext`, `mockRequest` and `mockResponse` variables – CAMOBAP Jan 21 '13 at 20:45
  • Late comment to the thread, but you are not getting those variables in the "Load Script" tab. It needs to be in the tear-up or tear-down part. – PeterS Aug 10 '16 at 14:30
0

For the mixin error message, you might get rid of that if you use ExpandoMetaClass.enableGlobally() and then this.metaClass = null. at the very beginning, before you start mixing in.

CAMOBAP
  • 5,523
  • 8
  • 58
  • 93
blackdrag
  • 6,413
  • 2
  • 26
  • 38