Soap UI:Groovy测试步骤:如何从另一个Groovy脚本中的groovy脚本中调用特定方法

时间:2019-02-23 06:13:55

标签: groovy soapui

在我的项目中,我希望将所有常规工具的测试步骤都放在一个测试用例下,并在需要的地方一次又一次地调用它们。像读取测试数据文件等。如果解决了以下问题,我将能够实现。我尝试了很多方法,但未能成功。欢迎任何帮助!

例如

脚本1:test1Script

def sayHellow(){
log.info "Hello!!"

}

脚本2:test2Script

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

def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def projectPath = groovyUtils.projectPath
def scriptPath = projectPath + "\\GroovyScripts\\"



//GroovyShell shell = new GroovyShell()
//Util = shell.parse(new File(directoryName + "groovyUtilities.groovy"))
//groovyUtilities gu = new groovyUtilities(Util)

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

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

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

// Sanity check 
assert instance!= null

// run the foo method in the external script
instance.sayhellowTest()

当我从另一个脚本中调用该方法时,我将遇到异常

groovy.lang.MissingPropertyException: No such log for class test1Script

3 个答案:

答案 0 :(得分:1)

该错误表明groovy运行时调用了您的方法,但找不到log属性。我假设此log变量是在test1Script主体中声明的,例如def log = ...在这种情况下,变量在其声明范围内变为局部,并且对于脚本函数不可见。 To make it visible,可以用@Field注释,也可以不声明(没有类型声明,只有log = ...)。但是,后者要求您在运行脚本时通过所谓的绑定传递变量值。因此,根据上述假设,您可以将log变量注释为字段,并且该字段应该可以工作:

//just for the sake of example it prints to stdout whatever it receives
@groovy.transform.Field
def log = [info: {
    println it
}]

def sayHellow() {
    log.info "Hello!!"
}

现在从另一个脚本调用sayHellow会将“ Hello”打印到标准输出:

...
instance.sayHellow()

答案 1 :(得分:0)

在被调用的脚本中声明,上下文,testRunner和Log变量非常重要。

脚本1:sayHello.groovy

public class groovyUtilities {

    def context
    def testRunner
    def log

    def sayhellowTest() {
        log.info "Hi i'm arpan"
    }
}

脚本2:RunAnotherGroovyScript.groovy

def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def projectPath = groovyUtils.projectPath
def scriptPath = projectPath + "\\GroovyScripts\\"

// Create Groovy Script Engine to run the script and pass the location of the directory of your script.
GroovyScriptEngine gse = new GroovyScriptEngine(scriptPath)

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

// Create a runtime instance of script
instance = externalScript.newInstance(context: context, log: log, testRunner: testRunner)

// Sanity check if the instance is null or not
assert instance != null

// run the foo method in the external script
instance.sayhellowTest()

标准输出:

Hi i'm arpan

答案 2 :(得分:0)

  

“我想将所有常规的测试步骤都保留在一个测试用例下,   在需要的地方一遍又一遍地打电话给他们。喜欢阅读   测试数据文件等。

好的,对我来说,这听起来像您拥有一个可重用函数库,并希望能够从您可能正在运行的任何测试中调用它们。

我想您可以将它们与另一个测试一起存储,然后从当前正在运行的测试中调用它们,但是SoapUI具有简洁的功能,因为您可以将常用功能/库存储在SoapUI项目的“外部”。

我有很多这样的Groovy库,并将我的库存储在SoapUI的bin / scripts文件夹下。我通常在我正在运行的测试中的脚本声明测试步骤中调用通用函数。例如,我有一个getUserDetails类型测试步骤。我可以针对该步骤执行所有常规声明,例如有效的响应代码,SLA等。然后可以使用脚本声明测试步骤。这种类型的步骤允许您运行大量Groovy脚本。对于特定情况,这是可以的,但是您不希望粘贴一些常见的内容,因为如果发生更改,则需要更新每个Script断言。但是您可以调用“外部”常规脚本。另外,“脚本声明”步骤只是一个传递了日志,上下文和消息交换的方法,因此无需实例化您自己的方法。只需将它们传递给您的外部常规脚本即可。

所以,作为说明...

ValidateUser.groovy(存储在bin / scripts / groovyScripts / yourOrg / common中)

package groovyScripts.yourOrg.common;  // Package aligns with folder it's stored in.

Class ValidateUser {

    def log = null;
    def context = null;
    def messageExchange = null;

    // Constructor for the class
    ValidateUser(logFromTestStep, contextFromTestStep, messageExchangeFromTestStep) {

        // Assign log, context and messageExchange to the groovy class.
        this.log = logFromTestStep;
        this.context = contextFromTestStep;
        this.messageExhange = messageExchangeFromTestStep;

    }

    def runNameCheck() {

        // Performs some validation.  You have access to the API response via 
        // this.messageExchange

        log.info("Running the Name Check");

    }
}

在感兴趣的测试步骤中,转到断言并创建“脚本断言”,从这里您可以实例化外部类并调用某些方法。例如

def validateUserObject = new groovyScripts.yourOrg.common.ValidateUser(log, context, messageExchange);

validateUserObject.runNameCheck();

对于这些外部类型脚本,我喜欢的是可以使用任何喜欢的文本编辑器。另外,当我进行更改并按Save时,SoapUI会监视scripts文件夹中的更改并重新加载脚本,因此无需重新启动SoapUI。

相关问题