groovy/SoapUi从另一个脚本调用一个静态函数

问题描述:

我想写一个groovy脚本,它将包含SoapUI测试套件常用的函数。具体而言,我想编写一个脚本,其中包含从测试套件输出的所有日志。groovy/SoapUi从另一个脚本调用一个静态函数

GroovyScript1将调用GroovyScripts.groovy文件中的函数。所有这些都存在于SoapUI测试套件中。

我还没有找到任何有关如何执行此任务的建议。

要再次指定,我想调用另一个Groovy脚本中包含的函数。

+0

不完全。但是这样做会提供所有可重用方法,像GroovyScripts {//所有可重用的方法都在这里}这样的常规类。编译这个类并创建一个jar文件,并将这个文件放在SOAPUI_HOME/bin/ext目录下。现在,只需要在任何项目的Groovy脚本测试步骤中从上级调用方法。希望这可以帮助。 – Rao

是的,你可以通过以下步骤做到这一点,

在你的“GroovyScripts.groovy”文件中添加以下代码,

class GLF 
{ 

    def log 
    def context 
    def testRunner 

    def GLF(logIn, contextIn, testRunnerIn) 
    { 
     this.log = logIn 
     this.context = contextIn 
     this.testRunner = testRunnerIn 
    } 

    //Till abobe line you must keep same code except class name 

    public String returnVal() 
    { 
     return 'Himanshu' 
    } 
} 

context.setProperty("Rt", new GLF(log, context, testRunner)) 
============================ END GroovyScripts.groovy ========== 

现在在你的“GroovyScript1”的文件,你应该在下面的代码中使用,

lib = testRunner.testCase.testSuite.project.testSuites["GroovyLibraryFunction"].testCases["TestCase 1"].testSteps["EndpointVerification"] 

lib.run(testRunner, context) 

def RT = context.Rt 

def PT = RT.returnVal() 

log.info PT 

这样你就可以achive你的目标。