Reputation: 353
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.
Upvotes: 3
Views: 4197
Reputation: 2387
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)
}
Upvotes: 0
Reputation: 21379
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.
Upvotes: 4