Soap UI:Groovy测试步骤:如何从另一个Groovy脚本中调用groovy脚本中的特定方法 [英] Soap UI: Groovy Test Step : How to call a Specific Method in a groovy script from another Groovy Script
问题描述
在我的项目中,我想将所有常规的测试步骤都放在一个测试用例下,并在需要的地方一次又一次地调用它们.像读取测试数据文件等.如果解决了以下问题,我将能够实现.我尝试了很多方法,但未能成功.欢迎任何帮助!
In my project, I want to keep all groovy utilities test step under one test case and to call them again and again where ever is needed. Like reading the test data file etc. I would be able to achieve that if the below problem is resolved. I tried a lot of ways but couldn't make it. Any help is welcome!!
例如
脚本1:test1Script
script 1: test1Script
def sayHellow(){
log.info "Hello!!"
}
脚本2:test2Script
Script 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()
当我从另一个脚本中调用该方法时,我将遇到异常
When I'm calling that method from another script, I'm getting below exception
groovy.lang.MissingPropertyException:类test1Script没有此类日志
推荐答案
该错误表明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()
这篇关于Soap UI:Groovy测试步骤:如何从另一个Groovy脚本中调用groovy脚本中的特定方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!