昨晚帮老同事解决了一个SoapUI的代码问题,好长时间没用SoapUI,好多东西都忘了,今天先总结下Groovy读取properties
首先吐槽下SoapUI的apidocs,我几乎从中看不出什么东西,官网的tips有那么一点用,但是好长时间没有更新了,好多东西都找不到。而且在SoapUI里调试Groovy代码几乎不太可能。
APIDocs: http://www.soapui.org/apidocs/index.html?com/eviware/soapui/model/testsuite/TestRunContext.html
废话不多说,首先获取当前的testsuite:
def ts = testRunner.testCase.testSuite
获取的properties文件路径:
def filename = “C:\\TestSuiteProperties\\CurrentTestingEnvironment\\endpoints.properties"
log.info "Loading properties from " + filename
使用java包导入properties:
def props = new java.util.Properties();
props.load( new java.io.FileInputStream(filename));
利用Emumeration读取properties value,并设置到testsuite的properties中去:
Enumeration e = props.keys();
while (e.hasMoreElements())
{
key = e.nextElement();
val = props.get(key);
ts.setPropertyValue(key, val);
log.info "Set property " + key + " to " + val;
}
这是一个properties参数的初始化过程。
另外一个一次读取txt文件的内容赋值给request的脚本:
def file = new File("C:/temp/yourFile.txt") // for each line
file.eachLine { line ->
// put the property for your request
testRunner.testCase.setPropertyValue("myProperty",line)
// execute your request
testRunner.runTestStepByName( "myRequest")
log.info "execute request for line: " + line
}