且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-12-05 13:29:46

该错误表明groovy运行时调用了您的方法,但找不到 log 属性.我假设在 test1Script 主体中声明了此 log 变量,例如 def log = ... 在这种情况下,变量成为其声明范围的局部变量,并且对于脚本函数不可见.要使其可见,可以用

The error shows that groovy runtime calls your method but it can't find the log property. I assume that this log variable is declared in the test1Script body, e.g. def log = ... In this case the variable becomes local to its declaration scope and it's not visible to the script functions. To make it visible, it can be annotated by @Field or it should be undeclared (doesn’t have type declaration, just log = ...). The latter, however, requires you to pass the variable value via so-called bindings when running the script as you run it. So given my assumptions above, you can annotate your log variable as a field and it should work:

//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"打印到标准输出:

Now calling sayHellow from another script prints "Hello" to stdout:

...
instance.sayHellow()