SOAPUI Load Custom Properties from file using groovy

丶灬走出姿态 提交于 2019-12-23 17:04:36

问题


I am trying to write a groovy script which loads the custom properties for a test suite using information from a properties file. The properties file has around 6 different attributes I have had a look at quite a few different methods i.e Loading from Properties test step and trying to expand the properties with groovy, but have not been successful.

If anyone could advise on how to achieve this, it would be much appreciated.

Thanks in advance.


回答1:


Here is the groovy script which reads a property file and set them at test suite level:

def props = new Properties()
//replace the path with your file name below. use / instead of \ as path separator even on windows platform.
new File("/absolute/path/of/test.properties").withInputStream { s ->
  props.load(s) 
}
props.each {
    context.testCase.testSuite.setPropertyValue(it.key, it.value)
}

The above script load test suite level for the current suite where the groovy script is present.




回答2:


Unfortunately, in my case I want to have the properties in the same order as the input file, ie. sorted, and this methode does not work. I wanted to load a 'Project properties' file containing sorted properties and each time I used this method it stored them unsorted. I had to use a more straightforward method (see below). If anyone knows about a more elegant/practical way to do it, I'm interested

def filename = context.expand( '${#TestCase#filename}' )

def propertiesFile = new File(filename)
assert propertiesFile.exists(), "$filename does not exist"

project = testRunner.testCase.testSuite.project

//Remove properties
project.propertyNames.collect{project.removeProperty(it)}

//load the properties of external file
propertiesFile.eachLine {
    line->
    firstIndexOf = line.indexOf('=') // properties as set as key=value in the file
    key = line.substring(0, firstIndexOf)
    value = line.substring(firstIndexOf+1)
    project.setPropertyValue(key, value)
}


来源:https://stackoverflow.com/questions/37723177/soapui-load-custom-properties-from-file-using-groovy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!