问题
How can I include groovy script from an external file?

I was tried to use:
def script = new GroovyScriptEngine('d:/soapui/payment.v2').with {
loadScriptByName( 'proxy.groovy' )
}
this.metaClass.mixin script
But I get:

Update
Is there exists some possibility to pack my methods into jar or something like this, and use them from Script TextArea
?
回答1:
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()
回答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:
- Create your code in java classes inside a package.
- Right click on the package and export (select jar)
- Add this jar to soapui /bin/ext folder (make sure to close soapui before this step)
restart soapui and now you can import and use the scripts inside soapui as below.
Create a groovy step and import the jar
import package name.class name
Call the function as:
class name.function name(parameter);
回答3:
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'));
回答4:
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.
来源:https://stackoverflow.com/questions/14357680/soapui-how-to-include-groovy-script-from-an-external-file